Sinatra routes and i18n
set :locales, %w[en it]
set :default_locale, 'it'
set :locale_pattern, /^\/?(#{Regexp.union(settings.locales)})(\/.+)$/
helpers do
def locale
@locale || settings.default_locale
end
end
before('/:locale/*') { |params| @locale = params.first } # params shouldn't be a Hash?
I cannot get other pages starting with a /en/:
get '/attivita/:activity' do |activity|
erb "attivita/#{activity.to_sym}".to_sym
end
Should I pest routes with :loca开发者_运维问答le anywhere? Thanks
Your before
block gives me a wrong number of arguments (2 for 1)
on Sinatra 1.1.3
.
With the before
from How to detect language from URL in Sinatra works for me:
before do
@locale, request.path_info = $1, $2 if request.path_info =~ settings.locale_pattern
end
精彩评论