Symfony2中翻译的高级定制 [英] Advanced customization of translations in Symfony2
问题描述
我有一个Symfony2
项目,并且正在使用Translation
组件来翻译文本.我的所有翻译都在yml
文件中
I have a Symfony2
project and I am using Translation
component for translating text. I have all translations in yml
file like so
translation-identifier: Translated text here
在Twig
'translation-identifier'|trans({}, 'domain')
问题是,在某些情况下,我想为相同的翻译提供两个不同的文本(不为复数形式).这是我希望它起作用的方式:
The thing is, in some cases I would like to have two different texts for same translation (not for pluralization). Here's how I would like it to work:
-
在
yml
文件中为需要具有不同文本的翻译定义两个文本.每个人都有自己独特的后缀
Define two texts in
yml
file for translations that need to have different texts. Each would have it's own unique suffix
translation-identifier-suffix1
translation-identifier-suffix2
定义一个全局规则,该规则将定义应选择的后缀.伪代码如下:
Define a global rule that would define which suffix should be choosen. Psuedocode below:
public function getSuffix() {
return rand(0, 10) < 5 ? '-suffix1' : '-suffix2';
}
Twig(和PHP)看起来相同-我仍然只指定不带后缀的标识符.然后,翻译人员将后缀附加到标识符上,并尝试查找匹配项.如果没有匹配项,它将尝试再次找到不带后缀的匹配项.
Twig (and PHP) would look the same - I would still specify just the identifier without suffix. Translator would then append suffix to the identifier and try to find a match. If there would be no match it would try to find a match again without suffix.
推荐答案
AFAIK,翻译器组件不支持.
AFAIK, Translator component doesn't support it.
但是,如果您想要相同的行为,则可以通过覆盖翻译器服务来实现.
But if you want same kind of behavior, you could do by overriding the translator service.
1)覆盖服务
# app/config/config.yml
parameters:
translator.class: Acme\HelloBundle\Translation\Translator
首先,您可以通过在app/config/config.yml
中进行设置,将包含服务类名的参数设置为您自己的类.
仅供参考: https://github.com/symfony/FrameworkBundle/blob/master/Resources/config/translation.xml
First, you can set the parameter holding the service's class name to your own class by setting it in app/config/config.yml
.
FYI: https://github.com/symfony/FrameworkBundle/blob/master/Resources/config/translation.xml
2)扩展提供的翻译器类symfony framework bundle
.
仅供参考: https://github.com/symfony/FrameworkBundle/blob/master/Translation/Translator. php
2) Extend the translator class provided symfony framework bundle
.
FYI: https://github.com/symfony/FrameworkBundle/blob/master/Translation/Translator.php
3)覆盖由translator component
提供的trans
函数.
https://github.com/symfony/Translation/blob/master/Translator.php
3) Overwrite the trans
function which is provider by translator component
.
https://github.com/symfony/Translation/blob/master/Translator.php
希望这会有所帮助!
这篇关于Symfony2中翻译的高级定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!