Class: I18n::Backend::Simple
- Defined in:
- lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
Constant Summary collapse
- INTERPOLATION_RESERVED_KEYS =
%w(scope default)
- MATCH =
/(\\\\)?\{\{([^\}]+)\}\}/
Instance Method Summary collapse
- #initialized? ⇒ Boolean
-
#load_translations(*filenames) ⇒ Object
Accepts a list of paths to translation files.
-
#localize(locale, object, format = :default) ⇒ Object
Acts the same as
strftime
, but returns a localized version of the formatted date string. - #reload! ⇒ Object
-
#store_translations(locale, data) ⇒ Object
Stores translations for the given locale in memory.
- #translate(locale, key, options = {}) ⇒ Object
Instance Method Details
#initialized? ⇒ Boolean
68 69 70 |
# File 'lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb', line 68 def initialized? @initialized ||= false end |
#load_translations(*filenames) ⇒ Object
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.
12 13 14 |
# File 'lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb', line 12 def load_translations(*filenames) filenames.each { |filename| load_file(filename) } end |
#localize(locale, object, format = :default) ⇒ Object
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'
).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb', line 48 def localize(locale, object, format = :default) raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime) type = object.respond_to?(:sec) ? 'time' : 'date' # TODO only translate these if format is a String? formats = translate(locale, :"#{type}.formats") format = formats[format.to_sym] if formats && formats[format.to_sym] # TODO raise exception unless format found? format = format.to_s.dup # TODO only translate these if the format string is actually present # TODO check which format strings are present, then bulk translate then, then replace them format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon]) format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour object.strftime(format) end |
#reload! ⇒ Object
72 73 74 75 |
# File 'lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb', line 72 def reload! @initialized = false @translations = nil end |
#store_translations(locale, data) ⇒ Object
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.
20 21 22 |
# File 'lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb', line 20 def store_translations(locale, data) merge_translations(locale, data) end |
#translate(locale, key, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb', line 24 def translate(locale, key, = {}) raise InvalidLocale.new(locale) if locale.nil? return key.map { |k| translate(locale, k, ) } if key.is_a? Array reserved = :scope, :default count, scope, default = .values_at(:count, *reserved) .delete(:default) values = .reject { |name, value| reserved.include?(name) } entry = lookup(locale, key, scope) if entry.nil? entry = default(locale, default, ) if entry.nil? raise(I18n::MissingTranslationData.new(locale, key, )) end end entry = pluralize(locale, entry, count) entry = interpolate(locale, entry, values) entry end |