Module: I18n::Backend::Base

Includes:
Transliterator
Included in:
Chain::Implementation, KeyValue::Implementation, Simple::Implementation
Defined in:
lib/i18n/backend/base.rb

Constant Summary

Constants included from Transliterator

Transliterator::DEFAULT_REPLACEMENT_CHAR

Instance Method Summary collapse

Methods included from Transliterator

get, #transliterate

Instance Method Details

#available_localesObject

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

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/i18n/backend/base.rb', line 97

def available_locales
  raise NotImplementedError
end

#eager_load!Object



105
106
107
# File 'lib/i18n/backend/base.rb', line 105

def eager_load!
  @eager_loaded = true
end

#exists?(locale, key, options = EMPTY_HASH) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/i18n/backend/base.rb', line 71

def exists?(locale, key, options = EMPTY_HASH)
  lookup(locale, key, options[:scope]) != nil
end

#load_translations(*filenames) ⇒ Object

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



14
15
16
17
18
19
20
# File 'lib/i18n/backend/base.rb', line 14

def load_translations(*filenames)
  filenames = I18n.load_path if filenames.empty?
  filenames.flatten.each do |filename|
    loaded_translations = load_file(filename)
    yield filename, loaded_translations if block_given?
  end
end

#localize(locale, object, format = :default, options = EMPTY_HASH) ⇒ 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:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/i18n/backend/base.rb', line 78

def localize(locale, object, format = :default, options = EMPTY_HASH)
  if object.nil? && options.include?(:default)
    return options[:default]
  end
  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'
    options = options.merge(:raise => true, :object => object, :locale => locale)
    format  = I18n.t(:"#{type}.formats.#{key}", **options)
  end

  format = translate_localization_format(locale, object, format, options)
  object.strftime(format)
end

#reload!Object



101
102
103
# File 'lib/i18n/backend/base.rb', line 101

def reload!
  eager_load! if eager_loaded?
end

#store_translations(locale, data, options = EMPTY_HASH) ⇒ Object

This method receives a locale, a data hash and options for storing translations. Should be implemented

Raises:

  • (NotImplementedError)


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

def store_translations(locale, data, options = EMPTY_HASH)
  raise NotImplementedError
end

#translate(locale, key, options = EMPTY_HASH) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/i18n/backend/base.rb', line 28

def translate(locale, key, options = EMPTY_HASH)
  raise I18n::ArgumentError if (key.is_a?(String) || key.is_a?(Symbol)) && key.empty?
  raise InvalidLocale.new(locale) unless locale
  return nil if key.nil? && !options.key?(:default)

  entry = lookup(locale, key, options[:scope], options) unless key.nil?

  if entry.nil? && options.key?(:default)
    entry = default(locale, key, options[:default], options)
  else
    entry = resolve_entry(locale, key, entry, options)
  end

  count = options[:count]

  if entry.nil? && (subtrees? || !count)
    if (options.key?(:default) && !options[:default].nil?) || !options.key?(:default)
      throw(:exception, I18n::MissingTranslation.new(locale, key, options))
    end
  end

  entry = entry.dup if entry.is_a?(String)
  entry = pluralize(locale, entry, count) if count

  if entry.nil? && !subtrees?
    throw(:exception, I18n::MissingTranslation.new(locale, key, options))
  end

  deep_interpolation = options[:deep_interpolation]
  skip_interpolation = options[:skip_interpolation]
  values = Utils.except(options, *RESERVED_KEYS) unless options.empty?
  if !skip_interpolation && values && !values.empty?
    entry = if deep_interpolation
      deep_interpolate(locale, entry, values)
    else
      interpolate(locale, entry, values)
    end
  elsif entry.is_a?(String) && entry =~ I18n.reserved_keys_pattern
    raise ReservedInterpolationKey.new($1.to_sym, entry)
  end
  entry
end