Skip to content

Instantly share code, notes, and snippets.

@henrik
Created January 13, 2010 13:42
Show Gist options
  • Save henrik/276191 to your computer and use it in GitHub Desktop.
Save henrik/276191 to your computer and use it in GitHub Desktop.
Ruby on Rails i18n: storing locale names in their yml files and retrieving them.
# config/locales/en.yml
en:
i18n:
language:
name: 'English'
# config/initializers/i18n.rb
module I18n
def self.name_for_locale(locale)
I18n.backend.translate(locale, "i18n.language.name")
rescue I18n::MissingTranslationData
locale.to_s
end
end
# E.g.:
# I18n.name_for_locale(:en) # => "English"
@ritikesh
Copy link

+1 for the updated answer. thanks!

@mcmegavolt
Copy link

Exception I18n::MissingTranslationData not working
I'm getting "translation missing: en.i18n.language.name" instead of locale.to_s

@jitscar
Copy link

jitscar commented Jul 24, 2017

@mcmegavolt just replace your code to smth like this

self.with_locale(locale) { self.t('i18n.language.name', raise: true) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment