【问题标题】:I18N with Express/Jade: Strings with embedded tags and interpolationI18N with Express/Jade:带有嵌入标签和插值的字符串
【发布时间】:2012-03-25 20:02:38
【问题描述】:

tl;dr:我正在寻找一种在 Jade 模板中进行国际化的优雅方法。本质上,问题归结为我必须对变量中的字符串进行插值,而不是逐字代码。

问题详情:

在单语 Jade 模板中,我可以制作一个带有嵌入式标签和变量的元素,如下所示:

p Posted by 
  span.author= post.author
  | on
  span.date= post.author

得到类似的东西

<p>Posted by <span style="author">The Author</span> on 
<span style="date">2012-03-08</span></p>

但是当我想将其国际化时,我需要一个字符串,因为每种语言的词序都不相同。另外,我想对翻译人员隐藏 html 详细信息,只需给他们这样一行:

Posted by #{author} on #{date}

现在,当我将此字符串的 i18n 版本作为 i18n.posted_by_on 传递给 Jade 模板时,它不会对其进行插值,所以我能做的最好的事情是:

- var author = '<span class="author">$</span>'.replace('$',post.author); 
- var date = '<span class="date">$</span>'.replace('$',post.date);
- var header = i18n.posted_by_on
      .replace('#{author}',author)
      .replace('#{date}',date); 
p!= header

这在很大程度上破坏了漂亮的 Jade 模板的意义,因为我必须手动进行所有插值。有没有更好、更紧凑、更易读的方法?

【问题讨论】:

    标签: internationalization express template-engine pug


    【解决方案1】:

    您现在可能找到了解决方案(如果是,请告诉我们它是什么),但如果您没有,您可能需要查看i18next-node 库。它支持变量,因此您可以执行以下操作:

    // Localized resources
    {           
      'en-GB': {
        translation: { 
            space_temperature_is: 'Space temperature is __COUNT__ degrees Celsius.'
         }
       }
      'fr-FR': {
        translation: { 
            space_temperature_is: 'Température de l'espace est de __COUNT__ degrés Celsius.'
         }
       }
    };
    

    然后,在你的翡翠模板中,你会这样做:

    // This translates to "Space temperature is 28 degrees Celsius."
    i18n.t('space_temperature_is', { COUNT: 28 });
    

    该库的文档确实很好,但是,如果您赶时间,这里是 an article,我发现它可以作为快速介绍。

    【讨论】:

    • 我从来没有找到答案(优先级转移),但这似乎很合适,所以我会接受它:)
    【解决方案2】:

    正如我们在简短文档中看到的那样,有一个 sprintf 风格的函数 https://github.com/mashpie/i18n-node 我认为这将帮助您实现目标

    【讨论】:

    • 您好,请原谅稍晚:) 回复。该工具似乎不符合要求,因为它使用位置参数。
    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 2016-10-04
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多