Symfony2 中的高级翻译定制 [英] Advanced customization of translations in Symfony2

查看:22
本文介绍了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

Translating text looks like this from 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:

  1. yml 文件中定义两个文本,用于需要不同文本的翻译.每个都有自己独特的后缀

  1. 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,Translator 组件不支持.

    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:      AcmeHelloBundleTranslationTranslator
    

    首先,您可以通过在app/config/config.yml 中设置来将保存服务类名的参数设置为您自己的类.仅供参考:https://github.com/symfony/FrameworkBundle/blob/master/Resources/配置/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 组件 提供的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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆