【问题标题】:symfony2 translation with transchoice带有transchoice的symfony2翻译
【发布时间】:2012-06-09 01:03:05
【问题描述】:

我正在翻译activity.fr.yml中的以下键

user.list.link: '{1}et %count% autre|]1,Inf[voir les %count% autres'

使用 tranchoice

<a href="{{ moreLink }}" >{% transchoice count from "activity" %}user.list.link{% endtranschoice %}</a>

我收到以下错误

An exception has been thrown during the rendering of a template ("Unable to choose a translation.")

我认为翻译已经找到,否则我不会收到有关无法选择翻译但密钥本身的错误。

同样,来自同一个 yaml 的所有其他键,甚至其他 tranchoice 都得到了很好的翻译。

我关注了doc并尝试添加with {'%count%': count}但没有成功。

有人知道这里出了什么问题吗? 提前致谢

【问题讨论】:

    标签: symfony translation yaml twig pluralize


    【解决方案1】:

    语法很好,但作为 %count% 传递的值不能为负数,也不等于 0,因为复数字符串中没有 {0} 定义。 所以我进行了测试以确保该值 >= 0 并像这样修改了字符串并修复了它。

    user.list.link: '{0}|{1}et %count% autre|]1,Inf[voir les %count% autres'
    

    【讨论】:

      【解决方案2】:

      您需要传递用于确定要选择的翻译的参数。

      看下面的例子发现in the doc

      {% transchoice count with {'%name%': 'Fabien'} from "app" %}
          {0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples
      {% endtranschoice %}
      

      适应你的例子:

      {% transchoice count with {'%count%': count} from "activity" %}
          user.list.link
      {% endtranschoice %}
      

      如果它不起作用,则可能找不到您的翻译。因此,symfony 使用您的密钥作为后备,并且无法确定有效的选择,因为您的密钥不支持。

      要检查这一点,请尝试使用这样的密钥:

      user.list.link | user.list.link.many
      

      不要忘记在您的活动目录中使用相同的键。

      【讨论】:

      • 我认为翻译已经找到,否则我不会收到关于 Unable to choose a translation 的错误,而是密钥本身。
      • 通过添加您的建议{% transchoice count with {'%count%':count} from "activity" %}user.list.link.link|user.list.many{% endtranschoice %} 我得到user.list.link.many 代替模板中的字符串
      • 如果没有找到翻译,就是使用的key。并且不可能用这样的键选择翻译。也许错误是由于您的翻译和我的示例之间的空格造成的。试试user.list.link|user.list.link.many。 (当然,在您的目录中使用相同的内容并清除缓存。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2015-10-07
      • 1970-01-01
      相关资源
      最近更新 更多