Module: I18n

Extended by:
Base
Defined in:
lib/i18n/backend/cache.rb,
lib/i18n.rb,
lib/i18n/tests.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/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/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 Classes: ArgumentError, Config, ExceptionHandler, InvalidLocale, InvalidLocaleData, InvalidPluralizationData, Middleware, MissingInterpolationArgument, MissingTranslation, MissingTranslationData, ReservedInterpolationKey, UnknownFileType

Constant Summary collapse

RESERVED_KEYS =
%i[
  cascade
  deep_interpolation
  default
  exception_handler
  fallback
  fallback_in_progress
  format
  object
  raise
  resolve
  scope
  separator
  throw
].freeze
RESERVED_KEYS_PATTERN =
/%\{(#{RESERVED_KEYS.join("|")})\}/
EMPTY_HASH =
{}.freeze
VERSION =
"1.1.0"
INTERPOLATION_PATTERN =
Regexp.union(
  /%%/,
  /%\{(\w+)\}/,                               # matches placeholders like "%{foo}"
  /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
)
@@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=, 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
# File 'lib/i18n/backend/fallbacks.rb', line 17

def fallbacks
  @@fallbacks ||= I18n::Locale::Fallbacks.new
end

.fallbacks=(fallbacks) ⇒ Object

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



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

def fallbacks=(fallbacks)
  @@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.



14
15
16
17
18
# File 'lib/i18n/interpolate/ruby.rb', line 14

def interpolate(string, values)
  raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ 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/i18n/interpolate/ruby.rb', line 20

def interpolate_hash(string, values)
  string.gsub(INTERPOLATION_PATTERN) do |match|
    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
end

.new_double_nested_cacheObject

:nodoc:



35
36
37
# File 'lib/i18n.rb', line 35

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