【问题标题】:Rails i18n different locales for front-end, back-endRails i18n 前端、后端的不同语言环境
【发布时间】:2015-03-31 23:57:08
【问题描述】:

我正在尝试开发具有后端和前端的商务解决方案。我刚刚完成了由名称空间分隔的后端,并意识到我需要为每一侧分隔不同的语言环境。那么有没有解决方案如何分别为前端和后端设置语言环境?谢谢建议

【问题讨论】:

    标签: ruby-on-rails localization namespaces internationalization e-commerce


    【解决方案1】:

    http://guides.rubyonrails.org/i18n.html#setting-and-passing-a-locale

    区域设置可以伪全局设置为I18n.locale(使用Thread.current,例如Time.zone),也可以作为选项传递给#translate#localize

    如果没有传递语言环境,则使用I18n.locale

    I18n.locale = :de
    I18n.t :foo
    I18n.l Time.now
    

    显式传递语言环境:

    I18n.t :foo, locale: :de
    I18n.l Time.now, locale: :de
    

    I18n.locale 默认为 I18n.default_locale,默认为 :en。默认语言环境可以这样设置:I18n.default_locale = :de

    因此,来自现实生活的示例(语言环境,基于 Accept-Language HTTP-header (https://github.com/iain/http_accept_language)):

    class ApplicationController < ActionController::Base
      #...
      before_filter :set_locale
    
      def set_locale
        I18n.locale =     http_accept_language.compatible_language_from(I18n.available_locales)
      end
    
    end
    

    【讨论】:

    • 我不确定你是否理解我的意思。我的问题是我在管理模块中设置了语言环境,然后我在前端模块中工作,并且会有无效语言环境的消息,因为我在前端和后端有不同的语言环境。我在应用程序控制器中为前端设置语言环境,在 admin::application 控制器中为后端设置语言环境。
    • 您可以在每个请求范围 (I18n.locale) 或每个方法调用 (I18n.t :foo, locale: :&lt;locale&gt;) 的任何位置设置区域设置的文档点。您在不同的控制器中设置了不同的语言环境。任何问题? :) 我只想说,如果您设置了错误的语言环境并看到错误 - 那么您的问题不是您看到错误,而是您设置了错误的语言环境。
    猜你喜欢
    • 1970-01-01
    • 2011-02-28
    • 2019-04-23
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多