Mukesh
Mukesh

Reputation: 1267

Getting I18n::InvalidLocale (:en is not a valid locale): on rails ajax request

When i load page normally it doesn't show any locale error but when i do ajax request I get

I18n::InvalidLocale (:en is not a valid locale):
  i18n (0.7.0) lib/i18n.rb:284:in `enforce_available_locales!'
  i18n (0.7.0) lib/i18n.rb:151:in `translate'
  i18n (0.7.0) lib/i18n.rb:168:in `translate!'

// ajax request

$.ajax({
  url: "/unlock_company",
  type: "GET",
  dataType: 'json',
  success: function(data) {
    console.log(data);
  }
});

In the controller

def unlock_company
 respond_to do |format|
  format.json {render json: { :result => '@result'}}
 end
end

In application.rb file

config.i18n.enforce_available_locales = true
I18n.config.enforce_available_locales = true
config.i18n.available_locales = [:"en-US"]

Upvotes: 4

Views: 1687

Answers (3)

Goz
Goz

Reputation: 179

I've the same problem, i add these lines to my application.rb file, and it solved the problem :

config.i18n.fallbacks = true
config.i18n.enforce_available_locales = false

Upvotes: 2

Elvn
Elvn

Reputation: 3057

I'm not sure if this is a solution, but it may be worth investigating. Simple test. You don't mention which Rails version you're using, but as of Rails 4-ish these are deprecated:

config.i18n.enforce_available_locales = true
I18n.config.enforce_available_locales = true

In the past I18n had a bug that produced the same error you're seeing. The bug/error was triggered by using .enforce_available_locales. More here.

Since .enforce_available_locales defaults to true, you should remove it from application.rb, and as a side effect it may eliminate the error.

Upvotes: 1

EugZol
EugZol

Reputation: 6555

Try using String instead of symbol for long locale name:

config.i18n.available_locales = ["en-US"]

Upvotes: 1

Related Questions