【问题标题】:Sinatra I18n Fallbacks using Rack::Locale使用 Rack::Locale 的 Sinatra I18n 回退
【发布时间】:2017-04-26 13:54:46
【问题描述】:

我正在尝试使用 I18n 设置一个简单的 Sinatra 应用程序,遵循 the recommended Sinatra recipe,并使用 Rack:Locale 来确定语言。

我的 app.rb:

require 'rubygems'
require 'sinatra'
require 'rack/contrib'
require 'i18n'
require 'i18n/backend/fallbacks'
require 'tilt/haml'

use Rack::Locale

configure do
  I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
  I18n.load_path = Dir[File.join(settings.root, 'locales', '*.yml')]
  I18n.backend.load_translations
end

helpers do
  def t(*args)
    I18n.t(*args)
  end
end

get '/' do
  haml :index
end

我的语言环境/en.yml:

en:
  welcome: "Welcome!"

当我运行 rackup 并访问我的 Sinatra 应用程序的根路径时,我得到以下信息:

I18n::InvalidLocale at /
"en-US" is not a valid locale
file: i18n.rb location: enforce_available_locales! line: 284

我认为I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) 会处理这个问题,方法是找不到en-US 并退回到en(我有),但显然不是。我错过了什么?

【问题讨论】:

    标签: ruby internationalization sinatra i18n-gem


    【解决方案1】:

    添加:

    I18n.enforce_available_locales = false
    

    【讨论】:

      猜你喜欢
      • 2011-01-15
      • 2011-12-03
      • 2010-10-31
      • 1970-01-01
      • 2013-08-05
      • 2011-03-21
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多