Module: I18n::Backend::Base

Includes:
Helpers
Included in:
ActiveRecord, Simple
Defined in:
lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb

Constant Summary collapse

RESERVED_KEYS =
[:scope, :default, :separator, :resolve]
INTERPOLATION_SYNTAX_PATTERN =
/(\\)?\{\{([^\}]+)\}\}/

Constants included from Helpers

Helpers::SEPARATOR_ESCAPE_CHAR

Instance Method Summary collapse

Methods included from Helpers

#deep_symbolize_keys, #escape_default_separator, #unescape_default_separator, #unwind_keys, #wind_keys

Instance Method Details

#available_localesObject

Returns an array of locales for which translations are available ignoring the reserved translation meta data key :i18n.



83
84
85
86
87
88
89
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 83

def available_locales
  init_translations unless initialized?
  translations.inject([]) do |locales, (locale, data)|
    locales << locale unless (data.keys - [:i18n]).empty?
    locales
  end
end

#initialized?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 77

def initialized?
  @initialized ||= false
end

#load_translations(*filenames) ⇒ Object

Accepts a list of paths to translation files. Loads translations from plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml for details.



17
18
19
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 17

def load_translations(*filenames)
  filenames.each { |filename| load_file(filename) }
end

#localize(locale, object, format = :default, options = {}) ⇒ Object

Acts the same as strftime, but uses a localized version of the format string. Takes a key from the date/time formats translations as a format argument (e.g., :short in :'date.formats').

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 54

def localize(locale, object, format = :default, options = {})
  raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)

  if Symbol === format
    key = format
    type = object.respond_to?(:sec) ? 'time' : 'date'
    format = I18n.t(:"#{type}.formats.#{key}", :locale => locale, :raise => true)
  end

  # format = resolve(locale, object, format, options)
  format = format.to_s.gsub(/%[aAbBp]/) do |match|
    case match
    when '%a' then I18n.t(:"date.abbr_day_names",                  :locale => locale, :format => format)[object.wday]
    when '%A' then I18n.t(:"date.day_names",                       :locale => locale, :format => format)[object.wday]
    when '%b' then I18n.t(:"date.abbr_month_names",                :locale => locale, :format => format)[object.mon]
    when '%B' then I18n.t(:"date.month_names",                     :locale => locale, :format => format)[object.mon]
    when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
    end
  end

  object.strftime(format)
end

#reload!Object



91
92
93
94
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 91

def reload!
  @initialized = false
  @translations = nil
end

#store_translations(locale, data, options = {}) ⇒ Object

Stores translations for the given locale in memory. This uses a deep merge for the translations hash, so existing translations will be overwritten by new ones only at the deepest level of the hash.



25
26
27
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 25

def store_translations(locale, data, options = {})
  merge_translations(locale, data, options)
end

#translate(locale, key, options = {}) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_support/vendor/i18n-0.3.7/i18n/backend/base.rb', line 29

def translate(locale, key, options = {})
  raise InvalidLocale.new(locale) unless locale
  return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)

  if options.empty?
    entry = resolve(locale, key, lookup(locale, key), options)
    raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
  else
    count, scope, default = options.values_at(:count, :scope, :default)
    values = options.reject { |name, value| RESERVED_KEYS.include?(name) }

    entry = lookup(locale, key, scope, options)
    entry = entry.nil? && default ? default(locale, key, default, options) : resolve(locale, key, entry, options)
    raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?

    entry = pluralize(locale, entry, count)    if count
    entry = interpolate(locale, entry, values) if values
  end

  entry
end