我正在开发一个支持 i18n 的 rails 4 应用程序。我已经安装了 i18n gem。在我的 application.rb 文件中,我有这个代码
config.i18n.enforce_available_locales = true
config.i18n.available_locales = [:es]
config.i18n.default_locale = :es
在开发模式下,一切都完美无缺,但是当我切换到生产模式时,一切都输出“翻译缺失”。我尝试了不同的解决方案,例如在 config/initializers/locale.rb 中编写此代码
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}')]
I18n.default_locale = :es
并将其写入 config/environment.rb
I18n.reload!
没有任何效果。进入控制台模式会显示这一点,就像我在 Web 浏览器中打开应用程序时一样。
user@opendraft:/home/user/www/awesomeapp# RAILS_ENV=development bundle exec rails c
Loading development environment (Rails 4.0.1)
irb(main):001:0> I18n.t('btn_login')
=> "Acceder"
irb(main):002:0> exit
user@opendraft:/home/user/www/awesomeapp# RAILS_ENV=production bundle exec rails c
Loading production environment (Rails 4.0.1)
irb(main):001:0> I18n.t('btn_login')
=> "translation missing: es.btn_login"
irb(main):002:0> exit
我的 config/locales/es.yml 文件:
es:
btn_login: "Acceder"
reset: "Reinicializar"
login: "Acceso"
sign_up_candidate: "Candidatos"
sign_up_company: "Empresas"
sign_up_teacher: "Profesores"