【问题标题】:Ruby on Rails i18n change text inside AjaxRuby on Rails i18n 更改 Ajax 中的文本
【发布时间】:2013-03-19 20:44:08
【问题描述】:

我有一个 Highcharts 图表,里面有文字。如何根据所选语言更改此文本?这是我的代码:

var chart;
chart = new Highcharts.Chart({
(...)
    subtitle: {
        text: document.ontouchstart === undefined ?
        'Click and drag in the plot area to zoom in' :
        'Drag your finger over the plot to zoom in'
        }
(...)

我的 en.yml 文件看起来像:

en:
   actions:
      zoom: "Click and drag in the plot area to zoom in"

我试过了

t("actions.zoom")

<% t "actions.zoom" %>

但他们不在 Ajax 内部工作。我应该怎么做 ?

【问题讨论】:

    标签: ajax ruby-on-rails-3 internationalization highcharts


    【解决方案1】:

    无论您调用什么文件,只需添加 erb 扩展名:

    my_file.js.erb

        subtitle: {
        text: document.ontouchstart === undefined ?
          <%= t "actions.zoom" %> : 'something something'
        }
    

    还要注意&lt;%= 会打印结果,&lt;% 不会。

    编辑:这假设您正在使用 rails 资产管道,如果您不是,它将更加复杂。

    【讨论】:

    • 我的文件名为 index.html.erb。我尝试了 index.html.js.erb,但出现错误“模板丢失”
    • 您的 javascript 不在外部 js 文件中?然后我假设你已经使用&lt;script&gt; 标签将它放在 html 页面上还是什么?如果是这种情况,&lt;%= t "actions.zoom" %&gt; 应该可以工作,并且您没有表明您已经使用= 进行了尝试
    【解决方案2】:

    尝试使用 i18n-js gem 将 Rails I18n.locale 传递到您的 javascript 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多