avoo / serializer-translation
此包已被弃用且不再维护。未建议替代包。
JMS Serializer 的翻译组件
0.1.1
2015-06-01 13:40 UTC
Requires
- php: >=5.3.3
Requires (Dev)
This package is not auto-updated.
Last update: 2023-05-22 20:08:29 UTC
README
![构建状态] (https://scrutinizer-ci.com/g/avoo/SerializerTranslation/badges/build.png?b=master) ![Scrutinizer 代码质量] (https://scrutinizer-ci.com/g/avoo/SerializerTranslation/badges/quality-score.png?b=master) ![最新稳定版本] (https://poser.pugx.org/avoo/serializer-translation/v/stable.svg) ![许可证] (https://poser.pugx.org/avoo/serializer-translation/license.svg)
这是一个基于 JMS Serializer 的 PHP 库,并为任何属性添加了翻译选项配置。
安装
将 avoo/serializer-translation-bundle
添加到您的 composer.json
文件中
{ "require": { "avoo/serializer-translation-bundle": "@dev-master" } }
在 app/AppKernel.php
中注册该包
// app/AppKernel.php public function registerBundles() { return array( // ... new Avoo\SerializerTranslationBundle\AvooSerializerTranslationBundle(), ); }
默认配置
# app/config/config.yml avoo_serializer_translation: metadata: cache: file file_cache: dir: %kernel.cache_dir%/avoo
默认用法
例如,您想将 BDD 中的 acme.foo.bar
翻译。
默认激活
到您的翻译文件中
# Acme/DemoBundle/Resources/translations/messages.en.yml acme: foo.bar: "Welcome."
<?xml version="1.0" encoding="UTF-8" ?> <serializer> <class name="Acme\DemoBundle\Entity\Sample" exclusion-policy="ALL" xml-root-name="sample" xmlns:a="https://github.com/avoo/SerializerTranslationBundle"> <property name="property" expose="true" type="string"> <a:translate /> </property> </class> </serializer>
YAML
Acme\DemoBundle\Entity\Sample: exclusion_policy: ALL xml_root_name: sample properties: property: expose: true type: string translate: true
注解
重要:需要定义注解。
use Avoo\SerializerTranslation\Configuration\Annotation as AvooSerializer; /** * @var string $property * * @AvooSerializer\Translate() */ protected $property;
JSON 输出示例
{ "property": "welcome." }
使用自定义参数
使用自定义参数
到您的翻译文件中
# Acme/DemoBundle/Resources/translations/messages.en.yml acme: foo.bar: "welcome %foo%"
XML
<?xml version="1.0" encoding="UTF-8" ?> <serializer> <class name="Acme\DemoBundle\Entity\Sample" exclusion-policy="ALL" xml-root-name="sample" xmlns:a="https://github.com/avoo/SerializerTranslationBundle"> <property name="property" expose="true" type="string"> <a:translate domain="messages" locale="en"> <a:parameter name="%foo%" value="expr(object.getProperty())" /> </a:translate> </property> </class> </serializer>
YAML
Acme\DemoBundle\Entity\Sample: exclusion_policy: ALL xml_root_name: sample properties: property: expose: true type: string translate: parameters: %foo%: expr(object.getProperty()) locale: en domain: messages
注解
use Avoo\SerializerTranslation\Configuration\Annotation as AvooSerializer; /** * @var string $property * * @AvooSerializer\Translate( * parameters = { * "%foo%" = "expr(object.getProperty())" * }, * domain = "messages", * locale = "en" * ) */ protected $property;
JSON 输出示例
{ "property": "welcome Superman", }
许可证
此包在 MIT 许可证下发布。请参阅包中的完整许可证。