bcc / extra-tools-bundle
一套用于 Symfony2 的额外工具包:翻译提取器、区域日期解析/格式化器、单位转换...
Requires
- php: >=5.3.2
- symfony/framework-bundle: 2.*
This package is not auto-updated.
Last update: 2024-09-14 13:07:34 UTC
README
这是一个包含一些有用 symfony2 工具的包。
警告
bcc:trans:update
命令已被合并到框架中,因此将不再在此处维护。新名称为 translation:extract
。
功能
- 一个
bcc:trans:update
命令,可以提取所有从 twig 模板中缺失的 i18n 消息,并将其保存到 yaml、xliff、php 或 pot 翻译文件中。 - 一个可以翻译日期和国家的 twig 扩展
- 一个日期格式化器,可以格式化和翻译日期,还可以解析多种本地化日期字符串格式
- 一个仅可扩展且可转换单位的单位转换器
安装和配置
获取该包
将其添加到您的 /deps
文件中
[BCCExtraToolsBundle]
git=http://github.com/michelsalib/BCCExtraToolsBundle.git
target=/bundles/BCC/ExtraToolsBundle
然后运行 php bin/vendors install
。
注册命名空间
<?php // app/autoload.php $loader->registerNamespaces(array( 'BCC' => __DIR__.'/../vendor/bundles', // your other namespaces ));
将 ExtraToolsBundle 添加到您的应用程序内核中
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new BCC\ExtraToolsBundle\BCCExtraToolsBundle(), // ... ); }
注册 twig 扩展
如果您想使用 twig 扩展,则必须安装 apache intl 模块。
将其添加到您的 config.yml
#BCC configuration services: bcc.twig.extension: class: BCC\ExtraToolsBundle\Twig\TwigExtension tags: - { name: twig.extension }
使用示例
bcc:trans:update 命令示例
您现在有了新命令。您可以使用它如下
-
从您的包中提取消息并在控制台显示
bcc:trans:update --dump-messages fr MyBundle
-
您可以将它们保存到
MyBundle\Resources\translations
目录中bcc:trans:update --force fr MyBundle
-
在其他语言中
bcc:trans:update --force en MyBundle
-
使用
--output-format
选项(可以是yml
、xliff
、php
或pot
)指定输出格式bcc:trans:update --output-format="xliff" --force en MyBundle
-
使用
--prefix
选项更改用于新添加消息的前缀bcc:trans:update --output-format="xliff" --force --prefix='myprefix' en MyBundle
Twig 扩展示例
翻译日期值
-
{{ user.createdAt | localeDate }}
以在当前区域显示中等日期和没有时间 -
{{ user.createdAt | localeDate('long','medium') }}
以在当前区域显示长日期和中等时间
翻译国家
-
{{ user.country | country }}
以在当前区域显示国家 -
{{ user.country | country('c ountry does not exist') }}
定义如果国家不存在时返回的值
DateFormatter 示例
获取服务
<?php $dateFormatter = $container->get('bcc_extra_tools.date_formatter');
解析日期
<?php $date = $dateFormatter->parse('November 1, 2011', 'en'); // obtains a datetime instance
格式化日期
<?php echo $dateFormatter->format($date, 'long', 'none', 'fr'); // echoes : "1 novembre 2011"
注意,区域参数(此处为 'fr' 和 'en')是可选的,默认为当前区域。
单位转换器示例
获取服务
<?php $unitConverter = $container->get('bcc_extra_tools.unit_converter');
转换值
<?php echo $unitConverter->parse(1000, 'm', 'km'); // echoes : 1
猜测源单位
<?php echo $unitConverter->parse('1h', 'm'); // echoes : 60
更多示例在测试中:./Tests/Util/UnitConverterTest。