【问题标题】:i18n Pluralization railsi18n 复数化轨道
【发布时间】:2013-11-11 19:23:57
【问题描述】:

我需要根据数字对文本进行复数。所以我的翻译文件有这个,

   en:
     people:
       one: %{count} People
       other: %{count} Peoples

问题是,我需要将数字加粗。所以在我看来我有这个,

 raw t(.people, count: content_tag(:strong, 4)) 

由于我包含了一个 content_tag,这是否会使用<strong> 4 </strong> 'count' 一个字符串并将其传递给翻译并始终返回'Peoples'?

或者有没有更好的方法来做到这一点。

谢谢

【问题讨论】:

    标签: ruby-on-rails internationalization rails-i18n


    【解决方案1】:

    一种选择是使用原始 i18n 输出并将 HTML 放入 i18n:

    en:
     people:
       one: <strong>%{count}</strong> People
       other: <strong>%{count}</strong> Peoples
    

    那么在你的翻译中使用:

    &lt;%= raw t(:people, count: 4) %&gt;

    这并不理想,但您的用例可能足够简单,这是最佳解决方案。

    另一种方法是使用两种不同的翻译并将它们嵌套,一种仅用于将其包裹在强标记中的数字,另一种用于单词的复数形式,传递强HTML,类似于您如何进行链接:

    text_with_anchor: This is a link. %{href} to click it!
    
    t(:text_with_anchor, href: link_to(t(:another_translation), some_path))
    

    【讨论】:

    • 我将使用第二种方法,因为我认为在翻译中包含 html 元素不是一个好的选择。
    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多