Module: DateTools::Mixin

Included in:
Date, Time
Defined in:
lib/date_tools/date_locale.rb

Overview

Constant Summary collapse

DATE_TEXTS =

Constant/Hash with the supported languages.

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_language_key(locale = nil, default = 'en') ⇒ Object

Get the key for the wanted language.

If locale is given, it i tried to detect the current language.

Allows the usage (or not to use) the locale-gem or i18n-gem.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/date_tools/date_locale.rb', line 61

def self.get_language_key( locale = nil, default = 'en' )
  
  if locale
      #convert Locale to string
      langkey = locale.to_s.downcase
  else #Undefined default, take actual locale or en
    langkey = default
    if defined?( Locale ) 
      langkey = Locale.current.to_s.downcase
    elsif defined?( I18n ) 
      langkey = I18n.locale.to_s.downcase
    end
  end

  #Convert language key to something like en_us, de_at...
  langkey.sub!(/-/, '_')
  #Use language if locale is not defined
  if ! locale?(langkey) and langkey =~ /(.+)[-_](.+)/
    langkey = $1
  end
  
  return langkey
    
end

.locale?(lang) ⇒ Boolean

Is the seleted locale/language supported?

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/date_tools/date_locale.rb', line 36

def self.locale?( lang )
  #fixme check via .get_language_key
  return !! DATE_TEXTS[lang.to_s]
end

.set_target_encoding(enc) ⇒ Object

The daynames are encoded in UTF by default. With this setter you can define an alternative target encoding.

Example:

Date_locale.set_target_encoding( 'iso-8859-1')


48
49
50
# File 'lib/date_tools/date_locale.rb', line 48

def self.set_target_encoding( enc )
  @@default_encoding = enc  
end

Instance Method Details

#strftime_locale(format = '%F', language = nil) ⇒ Object

strftime with the day- and month names in the selected language. This method is used to redefine Date#strftime, Time#strftime and DateTime#strftime .

language can be a Symbol, String or a Locale. It is checked with DateTools::Mixin.get_language_key .



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/date_tools/date_locale.rb', line 94

def strftime_locale(format = '%F', language = nil )
  
  lang = DateTools::Mixin.get_language_key(language)
  
  #Get the texts
  if DATE_TEXTS[lang]
    daynames = DATE_TEXTS[lang]['date']['day_names']
    abbr_daynames = DATE_TEXTS[lang]['date']['abbr_day_names']
    monthnames  = DATE_TEXTS[lang]['date']['month_names']
    abbr_monthnames = DATE_TEXTS[lang]['date']['abbr_month_names']
  else
    raise "Missing Support for locale #{lang.inspect}"
  end
  
  #Make the original replacements, after....
  result = self.strftime_orig( 
    #...you replaced the language dependent parts.
    format.gsub(/%([aAbB])/){|m|
          case $1
            when 'a'; abbr_daynames[self.wday]
            when 'A'; daynames[self.wday]
            when 'b'; abbr_monthnames[self.mon]
            when 'B'; monthnames[self.mon]
            else
              raise "Date#strftime: InputError"
          end
        }
      )
  if defined? @@default_encoding
    result.encode(@@default_encoding)
  else
    result
  end
end