Question: Sinatra I18n Fallbacks using Rack::Locale

Question

Sinatra I18n Fallbacks using Rack::Locale

Answers 1
Added at 2016-12-11 17:12
Tags
Question

I'm trying to set up a simple Sinatra app with I18n, following the recommended Sinatra recipe, and using Rack:Locale to determine the language.

My 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

My locales/en.yml:

en:
  welcome: "Welcome!"

When I run rackup and visit the root path of my Sinatra app, I get the following:

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

I thought that the I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) would handle this, by not finding en-US and falling back to en (which I have), but apparently not. What am I missing?

Answers to

Sinatra I18n Fallbacks using Rack::Locale

nr: #1 dodano: 2016-12-13 14:12

Add:

I18n.enforce_available_locales = false
Source Show
◀ Wstecz