I18n adding "-US" to the en locale

898 views Asked by At

I'm trying to persist the URLs with the specific language (i.e. en, pt, etc.) but after setting it up, I keep getting 'en-US' instead of 'en'. (i.e. localhost:3000/en-us/apps instead of localhost:3000/en/apps). Not sure where I can change this setting since I figured it would default to my fallbacks.

I've prefixed my routes with the following:

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do

    root :to => "home#index"

    get 'omniauth/:provider' => 'omniauth#localized', as: :localized_omniauth

    devise_for :users, skip: :omniauth_callbacks, :controllers => { :registrations => "registrations", :sessions => "sessions", :passwords => "passwords" }

    resources :submissions.....

ApplicationController.rb

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

def set_locale
    I18n.locale = params[:locale] ||
                  request.env['rack.locale'] ||
                  I18n.default_locale
end

Application.rb

config.i18n.default_locale = :en
config.i18n.available_locales = [:en, :pt]
config.i18n.fallbacks = true
1

There are 1 answers

5
blelump On

You're missing setting I18n.locale:

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

Detect and set locale for current request, depending on your input data:

before_filter :set_locale

def set_locale
  if defined?(params) && params[:locale]
    I18n.locale = params[:locale]
  elsif current_user && current_user.language_id.present?
    I18n.locale = current_user.language.code
  end
  I18n.locale ||= I18n.default_locale
end