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 81
81: def available_locales
82: raise NotImplementedError
83: 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 19
19: def load_translations(*filenames)
20: filenames = I18n.load_path.flatten if filenames.empty?
21: filenames.each { |filename| load_file(filename) }
22: 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 56
56: def localize(locale, object, format = :default, options = {})
57: raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
58:
59: if Symbol === format
60: key = format
61: type = object.respond_to?(:sec) ? 'time' : 'date'
62: format = I18n.t(:"#{type}.formats.#{key}", options.merge(:raise => true, :object => object, :locale => locale))
63: end
64:
65: # format = resolve(locale, object, format, options)
66: format = format.to_s.gsub(/%[aAbBp]/) do |match|
67: case match
68: when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
69: when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
70: when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
71: when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
72: when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
73: end
74: end
75:
76: object.strftime(format)
77: end
# File lib/i18n/backend/base.rb, line 85
85: def reload!
86: @skip_syntax_deprecation = false
87: end
This method receives a locale, a data hash and options for storing translations. Should be implemented
# File lib/i18n/backend/base.rb, line 26
26: def store_translations(locale, data, options = {})
27: raise NotImplementedError
28: end
# File lib/i18n/backend/base.rb, line 30
30: def translate(locale, key, options = {})
31: raise InvalidLocale.new(locale) unless locale
32: return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
33:
34: entry = key && lookup(locale, key, options[:scope], options)
35:
36: if options.empty?
37: entry = resolve(locale, key, entry, options)
38: else
39: count, default = options.values_at(:count, :default)
40: values = options.except(*RESERVED_KEYS)
41: entry = entry.nil? && default ?
42: default(locale, key, default, options) : resolve(locale, key, entry, options)
43: end
44:
45: raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
46: entry = entry.dup if entry.is_a?(String)
47:
48: entry = pluralize(locale, entry, count) if count
49: entry = interpolate(locale, entry, values) if values
50: entry
51: 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 100
100: def default(locale, object, subject, options = {})
101: options = options.dup.reject { |key, value| key == :default }
102: case subject
103: when Array
104: subject.each do |item|
105: result = resolve(locale, object, item, options) and return result
106: end and nil
107: else
108: resolve(locale, object, subject, options)
109: end
110: 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}"
Note that you have to double escape the \ when you want to escape the {{...}} key in a string (once for the string and once for the interpolation).
# File lib/i18n/backend/base.rb, line 152
152: def interpolate(locale, string, values = {})
153: return string unless string.is_a?(::String) && !values.empty?
154: original_values = values.dup
155:
156: preserve_encoding(string) do
157: string = string.gsub(DEPRECATED_INTERPOLATION_SYNTAX_PATTERN) do
158: escaped, key = $1, $2.to_sym
159: if escaped
160: "{{#{key}}}"
161: else
162: warn_syntax_deprecation!
163: "%{#{key}}"
164: end
165: end
166:
167: keys = string.scan(INTERPOLATION_SYNTAX_PATTERN).flatten
168: return string if keys.empty?
169:
170: values.each do |key, value|
171: if keys.include?(key.to_s)
172: value = value.call(values) if interpolate_lambda?(value, string, key)
173: value = value.to_s unless value.is_a?(::String)
174: values[key] = value
175: else
176: values.delete(key)
177: end
178: end
179:
180: string % values
181: end
182: rescue KeyError => e
183: if string =~ RESERVED_KEYS_PATTERN
184: raise ReservedInterpolationKey.new($1.to_sym, string)
185: else
186: raise MissingInterpolationArgument.new(original_values, string)
187: end
188: end
returns true when the given value responds to :call and the key is an interpolation placeholder in the given string
# File lib/i18n/backend/base.rb, line 203
203: def interpolate_lambda?(object, string, key)
204: object.respond_to?(:call) && string =~ /%\{#{key}\}|%\<#{key}>.*?\d*\.?\d*[bBdiouxXeEfgGcps]\}/
205: 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 211
211: def load_file(filename)
212: type = File.extname(filename).tr('.', '').downcase
213: raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
214: data = send(:"load_#{type}", filename) # TODO raise a meaningful exception if this does not yield a Hash
215: data.each { |locale, d| store_translations(locale, d) }
216: 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 220
220: def load_rb(filename)
221: eval(IO.read(filename), binding, filename)
222: end
Loads a YAML translations file. The data must have locales as toplevel keys.
# File lib/i18n/backend/base.rb, line 226
226: def load_yml(filename)
227: YAML::load(IO.read(filename))
228: end
The method which actually looks up for the translation in the store.
# File lib/i18n/backend/base.rb, line 92
92: def lookup(locale, key, scope = [], options = {})
93: raise NotImplementedError
94: 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 135
135: def pluralize(locale, entry, count)
136: return entry unless entry.is_a?(Hash) && count
137:
138: key = :zero if count == 0 && entry.has_key?(:zero)
139: key ||= count == 1 ? :one : :other
140: raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
141: entry[key]
142: end
# File lib/i18n/backend/base.rb, line 190
190: def preserve_encoding(string)
191: if string.respond_to?(:encoding)
192: encoding = string.encoding
193: result = yield
194: result.force_encoding(encoding) if result.respond_to?(:force_encoding)
195: result
196: else
197: yield
198: end
199: 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 116
116: def resolve(locale, object, subject, options = nil)
117: return subject if options[:resolve] == false
118: case subject
119: when Symbol
120: I18n.translate(subject, (options || {}).merge(:locale => locale, :raise => true))
121: when Proc
122: date_or_time = options.delete(:object) || object
123: resolve(locale, object, subject.call(date_or_time, options), options = {})
124: else
125: subject
126: end
127: rescue MissingTranslationData
128: nil
129: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.