Last active
December 16, 2015 17:19
-
-
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.
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
en-US: | |
welcome: "Welcome to %{app_name}!" | |
goodbye: "See ya later, %{name}. Come back to %{app_name} soon!" |
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
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!" |
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
# 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