Created
January 21, 2014 23:39
-
-
Save JonathonMA/8550796 to your computer and use it in GitHub Desktop.
Rails I18n fallback experiment
This file contains hidden or 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
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 |
Author
JonathonMA
commented
Jan 21, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment