在Javascript文件中提供yml翻译

Ste*_*anS 3 ruby-on-rails internationalization

我在我当前的Rails项目中使用jQuery,我想有一些方法可以在Javascript中使用我的yml文件中的翻译.

我知道我可以在我的.js.erb模板中轻松使用它们.但是/ public/javascript中的javascript文件怎么样?

看起来Babilu(http://github.com/toretore/babilu)会完全按照我的意愿行事.我只是想知道是否还有其他插件...这不是我对Babilu有什么东西,但我喜欢有选择;-)

也可能是Rails 2.3.5中有一些默认方式.我不知道,我可能根本不需要使用插件?

san*_*rom 5

控制器:

class JavascriptsController < ApplicationController

  skip_before_filter :authorize
  respond_to :js
  layout false
  caches_page :locale # don't know if this is reset on server restart.

  def locale

    # http://edgeguides.rubyonrails.org/caching_with_rails.html
    @translations = I18n.translate('javascripts')

  end

end
Run Code Online (Sandbox Code Playgroud)

查看脚本:

Translations = <%= raw @translations.to_json %>;
Run Code Online (Sandbox Code Playgroud)

并在application.html.haml中:

= javascript_include_tag "locale.js?language=#{I18n.locale}" # JavascriptsController
Run Code Online (Sandbox Code Playgroud)

然后添加一些简单的js来查找哈希中的键.相当容易修复.此外,这样您就不会将所有翻译传递给js,只是javascript层中实际使用的子集(不应该太多).