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/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, MissingInterpolationArgument, MissingTranslation, MissingTranslationData, ReservedInterpolationKey, UnknownFileType

Constant Summary collapse

RESERVED_KEYS =
[:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise, :deep_interpolation]
RESERVED_KEYS_PATTERN =
/%\{(#{RESERVED_KEYS.join("|")})\}/
VERSION =
"0.8.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

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

Class Method Details

.cache_key_digestObject



62
63
64
# File 'lib/i18n/backend/cache.rb', line 62

def cache_key_digest
  @@cache_key_digest
end

.cache_key_digest=(key_digest) ⇒ Object



66
67
68
# File 'lib/i18n/backend/cache.rb', line 66

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

.cache_namespaceObject



54
55
56
# File 'lib/i18n/backend/cache.rb', line 54

def cache_namespace
  @@cache_namespace
end

.cache_namespace=(namespace) ⇒ Object



58
59
60
# File 'lib/i18n/backend/cache.rb', line 58

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

.cache_storeObject



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

def cache_store
  @@cache_store
end

.cache_store=(store) ⇒ Object



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

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

.fallbacksObject

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



15
16
17
# File 'lib/i18n/backend/fallbacks.rb', line 15

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

.fallbacks=(fallbacks) ⇒ Object

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



20
21
22
# File 'lib/i18n/backend/fallbacks.rb', line 20

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

.perform_caching?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/i18n/backend/cache.rb', line 70

def perform_caching?
  !cache_store.nil?
end