0

I have an application with many languages and now I want to integrate in the URL the language. Something like this: https://example.com/en/main/cars https://example.com/fr/main/cars and so on. The application works perfectly for every language, only that the language is missing in the URL.

I checked already this and this but I did not have success. I will appreciate any help.

In helpers.rb I have:

def set_locale( new_locale )
  allowed_locales = %w[ de en fr it sk ]
  if allowed_locales.map{|l| l.to_sym }.include?( new_locale.to_sym )
    session[:locale] = new_locale.to_sym
  else
    session[:locale] = default_locale
  end
end
alias_method :set_lang, :set_locale

def locale
  session[:locale] || default_locale
end

def default_locale
  :de
end

def t( *args )
  I18n.locale = locale
  I18n.t( *args )
end

def l( *args )
  I18n.locale = locale
  I18n.l( *args )
end

In app.rb I define something like this:

Class Something < Sinatra::Application
  helpers Sinatra::Something::Helpers
  I18n.load_path += Dir[File.join(settings.root, 'locales', '??.yml').to_s]

  before "/*" do
    set_locale params[:locale] if params[:locale]
    set_locale params[:lang]   if params[:lang]
    set_locale params[:l]      if params[:l]
  end
end
Community
  • 1
  • 1
karlihnos
  • 415
  • 1
  • 7
  • 22

1 Answers1

0

Given the code you showed, you are nowhere setting the first part of your url as the language param. Try to add the before-filter from the doc you linked:

before '/:locale/*' do
    I18n.locale       =       params[:locale]
    request.path_info = '/' + params[:splat ][0]
end

Alternatively, you can edit your routes to include and name this parameter.

onli
  • 183
  • 12