Class Mack::Localization::Formatter
In: lib/mack-localization/formatter.rb
Parent: Object

Methods

Included Modules

Singleton

Public Class methods

Initialize the formatter object. Out of the box it will register 5 language supports for both the date and currency format

[Source]

    # File lib/mack-localization/formatter.rb, line 12
12:       def initialize
13:         @reg = Mack::Localization::FormatEngineRegistry.instance
14: 
15:         [:en, :bp, :fr, :it, :de, :es].each do |lang|
16:           klass = "Mack::Localization::DateFormatEngine::#{lang.to_s.upcase}" 
17:           @reg.register(lang, date_format_registry_key, klass.constantize.new)
18:         end
19:         
20:         [:en, :bp, :fr, :it, :de, :es].each do |lang|
21:           klass = "Mack::Localization::NumberAndCurrencyFormatEngine::#{lang.to_s.upcase}" 
22:           @reg.register(lang, currency_format_registry_key, klass.constantize.new)
23:         end
24:       end

Public Instance methods

Given a currency, format it according to the specified language

params:

  num - the amount of money to be formatted
  lang - the language

returns:

  the multibyte version of the formatted string

see also:

  l10n_currency in view_helpers

examples:

  currency_format(10000.00, :en) will produce "$10,000.00"

[Source]

    # File lib/mack-localization/formatter.rb, line 91
91:       def currency_format(num, lang)
92:         engine = @reg.get_engine(lang, currency_format_registry_key)
93:         raise Mack::Localization::Errors::FormatEngineNotFound.new(lang.to_s) if engine.nil?
94:         return u(engine.format_currency(num, lang))
95:       end

Return the registry key for currency formatter

[Source]

     # File lib/mack-localization/formatter.rb, line 107
107:       def currency_format_registry_key
108:         return :currency_format
109:       end

Given a time, format according to the specified type (:short, :medium, :long) and language.

params:

  time - the instance of time to be formatted
  type - what's the format? short, medium, or long
  lang - the language

returns:

  the multibyte version of the formatted string

see also:

  l10n_date in view_helpers

examples:

  time = Time.local(2008, "dec", 1)
  date_format(time, :short, :en) will produce "12/01/2008"
  date_format(time, :medium, :en) will produce "Tue, Dec 01, 2008"
  date_format(time, :long, :en) will produce "Tuesday, December 01, 2008"

[Source]

    # File lib/mack-localization/formatter.rb, line 47
47:       def date_format(time, type, lang)
48:         engine = @reg.get_engine(lang, date_format_registry_key)
49:         raise Mack::Localization::Errors::FormatEngineNotFound.new(lang.to_s) if engine.nil?
50:         return u(engine.format(time, type))
51:       end

Return the registry key for date formatter

[Source]

     # File lib/mack-localization/formatter.rb, line 100
100:       def date_format_registry_key
101:         return :date_format
102:       end

Given a number, format it according to the specified language

params:

  num - the number to be formatted
  lang - the language

returns:

  the multibyte version of the formatted string

see also:

  l10n_number in view_helpers

examples:

  number_format(10000.00, :en) will produce "10,000.00"

[Source]

    # File lib/mack-localization/formatter.rb, line 69
69:       def number_format(num, lang)
70:         engine = @reg.get_engine(lang, currency_format_registry_key)
71:         raise Mack::Localization::Errors::FormatEngineNotFound.new(lang.to_s) if engine.nil?
72:         return u(engine.format_number(num, lang))
73:       end

[Validate]