Skip to content

Instantly share code, notes, and snippets.

@philtr
Last active December 16, 2015 17:19
Show Gist options
  • Save philtr/5469864 to your computer and use it in GitHub Desktop.
Save philtr/5469864 to your computer and use it in GitHub Desktop.
Inject app name from settings to be available for all translations as an interpolation.
en-US:
welcome: "Welcome to %{app_name}!"
goodbye: "See ya later, %{name}. Come back to %{app_name} soon!"
Setting.set(:app_name, "Cool Web App")
I18n.t('welcome')
# => "Welcome to Cool Web App!"
I18n.t('goodbye', name: "Phillip")
# => "See ya later, Phillip. Come back to Cool Web App soon!"
# Override I18n::translate to pull in app_name from settings
module I18nAppNameInjector
def translate(*args)
options = args.extract_options!
options.reverse_merge!({ app_name: Setting.get(:app_name) })
super(*args, options)
end
alias_method :t, :translate
end
# Ruby 2.0 Module#prepend for the win!
module I18n
class << self
prepend I18nAppNameInjector
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment