Skip to content

Instantly share code, notes, and snippets.

@inouire
Last active May 20, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inouire/508792b27f1a06982683821b59f683c3 to your computer and use it in GitHub Desktop.
Save inouire/508792b27f1a06982683821b59f683c3 to your computer and use it in GitHub Desktop.
Simple i18n setup in my Hanami 1.3 app
# 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