module I18n::Backend::Base

Public Instance Methods

available_locales() click to toggle source

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

# File lib/i18n/backend/base.rb, line 79
def available_locales
  raise NotImplementedError
end
exists?(locale, key) click to toggle source
# File lib/i18n/backend/base.rb, line 45
def exists?(locale, key)
  lookup(locale, key) != nil
end
load_translations(*filenames) click to toggle source

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.

# File lib/i18n/backend/base.rb, line 13
def load_translations(*filenames)
  filenames = I18n.load_path if filenames.empty?
  filenames.flatten.each { |filename| load_file(filename) }
end
localize(locale, object, format = :default, options = {}) click to toggle source

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

# File lib/i18n/backend/base.rb, line 52
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'
    options = options.merge(:raise => true, :object => object, :locale => locale)
    format  = I18n.t(:"#{type}.formats.#{key}", options)
  end

  # format = resolve(locale, object, format, options)
  format = format.to_s.gsub(/%[aAbBpP]/) 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).upcase if object.respond_to? :hour
    when '%P' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
    end
  end

  object.strftime(format)
end
reload!() click to toggle source
# File lib/i18n/backend/base.rb, line 83
def reload!
end
store_translations(locale, data, options = {}) click to toggle source

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

# File lib/i18n/backend/base.rb, line 20
def store_translations(locale, data, options = {})
  raise NotImplementedError
end
translate(locale, key, options = {}) click to toggle source
# File lib/i18n/backend/base.rb, line 24
def translate(locale, key, options = {})
  raise InvalidLocale.new(locale) unless locale
  entry = key && lookup(locale, key, options[:scope], options)

  if options.empty?
    entry = resolve(locale, key, entry, options)
  else
    count, default = options.values_at(:count, :default)
    values = options.except(*RESERVED_KEYS)
    entry = entry.nil? && default ?
      default(locale, key, default, options) : resolve(locale, key, entry, options)
  end

  throw(:exception, I18n::MissingTranslation.new(locale, key, options)) if entry.nil?
  entry = entry.dup if entry.is_a?(String)

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

Protected Instance Methods

default(locale, object, subject, options = {}) click to toggle source

Evaluates defaults. If given subject is an Array, it walks the array and returns the first translation that can be resolved. Otherwise it tries to resolve the translation directly.

# File lib/i18n/backend/base.rb, line 97
def default(locale, object, subject, options = {})
  options = options.dup.reject { |key, value| key == :default }
  case subject
  when Array
    subject.each do |item|
      result = resolve(locale, object, item, options) and return result
    end and nil
  else
    resolve(locale, object, subject, options)
  end
end
interpolate(locale, string, values = {}) click to toggle source

Interpolates values into a given string.

interpolate "file %{file} opened by %%{user}", :file => 'test.txt', :user => 'Mr. X'
# => "file test.txt opened by %{user}"
# File lib/i18n/backend/base.rb, line 150
def interpolate(locale, string, values = {})
  if string.is_a?(::String) && !values.empty?
    I18n.interpolate(string, values)
  else
    string
  end
end
load_file(filename) click to toggle source

Loads a single translations file by delegating to load_rb or load_yml depending on the file extension and directly merges the data to the existing translations. Raises I18n::UnknownFileType for all other file extensions.

# File lib/i18n/backend/base.rb, line 162
def load_file(filename)
  type = File.extname(filename).tr('.', '').downcase
  raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
  data = send(:"load_#{type}", filename)
  unless data.is_a?(Hash)
    raise InvalidLocaleData.new(filename, 'expects it to return a hash, but does not')
  end
  data.each { |locale, d| store_translations(locale, d || {}) }
end
load_rb(filename) click to toggle source

Loads a plain Ruby translations file. eval'ing the file must yield a Hash containing translation data with locales as toplevel keys.

# File lib/i18n/backend/base.rb, line 174
def load_rb(filename)
  eval(IO.read(filename), binding, filename)
end
load_yml(filename) click to toggle source

Loads a YAML translations file. The data must have locales as toplevel keys.

# File lib/i18n/backend/base.rb, line 180
def load_yml(filename)
  begin
    YAML.load_file(filename)
  rescue TypeError, ScriptError, StandardError => e
    raise InvalidLocaleData.new(filename, e.inspect)
  end
end
lookup(locale, key, scope = [], options = {}) click to toggle source

The method which actually looks up for the translation in the store.

# File lib/i18n/backend/base.rb, line 89
def lookup(locale, key, scope = [], options = {})
  raise NotImplementedError
end
pluralize(locale, entry, count) click to toggle source

Picks a translation from a pluralized mnemonic subkey according to English pluralization rules :

  • It will pick the :one subkey if count is equal to 1.

  • It will pick the :other subkey otherwise.

  • It will pick the :zero subkey in the special case where count is equal to 0 and there is a :zero subkey present. This behaviour is not stand with regards to the CLDR pluralization rules.

Other backends can implement more flexible or complex pluralization rules.

# File lib/i18n/backend/base.rb, line 137
def pluralize(locale, entry, count)
  return entry unless entry.is_a?(Hash) && count

  key = :zero if count == 0 && entry.has_key?(:zero)
  key ||= count == 1 ? :one : :other
  raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
  entry[key]
end
resolve(locale, object, subject, options = {}) click to toggle source

Resolves a translation. If the given subject is a Symbol, it will be translated with the given options. If it is a Proc then it will be evaluated. All other subjects will be returned directly.

# File lib/i18n/backend/base.rb, line 113
def resolve(locale, object, subject, options = {})
  return subject if options[:resolve] == false
  result = catch(:exception) do
    case subject
    when Symbol
      I18n.translate(subject, options.merge(:locale => locale, :throw => true))
    when Proc
      date_or_time = options.delete(:object) || object
      resolve(locale, object, subject.call(date_or_time, options))
    else
      subject
    end
  end
  result unless result.is_a?(MissingTranslation)
end