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 / en /:

get '/attivita/:activity' do |activity|
  erb "attivita/#{activity.to_sym}".to_sym
end

Should I harm routes with: locale anywhere? Thanks

+3
source share
1 answer

Your unit beforegives me wrong number of arguments (2 for 1)on Sinatra 1.1.3.

With beforefrom How to determine the language from a URL in Sinatra works for me:

before do
  @locale, request.path_info = $1, $2 if request.path_info =~ settings.locale_pattern
end
+2
source

Source: https://habr.com/ru/post/1793896/


All Articles