Module: DateTools::Mixin
Overview
Constant Summary collapse
- DATE_TEXTS =
Constant/Hash with the supported languages.
{}
Class Method Summary collapse
-
.get_language_key(locale = nil, default = 'en') ⇒ Object
Get the key for the wanted language.
-
.locale?(lang) ⇒ Boolean
Is the seleted locale/language supported?.
-
.set_target_encoding(enc) ⇒ Object
The daynames are encoded in UTF by default.
Instance Method Summary collapse
-
#strftime_locale(format = '%F', language = nil) ⇒ Object
strftime with the day- and month names in the selected language.
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?
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 128 |
# 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(/%(-*\d*)([aAbB])/){|m| mformat = '%%%ss' % $1 case $2 when 'a'; mformat % abbr_daynames[self.wday] when 'A'; mformat % daynames[self.wday] when 'b'; mformat % abbr_monthnames[self.mon] when 'B'; mformat % monthnames[self.mon] else raise "Date#strftime: InputError" end } ) if defined? @@default_encoding result.encode(@@default_encoding) else result end end |