【问题标题】:rails javascript i18n specific yml using i18n-js plugin使用 i18n-js 插件的 rails javascript i18n 特定 yml
【发布时间】:2014-03-18 15:10:04
【问题描述】:

我的问题是是否有配置 i18n-js 以便只有一个特定的 .yml 文件被 javascript 查找,但不是全部。我已经按照下面链接中的示例并让 javascript 查找正确的键;但是,当我查看此插件在检查元素中生成的 translation.js 时,所有的翻译键都会显示出来。我们担心随着我们的应用程序的增长这将成为一个问题,因为 javascript 只需要知道它需要的翻译密钥。我在想只有一个 config/locales/en/javascript.yml 把所有的 javascript 翻译都放在这里。

按照示例进行

https://github.com/fnando/i18n-js

http://blog.10to1.be/rails/2011/03/22/localizing-javascript-in-your-rails-app/

rake i18n:js:setup

应用程序.js

//= require i18n
//= require i18n/translations

application.html.haml

 = javascript_include_tag 'translation'

目录

|config
|-locales
|--en
|---en.yml
|---javascript.yml   <------ that's what I want and the js only look at this one2

【问题讨论】:

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


    【解决方案1】:

    根据文档,您可以指定一个特定的键。我在更新 translations.js 文件时遇到问题,所以我无法验证这是否真的有效,但文档说明了这一点:

    在 config/i18n-js.yml 中

    translations:
    - file: "app/assets/javascripts/application/i18n/translations.js"
      only: '*.js*'
    

    en.yml 内

      js:
        posts:
          select2:
            placeholder: 'Please, select tags'
            no-matches: 'No tags found'
    

    【讨论】:

      【解决方案2】:

      目前没有办法通过简单的配置来做到这一点。

      i18n-js 与任何 YML 翻译文件或翻译文件夹无关(例如 Rails 的默认 config/locales)。它只是要求i18n gem(Rails 附带)检索所有可用的翻译,然后将它们导出为 JS 格式。

      但是,您可以定义一个自定义的 rails 环境,比如 js_i18n,在其中覆盖默认的 i18n 加载路径:

      # config/environments/js_i18n.rb
      # code similar to the rest of your config files - you could copy `development.rb`
      # ...
      config.i18n.load_path = Dir[Rails.root.join('config', 'locales', 'js', '*.{rb,yml}').to_s]
      

      然后使用这个环境,它只会加载config/locales/js 下的 YML/Ruby 文件来运行你喜欢的任何任务,例如:

      RAILS_ENV=js_i18n rake i18n:js:export
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-10
        • 2011-03-31
        • 2013-06-08
        • 1970-01-01
        • 2013-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多