ruby-on-rails – Rails 4中的Locale后备无法正常工作
我正在为新加坡,马来西亚,台湾和中国的客户建立一个Rails 4站点.
马来西亚华语的语言环境代码是zh-MY. 我想保留一组基本的zh-CN(简体中文)语言环境文件和zh-MY来回退到zh-CN. 因为zh-TW(繁体中文)是台湾使用的,所以只有一个zh是不正确的,并且它与zh-CN之间存在很大的差异. 所以这是我的config / application.rb文件as per the Rails Guide. require File.expand_path('../boot',__FILE__) require 'rails/all' require "i18n/backend/fallbacks" module MyAwesomeApp class Application < Rails::Application I18n::Backend::Simple.send(:include,I18n::Backend::Fallbacks) # all translations from config/locales/**/*.rb,yml are auto loaded. config.i18n.load_path += Dir[Rails.root.join('config','locales','**','*.{rb,yml}')] # The default locale is :en config.i18n.default_locale = :en # See http://guides.rubyonrails.org/i18n.html#localized-views for a discussion of # how language codes fall-back. config.i18n.available_locales = [:en,:'zh-CN',:'zh-TW',:'en-SG',:'en-MY',:'zh-MY'] I18n.fallbacks.map(:'zh-MY' => :'zh-CN') end end 但这根本行不通. 当我实际将语言环境设置为:zh-MY时,它不会回退到:zh-CN而是:en 我错过了什么? 更新: 更新 更新 [1] pry(#<ServicesController>)> I18n.locale => :"zh-MY" [2] pry(#<ServicesController>)> I18n.fallbacks => {:en=>[:en],:"zh-MY"=>[:"zh-MY",:zh,:en]} 因此,在启动Rails应用程序和调用控制器的set_locale方法之间,I18n.fallbacks将重置为默认值. 解决方法
不要问我为什么,但这是有效的,尽管官方文件说.
require File.expand_path('../boot',__FILE__) require 'rails/all' require "i18n/backend/fallbacks" Bundler.require(*Rails.groups) module MyAwesomeApp class Application < Rails::Application # all translations from config/locales/**/*.rb,yml}')] # The default locale is :en config.i18n.default_locale = :en # See http://guides.rubyonrails.org/i18n.html#localized-views for a # mostly correct discussion of how language codes fall-back. config.i18n.available_locales = [:en,:'zh-MY'] config.i18n.fallbacks = {:'zh-MY' => :'zh-CN'} end end 删除I18n :: Backend :: Simple.send(:include,I18n :: Backend :: Fallbacks)并通过config.i18n.fallbacks设置回退= {:’zh-MY’=> :’zh-CN’}而不是I18n.fallbacks.map(:’zh-MY’=>:’zh-CN’)使一切都完美无缺. 现在在我的控制器中,与第3个问题更新中讨论的断点相同: [1] pry(#<ServicesController>)> I18n.fallbacks => {:en=>[:en],:"zh-CN",:en]} 我希望这对其他人有帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |