Class: I18n::Backend::Advanced
- Defined in:
- lib/polish/backend/advanced.rb
Overview
Advanced I18n backend.
Extends Simple backend. Allows usage of “standalone” keys for DateTime localization and usage of user-defined Proc (lambda) pluralization methods in translation tables.
Constant Summary collapse
- LOCALIZE_ABBR_MONTH_NAMES_MATCH =
/(%d|%e)(.*)(%b)/
- LOCALIZE_MONTH_NAMES_MATCH =
/(%d|%e)(.*)(%B)/
- LOCALIZE_STANDALONE_ABBR_DAY_NAMES_MATCH =
/^%a/
- LOCALIZE_STANDALONE_DAY_NAMES_MATCH =
/^%A/
Constants inherited from Base
Base::INTERPOLATION_SYNTAX_PATTERN, Base::RESERVED_KEYS
Instance Method Summary collapse
-
#localize(locale, object, format = :default, options = nil) ⇒ Object
Acts the same as
strftime
, but returns a localized version of the formatted date string.
Methods inherited from Base
#available_locales, #initialized?, #load_translations, #reload!, #store_translations, #translate
Instance Method Details
#localize(locale, object, format = :default, options = nil) ⇒ 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'
).
Note that it differs from localize
in Simple< backend by checking for “standalone” month name/day name keys in translation and using them if available.
options
parameter added for i18n-0.3 compliance.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/polish/backend/advanced.rb', line 24 def localize(locale, object, format = :default, = nil) 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 if lookup(locale, :"date.standalone_abbr_day_names") format.gsub!(LOCALIZE_STANDALONE_ABBR_DAY_NAMES_MATCH, translate(locale, :"date.standalone_abbr_day_names")[object.wday]) end format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday]) if lookup(locale, :"date.standalone_day_names") format.gsub!(LOCALIZE_STANDALONE_DAY_NAMES_MATCH, translate(locale, :"date.standalone_day_names")[object.wday]) end format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday]) if lookup(locale, :"date.standalone_abbr_month_names") format.gsub!(LOCALIZE_ABBR_MONTH_NAMES_MATCH) do $1 + $2 + translate(locale, :"date.abbr_month_names")[object.mon] end format.gsub!(/%b/, translate(locale, :"date.standalone_abbr_month_names")[object.mon]) else format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon]) end if lookup(locale, :"date.standalone_month_names") format.gsub!(LOCALIZE_MONTH_NAMES_MATCH) do $1 + $2 + translate(locale, :"date.month_names")[object.mon] end format.gsub!(/%B/, translate(locale, :"date.standalone_month_names")[object.mon]) else format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon]) end format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour object.strftime(format) end |