【问题标题】:Limiting no of locales during rails i18n-js在 rails i18n-js 期间限制语言环境的数量
【发布时间】:2019-04-11 16:36:36
【问题描述】:

我正在开发一个 ruby​​ on rails web 应用程序,我们在其中使用 rails I18njs gem 进行 js 翻译。在我的浏览器源代码中,我可以看到所有语言环境的 js 翻译键都已呈现。有没有办法将其限制为仅用户语言环境和后备语言环境。

宝石名称:i18n-js

【问题讨论】:

    标签: ruby-on-rails rails-i18n


    【解决方案1】:

    如果设置了I18n.available_locales(例如在您的 Rails config/application.rb 文件中),则只会导出指定的语言环境。

    将您的语言环境设置为:

    I18n.defaultLocale = "pt-BR";
    I18n.locale = "pt-BR";
    I18n.currentLocale();
    // pt-BR
    

    注意:您现在可以在加载 I18n 之前应用您的配置,如下所示:

    I18n = {} // You must define this object in top namespace, which should be `window`
    I18n.defaultLocale = "pt-BR";
    I18n.locale = "pt-BR";
    
    // Load I18n from `i18n.js`, `application.js` or whatever
    
    I18n.currentLocale();
    // pt-BR
    

    点击这里了解更多详情:https://github.com/fnando/i18n-js

    【讨论】:

      【解决方案2】:

      您需要从在单个 translations.js 文件中加载所有翻译的默认设置切换为 export configuration

      这是一个非常简单的配置,可以将每种语言导出到单独的语言环境文件中:

      config/i18n-js.yml

      # bake fallbacks into each locale file.
      fallbacks: true
      
      # split translations into separate files per-locale
      translations:
        - file: "public/javascripts/i18n/%{locale}.js"
        only: '*'
      

      然后你可以像这样从布局中只加载你需要的语言:

      app/views/layouts/application.html.erb

      <%= javascript_include_tag "i18n" %>
      <%= javascript_include_tag "i18n/#{I18n.locale}" %>
      

      【讨论】:

        猜你喜欢
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-23
        • 1970-01-01
        相关资源
        最近更新 更多