Created
January 13, 2010 13:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/locales/en.yml | |
en: | |
i18n: | |
language: | |
name: 'English' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
+1 for the updated answer. thanks!
Exception I18n::MissingTranslationData not working
I'm getting "translation missing: en.i18n.language.name" instead of locale.to_s
@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
I stumbled accross this, trying to find a cleaner way other than a switch etc.
But alas, outdated it now throws an uncaught :exception.
This is now the correct way to do it I think. (works @ Rails 4.1.8)