'I18n::InvalidLocale: :en is not a valid locale

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?



Solution 1:[1]

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.

Solution 2:[2]

At config/application.rb file, look at the phase I18n.available_locales, make sure it is defined similar to below:

I18n.available_locales = %i(en)

Anyway you can add more locale here.

I18n.available_locales = %i(en de)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam Joseph
Solution 2 Komsun K.