Last active
May 20, 2019 10:44
-
-
Save inouire/508792b27f1a06982683821b59f683c3 to your computer and use it in GitHub Desktop.
Simple i18n setup in my Hanami 1.3 app
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
# config/initializers/i18n.rb | |
require 'i18n' | |
# Configure it as you want | |
I18n.load_path = Dir["config/locales/**.yml"] | |
I18n.enforce_available_locales = true | |
I18n.default_locale = "en" | |
I18n.backend.load_translations | |
# Use it the way you want | |
def t(key, default = nil) | |
# The real one is a bit more complicated but you get the idea | |
I18n.t(key) || default || key | |
end | |
# You can now t('my.locale.key') anywhere in the code to get the translation. | |
# Don't forget to set the locale somewhere in the request, e.g: I18n.locale = "fr" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment