Module: I18n

Extended by:
Base
Defined in:
lib/i18n/backend/cache.rb,
lib/i18n.rb,
lib/i18n/tests.rb,
lib/i18n/utils.rb,
lib/i18n/config.rb,
lib/i18n/locale.rb,
lib/i18n/backend.rb,
lib/i18n/gettext.rb,
lib/i18n/version.rb,
lib/i18n/exceptions.rb,
lib/i18n/locale/tag.rb,
lib/i18n/middleware.rb,
lib/i18n/tests/link.rb,
lib/i18n/tests/procs.rb,
lib/i18n/backend/base.rb,
lib/i18n/tests/basics.rb,
lib/i18n/tests/lookup.rb,
lib/i18n/backend/chain.rb,
lib/i18n/backend/simple.rb,
lib/i18n/tests/defaults.rb,
lib/i18n/backend/cascade.rb,
lib/i18n/backend/flatten.rb,
lib/i18n/backend/gettext.rb,
lib/i18n/backend/memoize.rb,
lib/i18n/gettext/helpers.rb,
lib/i18n/backend/metadata.rb,
lib/i18n/interpolate/ruby.rb,
lib/i18n/locale/fallbacks.rb,
lib/i18n/backend/fallbacks.rb,
lib/i18n/backend/key_value.rb,
lib/i18n/locale/tag/simple.rb,
lib/i18n/backend/cache_file.rb,
lib/i18n/locale/tag/parents.rb,
lib/i18n/locale/tag/rfc4646.rb,
lib/i18n/tests/localization.rb,
lib/i18n/tests/interpolation.rb,
lib/i18n/tests/pluralization.rb,
lib/i18n/backend/lazy_loadable.rb,
lib/i18n/backend/pluralization.rb,
lib/i18n/backend/transliterator.rb,
lib/i18n/tests/localization/date.rb,
lib/i18n/tests/localization/time.rb,
lib/i18n/tests/localization/procs.rb,
lib/i18n/tests/localization/date_time.rb,
lib/i18n/backend/interpolation_compiler.rb

Overview

The InterpolationCompiler module contains optimizations that can tremendously speed up the interpolation process on the Simple backend.

It works by defining a pre-compiled method on stored translation Strings that already bring all the knowledge about contained interpolation variables etc. so that the actual recurring interpolation will be very fast.

To enable pre-compiled interpolations you can simply include the InterpolationCompiler module to the Simple backend:

I18n::Backend::Simple.include(I18n::Backend::InterpolationCompiler)

Note that InterpolationCompiler does not yield meaningful results and consequently should not be used with Ruby 1.9 (YARV) but improves performance everywhere else (jRuby, Rubinius).

Defined Under Namespace

Modules: Backend, Base, Gettext, Locale, Tests, Utils Classes: ArgumentError, Config, Disabled, ExceptionHandler, InvalidFilenames, InvalidLocale, InvalidLocaleData, InvalidPluralizationData, Middleware, MissingInterpolationArgument, MissingTranslation, MissingTranslationData, ReservedInterpolationKey, UnknownFileType, UnsupportedMethod

Constant Summary collapse

RESERVED_KEYS =
%i[
  cascade
  deep_interpolation
  default
  exception_handler
  fallback
  fallback_in_progress
  fallback_original_locale
  format
  object
  raise
  resolve
  scope
  separator
  throw
]
EMPTY_HASH =
{}.freeze
VERSION =
"1.14.2"
DEFAULT_INTERPOLATION_PATTERNS =
[
  /%%/,
  /%\{([\w|]+)\}/,                            # matches placeholders like "%{foo} or %{foo|word}"
  /%<(\w+)>([^\d]*?\d*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
].freeze
INTERPOLATION_PATTERN =
Regexp.union(DEFAULT_INTERPOLATION_PATTERNS)
@@cache_store =
nil
@@cache_namespace =
nil
@@cache_key_digest =
nil
@@fallbacks =
nil

Class Method Summary collapse

Methods included from Base

available_locales_initialized?, config, config=, eager_load!, enforce_available_locales!, exists?, locale_available?, localize, normalize_keys, reload!, translate, translate!, transliterate, with_locale

Class Method Details

.cache_key_digestObject



64
65
66
# File 'lib/i18n/backend/cache.rb', line 64

def cache_key_digest
  @@cache_key_digest
end

.cache_key_digest=(key_digest) ⇒ Object



68
69
70
# File 'lib/i18n/backend/cache.rb', line 68

def cache_key_digest=(key_digest)
  @@cache_key_digest = key_digest
end

.cache_namespaceObject



56
57
58
# File 'lib/i18n/backend/cache.rb', line 56

def cache_namespace
  @@cache_namespace
end

.cache_namespace=(namespace) ⇒ Object



60
61
62
# File 'lib/i18n/backend/cache.rb', line 60

def cache_namespace=(namespace)
  @@cache_namespace = namespace
end

.cache_storeObject



48
49
50
# File 'lib/i18n/backend/cache.rb', line 48

def cache_store
  @@cache_store
end

.cache_store=(store) ⇒ Object



52
53
54
# File 'lib/i18n/backend/cache.rb', line 52

def cache_store=(store)
  @@cache_store = store
end

.fallbacksObject

Returns the current fallbacks implementation. Defaults to I18n::Locale::Fallbacks.



17
18
19
20
# File 'lib/i18n/backend/fallbacks.rb', line 17

def fallbacks
  @@fallbacks ||= I18n::Locale::Fallbacks.new
  Thread.current[:i18n_fallbacks] || @@fallbacks
end

.fallbacks=(fallbacks) ⇒ Object

Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.



23
24
25
26
# File 'lib/i18n/backend/fallbacks.rb', line 23

def fallbacks=(fallbacks)
  @@fallbacks = fallbacks.is_a?(Array) ? I18n::Locale::Fallbacks.new(fallbacks) : fallbacks
  Thread.current[:i18n_fallbacks] = @@fallbacks
end

.interpolate(string, values) ⇒ Object

Return String or raises MissingInterpolationArgument exception. Missing argument’s logic is handled by I18n.config.missing_interpolation_argument_handler.



23
24
25
26
27
# File 'lib/i18n/interpolate/ruby.rb', line 23

def interpolate(string, values)
  raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ I18n.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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n/interpolate/ruby.rb', line 29

def interpolate_hash(string, values)
  pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns]
  interpolated = false

  interpolated_string = string.gsub(pattern) do |match|
    interpolated = true

    if match == '%%'
      '%'
    else
      key = ($1 || $2 || match.tr("%{}", "")).to_sym
      value = if values.key?(key)
                values[key]
              else
                config.missing_interpolation_argument_handler.call(key, values, string)
              end
      value = value.call(values) if value.respond_to?(:call)
      $3 ? sprintf("%#{$3}", value) : value
    end
  end

  interpolated ? interpolated_string : string
end

.new_double_nested_cacheObject

:nodoc:



37
38
39
# File 'lib/i18n.rb', line 37

def self.new_double_nested_cache # :nodoc:
  Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
end

.perform_caching?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/i18n/backend/cache.rb', line 72

def perform_caching?
  !cache_store.nil?
end

.reserve_key(key) ⇒ Object

Marks a key as reserved. Reserved keys are used internally, and can’t also be used for interpolation. If you are using any extra keys as I18n options, you should call I18n.reserve_key before any I18n.translate (etc) calls are made.



45
46
47
48
# File 'lib/i18n.rb', line 45

def self.reserve_key(key)
  RESERVED_KEYS << key.to_sym
  @reserved_keys_pattern = nil
end

.reserved_keys_patternObject

:nodoc:



50
51
52
# File 'lib/i18n.rb', line 50

def self.reserved_keys_pattern # :nodoc:
  @reserved_keys_pattern ||= /%\{(#{RESERVED_KEYS.join("|")})\}/
end