使用i18n-js插件的Rails javascript

rails javascript i18n specific yml using i18n-js plugin

本文关键字:Rails javascript 插件 i18n-js 使用      更新时间:2023-09-26

我的问题是,如果有任何方式配置i18n-js,以便只有一个特定的.yml文件正在由javascript查找,但不是全部。我遵循了下面链接中的示例,并让javascript查找正确的键;然而,当我查看这个插件在inspect元素中生成的translate .js时,所有的翻译键都显示出来了。我们担心随着应用的发展,这将成为一个问题,因为javascript只需要知道它需要的翻译键。我想只是有一个config/locale/en/javascript。把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

application.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

根据文档可以指定一个特定的键。我有问题得到翻译。js文件更新,所以我不能验证这实际上是有效的,但文档说它:

内部配置/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'

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

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