dyserone dyserone - 4 months ago 20
Ruby Question

Rails, is there a way to assosciate a name with each locale?

I have 2 languages at the moment English and Greek (:en & :el).

I'm using the globalize gem.

Is there a way so I can put them in a loop and then extract the name of each language?

For example is there something like the following:

I18n.locale.Name_of_the_locale


If yes, where do I declare/change it?

Answer

One solution is to create a :language_name key inside your en.yml and gr.yml:

en:
  language_name: English

gr:
  language_name: Greek

Then simply call I18n.t('language_name'). If you need to have them all at once, you can still loop through all the available locales:

I18n.available_locales.map { |locale| I18n.t('language_name', locale: locale) }