Module: JactiveSupport::Localize::ClassMethods

Defined in:
lib/jactive_support/localize.rb

Instance Method Summary collapse

Instance Method Details

#i18n_formatter(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jactive_support/localize.rb', line 20

def i18n_formatter(options = {})
  format = options[:format] || :default
  locale = options.fetch(:locale, ::I18n.locale)
  if Symbol === format
    key  = format
    options = options.merge(raise: true, object: self)
    format = ::I18n.t("#{self.i18n_scope}.formats.#{key}", options)
  end
  if format.blank?
    formatter = default_formatter(locale)
  else
    formatter = pattern_formatter(format, locale)
  end

  if options[:time_zone] && formatter.respond_to?(:time_zone)
    formatter.time_zone = options[:time_zone].to_java_time_zone
  end

  formatter
end

#parse_i18n(str, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jactive_support/localize.rb', line 41

def parse_i18n(str, options = {})
  unless options[:format]
    defaults = [
      :"#{self.i18n_scope}.formats.default", 
      options.delete(:default)]
    defaults.compact!
    defaults.flatten!
    options[:default] = defaults unless defaults.empty?
    options[:format] = :input
  end
  formatter = i18n_formatter(options)
  formatter.parse(str)
end