34

I am trying to use I18n.transliterate to "normalize" some text with accented characters so I can analyze and compare it with different languages.

However, when using this method the following error pops out

I18n::InvalidLocale: :en is not a valid locale
    from /home/xxx/.rvm/gems/ruby-2.1.5/gems/i18n-0.7.0/lib/i18n.rb:284:in `enforce_available_locales!'
    from /home/xxx/.rvm/gems/ruby-2.1.5/gems/i18n-0.7.0/lib/i18n.rb:235:in `transliterate'
    from (irb):2
    from /home/xxx/.rvm/rubies/ruby-2.1.5/bin/irb:11:in `<main>'

I have made some searches for this problem and I only found solutions related to Rails configuration. I am just using this in a Ruby file, nothing related to Rails, so... What can I do?

dabadaba
  • 9,064
  • 21
  • 85
  • 155

1 Answers1

67

I seem to have a similar problem which I resolved by inspecting the source code associated with the error.

I found I have to explicitly set the available locales like so:

I18n.config.available_locales = :en

I'm not sure if this is the correct approach, I see also that the following achieves the same effect and is likely more robust.

Sam Joseph
  • 4,584
  • 4
  • 31
  • 47
  • 1
    Where? what file? – Arnold Roa May 08 '17 at 17:31
  • @ArnoldRoa just any file in `config/initializers` folder, you can use the `text_resources.rb` – bigsolom May 10 '17 at 07:45
  • 1
    `config.i18n.available_locales = :en` worked for me on Rails 5.1 – Santiago Martí Olbrich Mar 21 '18 at 21:10
  • Note that this is only true if you have "config.i18n.enforce_available_locales = true" defined in application.rb (at least in my configuration that was the case) – Nathan Crause May 14 '18 at 22:36
  • 1
    If you get this error while using `number_to` helpers, it's important that you have a corresponding `.yml` file in the `config/locales` folder for Rails to autoload the locale. For example, to use `locale: :de`, make sure you have a `de.yml` file there. – slhck Nov 06 '18 at 09:23