Class I18n::Backend::Simple
In: vendor/rails/activerecord/lib/active_record/i18n_interpolation_deprecation.rb
vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb
Parent: Object

Methods

Constants

DEPRECATED_INTERPOLATORS = { '%d' => '{{count}}', '%s' => '{{value}}' }
INTERPOLATION_RESERVED_KEYS = %w(scope default)
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/

Public Instance methods

Returns an array of locales for which translations are available

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 73
73:       def available_locales
74:         init_translations unless initialized?
75:         translations.keys
76:       end

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 68
68:       def initialized?
69:         @initialized ||= false
70:       end

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.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 12
12:       def load_translations(*filenames)
13:         filenames.each { |filename| load_file(filename) }
14:       end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 48
48:       def localize(locale, object, format = :default)
49:         raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
50: 
51:         type = object.respond_to?(:sec) ? 'time' : 'date'
52:         # TODO only translate these if format is a String?
53:         formats = translate(locale, "#{type}.formats""#{type}.formats")
54:         format = formats[format.to_sym] if formats && formats[format.to_sym]
55:         # TODO raise exception unless format found?
56:         format = format.to_s.dup
57: 
58:         # TODO only translate these if the format string is actually present
59:         # TODO check which format strings are present, then bulk translate then, then replace them
60:         format.gsub!(/%a/, translate(locale, "date.abbr_day_names""date.abbr_day_names")[object.wday])
61:         format.gsub!(/%A/, translate(locale, "date.day_names""date.day_names")[object.wday])
62:         format.gsub!(/%b/, translate(locale, "date.abbr_month_names""date.abbr_month_names")[object.mon])
63:         format.gsub!(/%B/, translate(locale, "date.month_names""date.month_names")[object.mon])
64:         format.gsub!(/%p/, translate(locale, "time.#{object.hour < 12 ? :am : :pm}""time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
65:         object.strftime(format)
66:       end

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 78
78:       def reload!
79:         @initialized = false
80:         @translations = nil
81:       end

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.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 20
20:       def store_translations(locale, data)
21:         merge_translations(locale, data)
22:       end

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 24
24:       def translate(locale, key, options = {})
25:         raise InvalidLocale.new(locale) if locale.nil?
26:         return key.map { |k| translate(locale, k, options) } if key.is_a? Array
27: 
28:         reserved = :scope, :default
29:         count, scope, default = options.values_at(:count, *reserved)
30:         options.delete(:default)
31:         values = options.reject { |name, value| reserved.include?(name) }
32: 
33:         entry = lookup(locale, key, scope)
34:         if entry.nil?
35:           entry = default(locale, default, options)
36:           if entry.nil?
37:             raise(I18n::MissingTranslationData.new(locale, key, options))
38:           end
39:         end
40:         entry = pluralize(locale, entry, count)
41:         entry = interpolate(locale, entry, values)
42:         entry
43:       end

Protected Instance methods

Return a new hash with all keys and nested keys converted to symbols.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 205
205:         def deep_symbolize_keys(hash)
206:           hash.inject({}) { |result, (key, value)|
207:             value = deep_symbolize_keys(value) if value.is_a? Hash
208:             result[(key.to_sym rescue key) || key] = value
209:             result
210:           }
211:         end

Evaluates a default translation. If the given default is a String it is used literally. If it is a Symbol it will be translated with the given options. If it is an Array the first translation yielded will be returned.

I.e., default(locale, [:foo, ‘default’]) will return default if translate(locale, :foo) does not yield a result.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 118
118:         def default(locale, default, options = {})
119:           case default
120:             when String then default
121:             when Symbol then translate locale, default, options
122:             when Array  then default.each do |obj|
123:               result = default(locale, obj, options.dup) and return result
124:             end and nil
125:           end
126:         rescue MissingTranslationData
127:           nil
128:         end

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 84
84:         def init_translations
85:           load_translations(*I18n.load_path.flatten)
86:           @initialized = true
87:         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).

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 151
151:         def interpolate(locale, string, values = {})
152:           return string unless string.is_a?(String)
153: 
154:           string.gsub(MATCH) do
155:             escaped, pattern, key = $1, $2, $2.to_sym
156: 
157:             if escaped
158:               pattern
159:             elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
160:               raise ReservedInterpolationKey.new(pattern, string)
161:             elsif !values.include?(key)
162:               raise MissingInterpolationArgument.new(pattern, string)
163:             else
164:               values[key].to_s
165:             end
166:           end
167:         end

[Source]

    # File vendor/rails/activerecord/lib/active_record/i18n_interpolation_deprecation.rb, line 12
12:         def interpolate_with_deprecated_syntax(locale, string, values = {})
13:           return string unless string.is_a?(String) && !values.empty?
14: 
15:           string = string.gsub(/%d|%s/) do |s|
16:             instead = DEPRECATED_INTERPOLATORS[s]
17:             ActiveSupport::Deprecation.warn "using #{s} in messages is deprecated; use #{instead} instead."
18:             instead
19:           end
20: 
21:           interpolate_without_deprecated_syntax(locale, string, values)
22:         end

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.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 173
173:         def load_file(filename)
174:           type = File.extname(filename).tr('.', '').downcase
175:           raise UnknownFileType.new(type, filename) unless respond_to?("load_#{type}""load_#{type}")
176:           data = send "load_#{type}""load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
177:           data.each { |locale, d| merge_translations(locale, d) }
178:         end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 182
182:         def load_rb(filename)
183:           eval(IO.read(filename), binding, filename)
184:         end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 188
188:         def load_yml(filename)
189:           YAML::load(IO.read(filename))
190:         end

Looks up a translation from the translations hash. Returns nil if eiher key is nil, or locale, scope or key do not exist as a key in the nested translations hash. Splits keys or scopes containing dots into multiple keys, i.e. currency.format is regarded the same as %w(currency format).

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 98
 98:         def lookup(locale, key, scope = [])
 99:           return unless key
100:           init_translations unless initialized?
101:           keys = I18n.send(:normalize_translation_keys, locale, key, scope)
102:           keys.inject(translations) do |result, k|
103:             if (x = result[k.to_sym]).nil?
104:               return nil
105:             else
106:               x
107:             end
108:           end
109:         end

Deep merges the given translations hash with the existing translations for the given locale

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 194
194:         def merge_translations(locale, data)
195:           locale = locale.to_sym
196:           translations[locale] ||= {}
197:           data = deep_symbolize_keys(data)
198: 
199:           # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
200:           merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
201:           translations[locale].merge!(data, &merger)
202:         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.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 134
134:         def pluralize(locale, entry, count)
135:           return entry unless entry.is_a?(Hash) and count
136:           # raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash)
137:           key = :zero if count == 0 && entry.has_key?(:zero)
138:           key ||= count == 1 ? :one : :other
139:           raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
140:           entry[key]
141:         end

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb, line 89
89:         def translations
90:           @translations ||= {}
91:         end

[Validate]