Rails:Javascript字符串的国际化?

时间:2010-04-23 20:20:29

标签: javascript ruby-on-rails internationalization

因此,我们有一个现有的Rails 2.3.5应用程序根本不支持国际化。现在,我对Rails I18n很熟悉,但我们在/javascripts/内有很多输出字符串。我不是这种方法的忠实粉丝,但不幸的是现在修复它已经太晚了。

我们如何在Rails应用程序中将存储在JS文件中的字符串国际化? Rails甚至不提供JS文件......

我想我总是可以让Rails应用程序提供JS文件,但这看起来非常糟糕。是否有插件可以做到这一点?

10 个答案:

答案 0 :(得分:93)

为什么不像以下那样简单:

<script type="text/javascript">
  window.I18n = <%= I18n.backend.send(:translations).to_json.html_safe %>
</script>

然后在JS中你可以做以下事情:

I18n["en-US"]["alpha"]["bravo"];

我已经在应用程序助手中包裹了我。

def current_translations
  @translations ||= I18n.backend.send(:translations)
  @translations[I18n.locale].with_indifferent_access
end

然后我在application.html.erb中的调用如下所示:

<script type="text/javascript">
  window.I18n = <%= current_translations.to_json.html_safe %>
</script>

这使您可以避免必须知道JavaScript中的当前语言环境。

I18n["alpha"]["bravo"];

或者

I18n.alpha.bravo;

答案 1 :(得分:18)

Balibu被遗弃了。使用i18n-js:https://github.com/fnando/i18n-js

答案 2 :(得分:8)

Ryan上面的解决方案是完美的,除非后端需要初始化,如果还没有。

I18n.backend.send(:init_translations) unless I18n.backend.initialized?
# now you can safely dump the translations to json

答案 3 :(得分:6)

对于rails 3应用程序,您可以这样做:

创建一个i18n.js.erb文件并将其添加到您的application.js。并添加这段代码 进入文件。

<%
@translator = I18n.backend
@translator.load_translations
@translations ||= @translator.send(:translations)[I18n.locale][:javascript]
%>

window.I18n = <%= @translations.to_json.html_safe %>

我的翻译范围也没有庞大的javascript文件。我的范围是:javascript。

希望它有所帮助!

答案 4 :(得分:5)

为什么不在您的Javascript文件中简单地这样做:

var a_message = "<%= I18n.t 'my_key' %>"

为此,您必须将.erb添加到Javascript文件的扩展名。

如果您不使用ruby&gt; = 2.0,您可能还需要在Javascript文件的顶部添加以下行。

<%# encoding: utf-8 %>

有关详细信息,请参阅此主题中已接受答案的最后评论:Encoding issues in javascript files using rails asset pipeline

答案 5 :(得分:2)

Babilu是一个Rails插件,可以帮到你。

答案 6 :(得分:2)

另一个可能有用的选项:

假设您有一个包含所有可用语言的模型语言(slug)。 它可以处理缺少翻译的情况(它被默认的语言环境版本替换)

资产/ JavaScript的/ i18n.js.erb

<%
@translator = I18n.backend
@translator.load_translations

translations = {}
Language.all.each do |l|
    translations[l.slug] = @translator.send(:translations)[l.slug.to_sym]
end

@translations = translations

%>
window.I18n = <%= @translations.to_json.html_safe %>

window.I18n.t = function(key){
    if(window.I18n[current_locale]){
        el = eval("I18n['"+current_locale+"']." + key);
    }
    if(window.I18n[default_locale] && typeof(el) == 'undefined'){
        el = eval("I18n['"+default_locale+"']." + key);
    }
    if(typeof(el) == 'undefined'){
        el = key;
    }
    return el;
};

的观点/布局/ application.html.erb

...
<%= javascript_tag "var current_locale = '#{I18n.locale.to_s}';" %>
<%= javascript_tag "var default_locale = '#{I18n.default_locale}';" %>
...

在您的javascript代码中,您可以这样翻译:

// current_locale:fr , default_locale:en

// existing translation (in french) 
I18n.t('message.hello_world'); // => Bonjour le monde

// non-existing translation (in french) but existing in english 
I18n.t('message.hello_this_world'); // => Hello this world

// non-existing translation (french & english) 
I18n.t('message.hello_this_new_world'); // => message.hello_this_new_world

希望它有所帮助!

答案 7 :(得分:1)

瑞安解决方案很棒。 但要不包含整个文件,您应该使用:@translations[I18n.locale].with_indifferent_access["alpha"] 而不是I18n.backend.send(:translations)["alpha"]

答案 8 :(得分:0)

I18n-js对我很有用,我推荐它。如果您使用他的rewrite branch,那么该插件将包含一个/assets/i18n/filtered.js文件,该文件可以输出@ ryan-montgomery所回答的内容,而无需手动执行任何操作。

这样,您可以在后端使用相同的翻译 使用Rails助手t(:key)并在前端的Javascript中使用I18n.t('key')

答案 9 :(得分:0)

对于您所描述的应用程序和#34;根本不支持国际化的应用程序&#34;并且&#34;为时已晚,无法立即解决问题&#34;我写了一个非常快速的方法:jQuery插件Quick-i18n:https://github.com/katio/Quick-i18n演示(以及如何使用它):http://johannpaul.net/Quick-i18n/