Module: I18n
- Defined in:
- lib/patches/i18n/interpolate.rb,
lib/i18n/missing_translations_log_handler.rb,
lib/i18n/missing_translations_raise_handler.rb
Overview
A simple exception handler that behaves like the default exception handler but also raises on missing translations.
Useful for identifying missing translations during testing.
E.g.
require 'globalize/i18n/missing_translations_raise_handler'
I18n.exception_handler = :missing_translations_raise_handler
Constant Summary collapse
- INTERPOLATION_PATTERN =
Regexp.union( /%%/, /%\{(\w+)\}/, # matches placeholders like "%{foo}" /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d" )
- @@missing_translations_logger =
nil
Class Method Summary collapse
-
.interpolate(string, values) ⇒ Object
Return String or raises MissingInterpolationArgument exception.
- .interpolate_hash(string, values) ⇒ Object
- .missing_translations_log_handler(exception, locale, key, options) ⇒ Object
- .missing_translations_logger ⇒ Object
- .missing_translations_logger=(logger) ⇒ Object
- .missing_translations_raise_handler(exception, locale, key, options) ⇒ Object
Class Method Details
.interpolate(string, values) ⇒ Object
Return String or raises MissingInterpolationArgument exception. Missing argument’s logic is handled by I18n.config.missing_interpolation_argument_handler.
14 15 16 17 18 |
# File 'lib/patches/i18n/interpolate.rb', line 14 def interpolate(string, values) raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ Backend::Base::RESERVED_KEYS_PATTERN raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash) interpolate_hash(string, values) end |
.interpolate_hash(string, values) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/patches/i18n/interpolate.rb', line 20 def interpolate_hash(string, values) string.gsub(INTERPOLATION_PATTERN) do |match| if match == '%%' '%' else key = ($1 || $2).to_sym value = if values.key?(key) values[key] else raise(MissingInterpolationArgument.new(values, string)) end value = value.call(values) if value.respond_to?(:call) $3 ? sprintf("%#{$3}", value) : value end end end |
.missing_translations_log_handler(exception, locale, key, options) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/i18n/missing_translations_log_handler.rb', line 32 def missing_translations_log_handler(exception, locale, key, ) if MissingTranslationData === exception missing_translations_logger.warn(exception.) return exception. else raise exception end end |
.missing_translations_logger ⇒ Object
21 22 23 24 25 26 |
# File 'lib/i18n/missing_translations_log_handler.rb', line 21 def missing_translations_logger @@missing_translations_logger ||= begin require 'logger' unless defined?(Logger) Logger.new(STDOUT) end end |
.missing_translations_logger=(logger) ⇒ Object
28 29 30 |
# File 'lib/i18n/missing_translations_log_handler.rb', line 28 def missing_translations_logger=(logger) @@missing_translations_logger = logger end |
.missing_translations_raise_handler(exception, locale, key, options) ⇒ Object
12 13 14 |
# File 'lib/i18n/missing_translations_raise_handler.rb', line 12 def missing_translations_raise_handler(exception, locale, key, ) raise exception end |