使用Rack :: Locale的Sinatra I18n后备版

时间:2016-12-11 16:25:28

标签: ruby internationalization sinatra i18n-gem

我尝试使用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

我的locales / 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(我有),但显然没有。我错过了什么?

1 个答案:

答案 0 :(得分:0)

添加:

I18n.enforce_available_locales = false
相关问题