【问题标题】:Rails translations issue with i18n-jsi18n-js 的 Rails 翻译问题
【发布时间】:2015-08-16 20:35:36
【问题描述】:

我正在使用http://github.com/fnando/i18n-js gem 进行 JavaScript 翻译。这是我的翻译 yml:

en:
  js:
    test: "<strong>%{name}</strong> created this board."

在我的 JS 中,我使用 "name" 参数运行是 "$&"。

console.log(I18n.t('js.test', {name: "$&"}))

不幸的是,它会显示如下消息:%{name} created this board,而我希望该消息应该是:$& created this board。 这个宝石有像“&”这样的特殊字符有什么问题吗?以及如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    您的代码中的问题是,您的插值不起作用,因为您在翻译文件中使用了%{name}。您必须使用:{{name}} 来进行插值。

    所以,改变:

    test: "<strong>%{name}</strong> created this board."
    

    到这里:

    test: "<strong>{{name}}</strong> created this board."
    

    它应该可以工作!

    【讨论】:

    • 好吧,不幸的是,我使用了您的解决方案,现在它会显示如下消息:{{name}} 创建了这个板。你能再检查一下吗?谢谢。
    • 尝试不使用双引号 ""&lt;strong&gt;,先看看是否可行:test: {{name}} created this board.
    • 好吧,现在它抛出翻译错误:解析块映射时没有找到预期的键。
    • 怎么样:test: "{{name}} created this board." 并传递一些简单的名称,例如 John
    • 实际上,像这样简单的事情总是很有效。但就我而言,这是一个带有特殊字符“$&”的​​单词,它不起作用。
    猜你喜欢
    • 2013-06-08
    • 2015-03-25
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2010-11-22
    • 2011-10-11
    相关资源
    最近更新 更多