Module: StampL10n::TranslatorExtension

Defined in:
lib/stamp-l10n/translator_extension.rb

Instance Method Summary collapse

Instance Method Details

#date_emitter_with_locale(token, locale = I18n.locale) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stamp-l10n/translator_extension.rb', line 37

def date_emitter_with_locale(token, locale = I18n.locale)
  case token
  when ::Stamp::Translator::MONTHNAMES_REGEXP
    ::Stamp::Emitters::Lookup.month(locale)
  when ::Stamp::Translator::ABBR_MONTHNAMES_REGEXP
    ::Stamp::Emitters::Lookup.abbr_month(locale)
  when ::Stamp::Translator::DAYNAMES_REGEXP
    ::Stamp::Emitters::Lookup.day(locale)
  when ::Stamp::Translator::ABBR_DAYNAMES_REGEXP
    ::Stamp::Emitters::Lookup.abbr_day(locale)
  else
    date_emitter(token)
  end
end

#time_emitter_with_locale(token, locale = I18n.locale) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/stamp-l10n/translator_extension.rb', line 26

def time_emitter_with_locale(token, locale = I18n.locale)
  case token
  when ::Stamp::Translator::MERIDIAN_LOWER_REGEXP
    ::Stamp::Emitters::AmPm.lowercase(locale)
  when ::Stamp::Translator::MERIDIAN_UPPER_REGEXP
    ::Stamp::Emitters::AmPm.uppercase(locale)
  else
    time_emitter(token)
  end
end

#translate_with_locale(example, locale = I18n.locale) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stamp-l10n/translator_extension.rb', line 3

def translate_with_locale(example, locale = I18n.locale)
  # extract any substrings that look like times, like "23:59" or "8:37 am"
  before, time_example, after = example.partition(::Stamp::Translator::TIME_REGEXP)

  # build emitters from the example date
  emitters = ::Stamp::Emitters::Composite.new
  emitters << build_emitters(before.split(/\b/)) do |token|
    date_emitter_with_locale(token, locale)
  end

  # build emitters from the example time
  unless time_example.empty?
    time_parts = time_example.scan(::Stamp::Translator::TIME_REGEXP).first
    emitters << build_emitters(time_parts) do |token|
      time_emitter_with_locale(token, locale)
    end
  end

  # recursively process any remaining text
  emitters << translate(after) unless after.empty?
  emitters
end