Module: I18n

Defined in:
lib/i18n/backend/cache.rb,
lib/i18n.rb,
lib/i18n/tests.rb,
lib/i18n/locale.rb,
lib/i18n/config.rb,
lib/i18n/version.rb,
lib/i18n/gettext.rb,
lib/i18n/backend.rb,
lib/i18n/exceptions.rb,
lib/i18n/locale/tag.rb,
lib/i18n/tests/link.rb,
lib/i18n/tests/procs.rb,
lib/i18n/tests/lookup.rb,
lib/i18n/backend/base.rb,
lib/i18n/tests/basics.rb,
lib/i18n/backend/chain.rb,
lib/i18n/tests/defaults.rb,
lib/i18n/backend/simple.rb,
lib/i18n/backend/flatten.rb,
lib/i18n/backend/memoize.rb,
lib/i18n/backend/gettext.rb,
lib/i18n/gettext/helpers.rb,
lib/i18n/backend/cascade.rb,
lib/i18n/locale/fallbacks.rb,
lib/i18n/interpolate/ruby.rb,
lib/i18n/backend/metadata.rb,
lib/i18n/backend/key_value.rb,
lib/i18n/locale/tag/simple.rb,
lib/i18n/backend/fallbacks.rb,
lib/i18n/locale/tag/parents.rb,
lib/i18n/tests/localization.rb,
lib/i18n/locale/tag/rfc4646.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, Gettext, Locale, Tests Classes: ArgumentError, Config, ExceptionHandler, InvalidLocale, InvalidLocaleData, InvalidPluralizationData, MissingInterpolationArgument, MissingTranslation, MissingTranslationData, ReservedInterpolationKey, UnknownFileType

Constant Summary

RESERVED_KEYS =
[:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise]
RESERVED_KEYS_PATTERN =
/%\{(#{RESERVED_KEYS.join("|")})\}/
VERSION =
"0.7.0.dev"
INTERPOLATION_PATTERN =
Regexp.union(
  /%%/,
  /%\{(\w+)\}/,                               # matches placeholders like "%{foo}"
  /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
)
@@cache_store =
nil
@@cache_namespace =
nil
@@fallbacks =
nil

Class Method Summary (collapse)

Class Method Details

+ (Object) cache_namespace



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

def cache_namespace
  @@cache_namespace
end

+ (Object) cache_namespace=(namespace)



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

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

+ (Object) cache_store



41
42
43
# File 'lib/i18n/backend/cache.rb', line 41

def cache_store
  @@cache_store
end

+ (Object) cache_store=(store)



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

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

+ (Object) fallbacks

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

+ (Object) fallbacks=(fallbacks)

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

+ (Object) interpolate(string, values)

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

+ (Object) interpolate_hash(string, values)



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).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

+ (Boolean) perform_caching?



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

def perform_caching?
  !cache_store.nil?
end