Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Created January 21, 2014 23:39
Show Gist options
  • Save JonathonMA/8550796 to your computer and use it in GitHub Desktop.
Save JonathonMA/8550796 to your computer and use it in GitHub Desktop.
Rails I18n fallback experiment
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send :include, I18n::Backend::Fallbacks
I18n.backend.store_translations :en, test: {
no_translations: {
specific: "en specific",
generic: "en generic",
},
only_generic_translations: {
specific: "en specific",
generic: "en generic",
},
complete_translations: {
specific: "en specific",
generic: "en generic",
},
}
I18n.backend.store_translations :"en-AU", test: {
no_translations: {
},
only_generic_translations: {
generic: "au generic",
},
complete_translations: {
specific: "au specific",
generic: "au generic",
},
}
def text key
I18n.t :"test.#{key}.specific", default: [:"test.#{key}.generic", "string fallback"]
end
def check key
printf "%5s | %s\n", I18n.locale, text(key)
end
for test in %w[no_translations only_generic_translations complete_translations]
puts
puts "Test case #{test}"
puts "=" * 20
I18n.locale = :"en"
check test
I18n.locale = :"en-AU"
check test
end
@JonathonMA
Copy link
Author

Test case no_translations
====================
   en | en specific
en-AU | en specific

Test case only_generic_translations
====================
   en | en specific
en-AU | au generic

Test case complete_translations
====================
   en | en specific
en-AU | au specific

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