Created
October 5, 2020 08:07
-
-
Save sunny/fd4d5fa2851e4e8cd3892291889252d1 to your computer and use it in GitHub Desktop.
Show i18n keys when locale is "translate"
This file contains 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 | |
I18n::Backend::Simple.include(I18n::Backend::TranslateLocale) |
This file contains 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
# lib/i18n/backend/translate_locale.rb | |
module I18n | |
module Backend | |
# Backend that prints the locale key if the current locale is "translate". | |
# | |
# Include it in your i18n backend in an initializer like so: | |
# | |
# I18n::Backend::Simple.include(I18n::Backend::TranslateLocale) | |
module TranslateLocale | |
IGNORED_KEYS = %w( | |
i18n.transliterate.rule | |
number.currency.format | |
number.format | |
number.human.decimal_units.units | |
number.human.format | |
number.percentage.format | |
number.precision.format | |
).freeze | |
def translate(locale, key, options = {}) | |
return super unless locale == :translate && | |
!options[:default].kind_of?(Hash) | |
full_key = [Array(options[:scope]), key].flatten.compact.join(".") | |
return super if IGNORED_KEYS.include?(full_key) | |
short_options = options.except( | |
:fallback, | |
:fallback_in_progress, | |
:default | |
) | |
if short_options.any? | |
strings = short_options.map { |k, v| "#{k}: #{v.inspect}" } | |
details = " (#{strings.join(', ')})" | |
end | |
"#{full_key}#{details}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment