2013-03-19 30 views
0

我有一個Highcharts圖,裏面有文本。如何根據所選語言更改此文本?這裏是我的代碼:Ruby on Rails i18n更改Ajax內部的文本

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裏面。我應該怎麼做 ?

回答

0

無論你的文件被調用時,只需添加erb擴展:

my_file.js.erb

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

還要注意的是<%=打印結果,<%沒有。

編輯:這假設你正在使用導軌資產管道,如果你不是它會複雜得多。

+0

我的文件名爲index.html.erb。我試過index.html.js.erb,但後來出現「模板丟失」錯誤 – 2013-03-19 21:41:45

+0

您的javascript不在外部js文件中?那麼我假設你已經使用'

0

嘗試使用i18n-js寶石通過Rails I18n.locale到您的JavaScript文件。