Skip to content

Instantly share code, notes, and snippets.

@sunny
Created October 5, 2020 08:07
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 sunny/fd4d5fa2851e4e8cd3892291889252d1 to your computer and use it in GitHub Desktop.
Save sunny/fd4d5fa2851e4e8cd3892291889252d1 to your computer and use it in GitHub Desktop.
Show i18n keys when locale is "translate"
# config/initializers/i18n.rb
I18n::Backend::Simple.include(I18n::Backend::TranslateLocale)
# 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