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 74
74: def available_locales
75: raise NotImplementedError
76: end
Accepts a list of paths to translation files. Loads translations from plain Ruby (*.rb) or YAML files (*.yml). See # and # for details.
# File lib/i18n/backend/base.rb, line 13
13: def load_translations(*filenames)
14: filenames = I18n.load_path if filenames.empty?
15: filenames.flatten.each { |filename| load_file(filename) }
16: end
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 48
48: def localize(locale, object, format = :default, options = {})
49: raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
50:
51: if Symbol === format
52: key = format
53: type = object.respond_to?(:sec) ? 'time' : 'date'
54: options = options.merge(:raise => true, :object => object, :locale => locale)
55: format = I18n.t(:"#{type}.formats.#{key}", options)
56: end
57:
58: # format = resolve(locale, object, format, options)
59: format = format.to_s.gsub(/%[aAbBp]/) do |match|
60: case match
61: when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
62: when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
63: when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
64: when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
65: when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
66: end
67: end
68:
69: object.strftime(format)
70: end
# File lib/i18n/backend/base.rb, line 78
78: def reload!
79: @skip_syntax_deprecation = false
80: end
This method receives a locale, a data hash and options for storing translations. Should be implemented
# File lib/i18n/backend/base.rb, line 20
20: def store_translations(locale, data, options = {})
21: raise NotImplementedError
22: end
# File lib/i18n/backend/base.rb, line 24
24: def translate(locale, key, options = {})
25: raise InvalidLocale.new(locale) unless locale
26: entry = key && lookup(locale, key, options[:scope], options)
27:
28: if options.empty?
29: entry = resolve(locale, key, entry, options)
30: else
31: count, default = options.values_at(:count, :default)
32: values = options.except(*RESERVED_KEYS)
33: entry = entry.nil? && default ?
34: default(locale, key, default, options) : resolve(locale, key, entry, options)
35: end
36:
37: throw(:exception, I18n::MissingTranslation.new(locale, key, options)) if entry.nil?
38: entry = entry.dup if entry.is_a?(String)
39:
40: entry = pluralize(locale, entry, count) if count
41: entry = interpolate(locale, entry, values) if values
42: entry
43: end
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 93
93: def default(locale, object, subject, options = {})
94: options = options.dup.reject { |key, value| key == :default }
95: case subject
96: when Array
97: subject.each do |item|
98: result = resolve(locale, object, item, options) and return result
99: end and nil
100: else
101: resolve(locale, object, subject, options)
102: end
103: end
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 142
142: def interpolate(locale, string, values = {})
143: if string.is_a?(::String) && !values.empty?
144: I18n.interpolate(string, values)
145: else
146: string
147: end
148: end
Loads a single translations file by delegating to # or # 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 154
154: def load_file(filename)
155: type = File.extname(filename).tr('.', '').downcase
156: raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
157: data = send(:"load_#{type}", filename)
158: raise InvalidLocaleData.new(filename) unless data.is_a?(Hash)
159: data.each { |locale, d| store_translations(locale, d || {}) }
160: end
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 164
164: def load_rb(filename)
165: eval(IO.read(filename), binding, filename)
166: end
Loads a YAML translations file. The data must have locales as toplevel keys.
# File lib/i18n/backend/base.rb, line 170
170: def load_yml(filename)
171: YAML.load_file(filename)
172: end
The method which actually looks up for the translation in the store.
# File lib/i18n/backend/base.rb, line 85
85: def lookup(locale, key, scope = [], options = {})
86: raise NotImplementedError
87: end
Picks a translation from an array according to English pluralization rules. It will pick the first translation if count is not equal to 1 and the second translation if it is equal to 1. Other backends can implement more flexible or complex pluralization rules.
# File lib/i18n/backend/base.rb, line 129
129: def pluralize(locale, entry, count)
130: return entry unless entry.is_a?(Hash) && count
131:
132: key = :zero if count == 0 && entry.has_key?(:zero)
133: key ||= count == 1 ? :one : :other
134: raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
135: entry[key]
136: end
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 109
109: def resolve(locale, object, subject, options = {})
110: return subject if options[:resolve] == false
111: result = catch(:exception) do
112: case subject
113: when Symbol
114: I18n.translate(subject, options.merge(:locale => locale, :throw => true))
115: when Proc
116: date_or_time = options.delete(:object) || object
117: resolve(locale, object, subject.call(date_or_time, options))
118: else
119: subject
120: end
121: end
122: result unless result.is_a?(MissingTranslation)
123: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.