0

I have added localization in my website and it is working fine, except two points

  1. When I open website for example www.test.help.com it shows this url instead it should also show locale as I am using path_prefix in my application controller.
  2. When I click to change the language,the url is not changing instantly

here is my code snippet

application.rb

    include HttpAcceptLanguage::AutoLocale
    before_action :set_locale

   def default_url_options(options = {})
{ :path_prefix => I18n.locale }

 end

def set_locale
if cookies[:educator_locale] && I18n.available_locales.include?(cookies[:educator_locale].to_sym)
  l = cookies[:educator_locale].to_sym
else
  if params[:path_prefix].present?
    l = params[:path_present]
    cookies.permanent[:educator_locale] = l
  else
    if (http_accept_language.preferred_language_from(http_accept_language.user_preferred_languages).include? "en")
       l = 'en'
       cookies.permanent[:educator_locale] = l
    end
    if ( http_accept_language.preferred_language_from(http_accept_language.user_preferred_languages).include? "fr")
      l = 'fr'
      cookies.permanent[:educator_locale] = l
    end
  end
  cookies.permanent[:educator_locale] = l
end
I18n.locale = l
end

My setting controller

 def change_locale
l = params[:locale].to_s.strip.to_sym
# puts "---------"                                                                                                                                                                                             
# puts l                                                                                                                                                                                                       
l = I18n.default_locale unless I18n.available_locales.include?(l)
cookies.permanent[:educator_locale] = l
url_hash = Rails.application.routes.recognize_path URI(request.referer).path    url_hash[:locale] =l                                                                                                                                                                                         
redirect_to url_hash

end

Then my routes

 get '/change_locale/:locale', to: 'settings#change_locale', as: :change_locale
 get "home/index"
   root 'home#index'

Once I click on any link, then path prefix is visible but not directly on opening the website.

nesiseka
  • 1,268
  • 8
  • 22
user2323
  • 49
  • 2
  • 11

1 Answers1

0

When you arrive on any url for the first on the application (root_url, users_url ...) the url is displayed in your web browser before you entrer in set_locale. Then if you visit an other link in the application the locale will be added in the url. But the translations will works anyway with the locale you setted in I18n.locale.

Ugo Mare
  • 442
  • 5
  • 15
  • Can you precise this one ? 'When I click to change the language,the url is not changing instantly' – Ugo Mare Jun 24 '15 at 10:01
  • when I click on changing the language from en to fr, url should instantly change from en to fr also, but it stays en even though text changes – user2323 Jun 24 '15 at 10:03
  • In `change_locale ` you don't set `I18n.locale` with the new selected locale. – Ugo Mare Jun 24 '15 at 10:20
  • thanks, it worked, but how or where should I handle root_url for first time to have locale in url – user2323 Jun 24 '15 at 11:31
  • Is it really a big deal to not have the locale in the url for the first time ? You can use some javascript if you really want to have the locale in the url the first time you visit the web site. – Ugo Mare Jun 24 '15 at 12:29