date_tools 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/data/locales/ar.yml +47 -0
  2. data/data/locales/bg.yml +47 -0
  3. data/data/locales/ca.yml +47 -0
  4. data/data/locales/cs.yml +47 -0
  5. data/data/locales/da.yml +47 -0
  6. data/data/locales/de.yml +47 -0
  7. data/data/locales/de_at.yml +47 -0
  8. data/data/locales/de_ch.yml +47 -0
  9. data/data/locales/en.yml +47 -0
  10. data/data/locales/en_au.yml +47 -0
  11. data/data/locales/en_ca.yml +47 -0
  12. data/data/locales/en_gb.yml +47 -0
  13. data/data/locales/en_in.yml +47 -0
  14. data/data/locales/en_us.yml +47 -0
  15. data/data/locales/es.yml +47 -0
  16. data/data/locales/es_ar.yml +47 -0
  17. data/data/locales/es_cl.yml +47 -0
  18. data/data/locales/es_co.yml +47 -0
  19. data/data/locales/es_mx.yml +47 -0
  20. data/data/locales/es_pe.yml +47 -0
  21. data/data/locales/es_ve.yml +47 -0
  22. data/data/locales/fi.yml +47 -0
  23. data/data/locales/fr.yml +47 -0
  24. data/data/locales/fr_ca.yml +47 -0
  25. data/data/locales/fr_ch.yml +47 -0
  26. data/data/locales/it.yml +47 -0
  27. data/data/locales/it_ch.yml +47 -0
  28. data/data/locales/ko.yml +47 -0
  29. data/data/locales/lt.yml +47 -0
  30. data/data/locales/nl.yml +47 -0
  31. data/data/locales/nl_be.yml +47 -0
  32. data/data/locales/no.yml +47 -0
  33. data/data/locales/pt.yml +47 -0
  34. data/data/locales/pt_br.yml +47 -0
  35. data/data/locales/ru.yml +47 -0
  36. data/data/locales/se.yml +47 -0
  37. data/data/locales/sr.yml +47 -0
  38. data/data/locales/zh_TW.yml +47 -0
  39. data/examples/example_date_creator.rb +9 -1
  40. data/examples/example_date_i18n.rb +52 -0
  41. data/examples/example_date_locale.rb +18 -5
  42. data/examples/example_date_time_compare.rb +11 -2
  43. data/lib/date_tools.rb +93 -26
  44. data/lib/date_tools/date_locale.rb +126 -234
  45. data/lib/date_tools/date_time_compare.rb +3 -0
  46. data/unittest/test_date_creator.rb +1 -0
  47. data/unittest/test_date_locale.rb +103 -196
  48. data/unittest/test_date_locale_loaded.rb +113 -0
  49. data/unittest/test_date_time_compare.rb +1 -0
  50. data/unittest/test_date_tools.rb +13 -0
  51. metadata +90 -37
  52. data/readme.html +0 -68
  53. data/readme.txt +0 -50
@@ -1,15 +1,28 @@
1
- require '../lib/date_tools/date_locale'
1
+ #encoding: utf-8
2
+ =begin rdoc
3
+ Example of the use of the gem in combination with locale.rb
4
+ =end
5
+ module Example
6
+ end
7
+ $:.unshift('../lib')
8
+ require 'date_tools/date_locale'
9
+
2
10
 
3
11
  #Here you can play with locales.
4
12
  #Please run the tests with and without locale.
5
- #~ require 'locale'
13
+ require 'locale'
6
14
  #~ Locale.current = 'en'
7
15
  #~ Locale.current = 'fr'
8
16
 
17
+ #~ puts $"
18
+ #Exception with ruby 1.9
19
+ #~ Locale.current = 'de'
20
+ p Locale.current
21
+
9
22
  #
10
23
  #Test with encoding conversion
11
24
  #
12
- Date_locale.set_target_encoding( 'iso-8859-1')
25
+ DateTools::Mixin.set_target_encoding( 'iso-8859-1')
13
26
  [
14
27
  Date.new(2009, 1,21),
15
28
  Date.new(2009, 2,21),
@@ -23,13 +36,13 @@ Date_locale.set_target_encoding( 'iso-8859-1')
23
36
 
24
37
  #
25
38
  #Test all languages (unicode nbeeded)
26
- Date_locale.set_target_encoding( 'utf-8')
39
+ DateTools::Mixin.set_target_encoding( 'utf-8')
27
40
  [
28
41
  Date.new(2009, 1,21),
29
42
  Date.new(2009, 2,21),
30
43
  Date.new(2009,10,21),
31
44
  ].each{|d|
32
- Date_locale::DATE_TEXTS.keys.each{|lang|
45
+ DateTools::Mixin::DATE_TEXTS.keys.each{|lang|
33
46
  puts d.strftime("#{lang}: %Y-%m-%d: %A (%a) %B (%b)", lang)
34
47
  }
35
48
  }
@@ -1,4 +1,11 @@
1
- require '../lib/date_tools/date_time_compare.rb'
1
+ =begin rdoc
2
+ Example, how you can compare Time and Date-objects.
3
+ =end
4
+ module Example
5
+ end
6
+
7
+ $:.unshift('../lib')
8
+ require 'date_tools/date_time_compare.rb'
2
9
 
3
10
 
4
11
  array = [
@@ -13,7 +20,9 @@ array = [
13
20
  array << DateTime.new(rand(10)+1995 ,rand(11)+1, rand(27)+1, rand(22)+1,rand(58)+1)
14
21
  }
15
22
 
16
- #~ array.sort #-> error
23
+ =begin
24
+ Normally array.sort with Time- and Date-objects end in an error.
25
+ =end
17
26
  #~ puts array.map{|d| d.to_date}.sort
18
27
  array.sort_by{|d| d.to_date}.each{|d|
19
28
  puts d.strftime("%Y-%m-%d %H:%M (#{d.class})")
@@ -1,31 +1,98 @@
1
- #Little helper for the classes Date, DateTime and Time:
2
- #
3
- #= date_time_compare.rb:
4
- #Allows to compare Date and Time objects (allows to sort by mixed Date/Time-keys).
5
- #
6
- #Time gets also some methods like Date:
7
- #* Time#cwday
8
- #* Time#cweek
9
- #* Time#cwyear
10
- #
11
- #= date_creator.rb:
12
- #New date creations:
13
- #
14
- #Date#new_by_mday: Allows "Third sunday in june 2010"...
15
- #
16
- #
17
- #= date_locale.rb:
18
- #Localization of Date, DateTime and Time.
19
- #
20
- #strftime (Date#strftime, Time#strftime and DateTime#strftime)
21
- #gets a optional parameter to determine the language.
22
- #
23
- #Default is en, or if locale is used the current locale.
1
+ =begin
2
+ =date_tools-Gem
3
+ Define some helper for the classes Date, DateTime and Time:
4
+ * compare to time (sort date and time-objects)
5
+ * define locales (non-english monthnames.
6
+ * new creators
7
+
8
+ Check the examples-folders to get an impression of the gem.
9
+
10
+ ==date_tools/date_time_compare.rb:
11
+ Usage:
12
+ require 'date_tools/date_time_compare'
13
+
14
+ Allows to compare Date and Time objects (allows to sort by mixed Date/Time-keys).
15
+
16
+ Time gets also some methods like Date:
17
+ * Time#cwday
18
+ * Time#cweek
19
+ * Time#cwyear
20
+
21
+ -> please check the examples in
22
+ examples/example_date_time_compare.rb[link:files/examples/example_date_time_compare_rb.html]
23
+
24
+ ==date_tools/date_creator.rb:
25
+ Usage:
26
+ require 'date_tools/date_creator'
27
+
28
+ New date creations:
29
+ Date.new_by_mday: Allows "Third sunday in june 2010"...
30
+
31
+ -> please check the examples in
32
+ examples/example_date_creator.rb[link:files/examples/example_date_creator_rb.html]
33
+
34
+
35
+ ==date_tools/date_locale.rb:
36
+ Usage:
37
+ require 'date_tools/date_locale.rb'
38
+
39
+ Localization of Date, DateTime and Time.
40
+
41
+ strftime (Date#strftime, Time#strftime and DateTime#strftime)
42
+ gets an optional parameter to determine the language.
43
+
44
+ Default is en, or if locale is used the current locale.
45
+
46
+ -> please check the examples in
47
+ examples/example_date_locale.rb[link:files/examples/example_date_locale_rb.html]
48
+
49
+ ==Usage with I18N
50
+
51
+ With I18N y
52
+ ou don't need a redefinition of Time#strftime,
53
+ with I18N you use the I18n.localize function.
54
+
55
+ But you can use this gem to define the translations of day and month names.
56
+
57
+ Example:
58
+
59
+ require 'date_tools'
60
+ require 'i18n'
61
+
62
+ I18n.load_path = DateTools::I18N_FILES
63
+ I18n.load_path += YOUR_I18N_FILES
64
+ I18n.backend.load_translations #this is done automatic by rails
65
+
66
+ %w{en de de_at}.each{|locale|
67
+ I18n.locale = locale
68
+ puts I18n.localize Time.now #"07. r 2013 00:13"
69
+ #~ puts I18n.l Time.now #"07. r 2013 00:13"
70
+ }
71
+
72
+ Remark:
73
+ If you get a
74
+ translation missing: en.time.formats.default (I18n::MissingTranslationData)
75
+ then you didn't localize time/date in your project (YOUR_I18N_FILES).
76
+
77
+ This gem doesn't define, how your Time/Date is printed,
78
+ it only defines the correct day- and month name if you use %B/%A.
79
+
80
+ -> please check the examples in
81
+ examples/example_date_i18n.rb[link:files/examples/example_date_i18n_rb.html]
82
+
83
+ =end
24
84
 
85
+ module DateTools
86
+ VERSION = '0.1.0'
25
87
 
88
+ #Path to I18N-compatible language definition for day- and month names.
89
+ I18N_FILES = Dir[File.join(File.expand_path(__FILE__),'..','..','data', 'locales','*.yml')]
90
+
91
+
92
+ end #module Date_tools
26
93
  #
27
94
  #There are no dependicies between the
28
95
  #
29
- require 'date_tools/date_creator.rb'
30
- require 'date_tools/date_locale.rb'
31
- require 'date_tools/date_time_compare.rb'
96
+ #~ require 'date_tools/date_creator.rb'
97
+ #~ require 'date_tools/date_locale.rb'
98
+ #~ require 'date_tools/date_time_compare.rb'
@@ -1,249 +1,137 @@
1
+ #encoding: utf-8
1
2
  #
2
3
  #Make a 'localization' for Date, DateTime and Time.
3
4
  #
4
5
  #This is not using locale, but if you use locale, it is detected and locale sensitive.
5
6
  #
6
- #The output is in iso-8859-1, other encodings can be set with Date_locale.set_target_encoding.
7
+ #The defaut output is utf-8, other encodings can be set with Date_locale.set_target_encoding.
7
8
  #
8
9
 
10
+ require_relative '../date_tools'
11
+ require 'date'
12
+ require 'yaml'
9
13
 
10
- require 'iconv'
11
- #~ require 'date'
12
-
14
+ module DateTools
13
15
  #
14
- #Adaption for a localized Date-class
16
+ #Adaption for a localized Date, Time and DateTime-class
15
17
  #
16
- #Solution based on discussion at ruby-forum.de
17
- #-http://forum.ruby-portal.de/viewtopic.php?f=1&t=10527&start=0
18
+ #Solution based on discussion at ruby-forum.de
19
+ #- http://forum.ruby-portal.de/viewtopic.php?f=1&t=10527&start=0
18
20
  #
19
- module Date_locale
20
-
21
- #Constant/Hash with the supported languages.
22
- #
23
- #Initial definitions are taken from localization_simplified.
24
- #
25
- #Changes:
26
- #* added de_at
27
- #* adapted :pt to pt_br (original :pt was French).
28
- DATE_TEXTS = {
29
- :ca => {
30
- :monthnames => [nil] + %w{gener febrer març abril maig juny juliol agost setembre octubre novembre desembre},
31
- :abbr_monthnames => [nil] + %w{gen feb mar abr mai jun jul ago set oct nov des},
32
- :daynames => %w{diumenge dilluns dimarts dimecres dijous divendres dissabte},
33
- :abbr_daynames => %w{dmg dll dmt dmc djs dvn dsb},
34
- },
35
- :cf => {
36
- :monthnames => [nil] + %w{Janvier Février Mars Avril Mai Juin Juillet Août Septembre Octobre Novembre Décembre},
37
- :abbr_monthnames => [nil] + %w{Jan Fev Mar Avr Mai Jun Jui Aou Sep Oct Nov Dec},
38
- :daynames => %w{Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi},
39
- :abbr_daynames => %w{Dim Lun Mar Mer Jeu Ven Sam},
40
- },
41
- :cs => {
42
- :monthnames => [nil] + %w{Leden Únor Březen Duben Květen Červen Červenec Srpen Září Říjen Listopad Prosinec},
43
- :abbr_monthnames => [nil] + %w{Led Úno Bře Dub Kvě Čvn Čvc Srp Zář Říj Lis Pro},
44
- :daynames => %w{Neděle Pondělí Úterý Středa Čtvrtek Pátek Sobota},
45
- :abbr_daynames => %w{Ne Po Út St Čt Pá So},
46
- },
47
- :da => {
48
- :monthnames => [nil] + %w{januar februar marts april maj juni juli august september oktober november december},
49
- :abbr_monthnames => [nil] + %w{jan feb mar apr maj jun jul aug sep okt nov dec},
50
- :daynames => %w{søndag mandag tirsdag onsdag torsdag fredag lørdag},
51
- :abbr_daynames => %w{søn man tir ons tors fre lør},
52
- },
53
- :de => {
54
- :monthnames => [nil] + %w{Januar Februar März April Mai Juni Juli August September Oktober November Dezember},
55
- :abbr_monthnames => [nil] + %w{Jan Feb Mrz Apr Mai Jun Jul Aug Sep Okt Nov Dez},
56
- :daynames => %w{Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag},
57
- :abbr_daynames => %w{So Mo Di Mi Do Fr Sa},
58
- },
59
- :de_at => {
60
- :monthnames => [nil] + %w(Jänner Feber März April Mai Juni Juli August September Oktober November Dezember),
61
- :abbr_monthnames => [nil] + %w(Jan Feb Mrz Apr Mai Jun Jul Aug Sep Okt Nov Dez),
62
- :daynames => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
63
- :abbr_daynames => %w(So Mo Di Mi Do Fr Sa),
64
- },
65
- :en => {
66
- :monthnames => [nil] + %w{January February March April May June July August September October November December},
67
- :abbr_monthnames => [nil] + %w{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec},
68
- :daynames => %w{Sunday Monday Tuesday Wednesday Thursday Friday Saturday},
69
- :abbr_daynames => %w{Sun Mon Tue Wed Thu Fri Sat},
70
- },
71
- :es => {
72
- :monthnames => [nil] + %w{enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre},
73
- :abbr_monthnames => [nil] + %w{ene feb mar abr may jun jul ago sep oct nov dic},
74
- :daynames => %w{domingo lunes martes miércoles jueves viernes sábado},
75
- :abbr_daynames => %w{dom lun mar mié jue vie sáb},
76
- },
77
- :es_ar => {
78
- :monthnames => [nil] + %w{enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre},
79
- :abbr_monthnames => [nil] + %w{ene feb mar abr may jun jul ago sep oct nov dic},
80
- :daynames => %w{domingo lunes martes miércoles jueves viernes sábado},
81
- :abbr_daynames => %w{dom lun mar mié jue vie sáb},
82
- },
83
- :fi => {
84
- :monthnames => [nil] + %w{tammikuu helmikuu maaliskuu huhtikuu toukokuu kesäkuu heinäkuu elokuu syyskuu lokakuu marraskuu joulukuu},
85
- :abbr_monthnames => [nil] + %w{tammi helmi maalis huhti touko kesä heinä elo syys loka marras joulu},
86
- :daynames => %w{sunnuntai maanantai tiistai keskiviikko torstai perjantai lauantai},
87
- :abbr_daynames => %w{su ma ti ke to pe la},
88
- },
89
- :fr => {
90
- :monthnames => [nil] + %w{Janvier Février Mars Avril Mai Juin Juillet Août Septembre Octobre Novembre Decembre},
91
- :abbr_monthnames => [nil] + %w{Jan Fév Mar Avr Mai Jui Jul Aoû Sep Oct Nov Déc},
92
- :daynames => %w{Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi},
93
- :abbr_daynames => %w{Dim Lun Mar Mer Jeu Ven Sam},
94
- },
95
- :it => {
96
- :monthnames => [nil] + %w{Gennaio Febbraio Marzo Aprile Maggio Giugno Luglio Agosto Settembre Ottobre Novembre Dicembre },
97
- :daynames => %w{ Domenica Lunedì Martedì Mercoledì Giovedì Venerdì Sabato },
98
- :abbr_monthnames => [nil] + %w{ Gen Feb Mar Apr Mag Giu Lug Ago Set Ott Nov Dic },
99
- :abbr_daynames => %w{ Dom Lun Mar Mer Gio Ven Sab },
100
- },
101
- :ko => {
102
- :monthnames => [nil] + %w{1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월},
103
- :abbr_monthnames => [nil] + %w{1 2 3 4 5 6 7 8 9 10 11 12},
104
- :daynames => %w{일요일 월요일 화요일 수요일 목요일 금요일 토요일},
105
- :abbr_daynames => %w{일 월 화 수 목 금 토},
106
- },
107
- :nl => {
108
- :monthnames => [nil] + %w{Januari Februari Maart April Mei Juni Juli Augustus September Oktober November December},
109
- :abbr_monthnames => [nil] + %w{Jan Feb Maa Apr Mei Jun Jul Aug Sep Okt Nov Dec},
110
- :daynames => %w{Zondag Maandag Dinsdag Woensdag Donderdag Vrijdag Zaterdag},
111
- :abbr_daynames => %w{Zo Ma Di Wo Do Vr Za},
112
- },
113
- :no => {
114
- :monthnames => [nil] + %w{januar februar mars april mai juni juli august september oktober november desember},
115
- :abbr_monthnames => [nil] + %w{jan feb mar apr mai jun jul aug sep okt nov des},
116
- :daynames => %w{søndag mandag tirsdag onsdag torsdag fredag lørdag},
117
- :abbr_daynames => %w{søn man tir ons tors fre lør},
118
- },
119
- :pt => {
120
- :monthnames => [nil] + %w{Janeiro Fevereiro Março Abril Maio Junho Julho Agosto Setembro Outubro Novembro Dezembro},
121
- :abbr_monthnames => [nil] + %w{Jan Fev Mar Abr Mai Jun Jul Ago Set Out Nov Dez},
122
- :daynames => %w{domingo segunda terça quarta quinta sexta sábado},
123
- :abbr_daynames => %w{Dom Seg Ter Qua Qui Sex Sab},
124
- },
125
- :pt_br => {
126
- :monthnames => [nil] + %w{janeiro fevereiro março abril maio junho julho agosto setembro outubro novembro dezembro},
127
- :abbr_monthnames => [nil] + %w{jan fev mar abr mai jun jul ago set out nov dez},
128
- :daynames => %w{domingo segunda terça quarta quinta sexta sábado},
129
- :abbr_daynames => %w{dom seg ter qua qui sex sáb},
130
- },
131
- :ru => {
132
- :monthnames => [nil] + %w{Январь Февраль Март Апрель Май Июнь Июль Август Сентябрь Октябрь Ноябрь Декабрь},
133
- :abbr_monthnames => [nil] + %w{Янв Фев Мар Апр Май Июн Июл Авг Сен Окт Ноя Дек},
134
- :daynames => %w{Воскресенье Понедельник Вторник Среда Четверг Пятница Суббота},
135
- :abbr_daynames => %w{Вск Пнд Втр Сре Чет Пят Суб},
136
- },
137
- :se => {
138
- :monthnames => [nil] + %w{januari februari mars april maj juni juli augusti september oktober november december},
139
- :abbr_monthnames => [nil] + %w{jan feb mar apr maj jun jul aug sep okt nov dec},
140
- :daynames => %w{söndag måndag tisdag onsdag torsdag fredag lördag},
141
- :abbr_daynames => %w{sön mån tis ons tors fre lör},
142
- },
143
- :sr => {
144
- :monthnames => [nil] + %w{Januar Februar Mart April Maj Jun Jul Avgust Septembar Oktobar Novembar Decembar},
145
- :abbr_monthnames => [nil] + %w{Jan Feb Mar Apr Maj Jun Jul Aug Sep Okt Nov Dec},
146
- :daynames => %w{Nedelja Ponedeljak Utorak Sreda Četvrtak Petak Subota},
147
- :abbr_daynames => %w{Ned Pon Uto Sre Čet Pet Sub},
148
- },
149
- }
150
- #~ puts DATE_TEXTS.to_yaml
151
-
152
- #Not really necessary.
153
- #But I want to avoid later changes.
154
- DATE_TEXTS.freeze
21
+ module Mixin
22
+ #Constant/Hash with the supported languages.
23
+ DATE_TEXTS = {}
24
+ I18N_FILES.each{|langdef|
25
+ DATE_TEXTS.update(
26
+ YAML.load(File.read(langdef, :encoding => 'utf-8'))
27
+ )
28
+ }
29
+
30
+ #Not really necessary.
31
+ #But I want to avoid later changes.
32
+ #~ DATE_TEXTS.freeze
155
33
 
156
- #
157
- #Test if the seleted language is available in Date_locale.
158
- def self.locale?( lang )
159
- return DATE_TEXTS[lang]
160
- end
161
-
162
- #Set default converter
163
- #~ @@encoding_converter = Iconv.new( 'iso-8859-1', 'utf-8' )
164
-
165
- #
166
- #The daynames are encoded in UTF (I hope ;-) ).
167
- #With this method you can define a global converter.
168
- #
169
- #Example:
170
- # Date_locale.set_target_encoding( 'iso-8859-1')
171
- #
172
- def self.set_target_encoding( enc )
173
- @@encoding_converter = Iconv.new( enc, 'utf-8' )
174
- end
175
-
176
-
177
- #
178
- #Get the key for the wanted language.
179
- #
180
- #Allows the usage (or not to use) locale.
181
- def self.get_language_key( lang = nil )
34
+ #
35
+ #Is the seleted locale/language supported?
36
+ def self.locale?( lang )
37
+ #fixme check via .get_language_key
38
+ return !! DATE_TEXTS[lang.to_s]
39
+ end
182
40
 
183
41
  #
184
- #What's the better solution? Check for locale, or check for the method :language?
42
+ #The daynames are encoded in UTF by default.
43
+ #With this setter you can define an alternative target encoding.
44
+ #
45
+ #Example:
46
+ # Date_locale.set_target_encoding( 'iso-8859-1')
185
47
  #
186
- if defined?( Locale ) and lang.is_a?(Locale::Object)
187
- #~ if lang.respond_to?(:language)
188
- Date_locale.set_target_encoding( lang.charset )
189
- return lang.language.to_sym
48
+ def self.set_target_encoding( enc )
49
+ @@default_encoding = enc
190
50
  end
191
-
192
- case lang
193
- when nil #Undefined default, take actual locale or en
194
- return defined?( Locale ) ? Locale.current.language.to_sym : :en
195
- #This code require locale (or you get an error "uninitialized constant Date_locale::Locale")
196
- #when Locale::Object
197
- # return lang.language.to_sym
198
- else
199
- return lang.to_sym
51
+
52
+
53
+ =begin rdoc
54
+ Get the key for the wanted language.
55
+
56
+ If locale is given, it i tried to detect the current language.
57
+
58
+ Allows the usage (or not to use) the locale-gem[https://rubygems.org/gems/locale]
59
+ or i18n-gem[https://rubygems.org/gems/i18n].
60
+ =end
61
+ def self.get_language_key( locale = nil, default = 'en' )
62
+
63
+ if locale
64
+ #convert Locale to string
65
+ langkey = locale.to_s.downcase
66
+ else #Undefined default, take actual locale or en
67
+ langkey = default
68
+ if defined?( Locale )
69
+ langkey = Locale.current.to_s.downcase
70
+ elsif defined?( I18n )
71
+ langkey = I18n.locale.to_s.downcase
72
+ end
200
73
  end
201
- end
202
74
 
203
- #
204
- #strftime with the day- and month names in the selected language.
205
- #
206
- #Lang can be a language symbol or a locale.
207
- def strftime_locale(format = '%F', lang = nil )
208
-
209
- lang = Date_locale.get_language_key(lang)
210
-
211
- #Get the texts
212
- if DATE_TEXTS[lang]
213
- daynames = DATE_TEXTS[lang][:daynames]
214
- abbr_daynames = DATE_TEXTS[lang][:abbr_daynames]
215
- monthnames = DATE_TEXTS[lang][:monthnames]
216
- abbr_monthnames = DATE_TEXTS[lang][:abbr_monthnames]
217
- else
218
- raise "Missing Support for locale #{lang.inspect}"
219
- end
220
-
221
- #Make the original replacements, after....
222
- result = self.strftime_orig(
223
- #...you replaced the language dependent parts.
224
- format.gsub(/%([aAbB])/){|m|
225
- case $1
226
- when 'a'; abbr_daynames[self.wday]
227
- when 'A'; daynames[self.wday]
228
- when 'b'; abbr_monthnames[self.mon]
229
- when 'B'; monthnames[self.mon]
230
- else
231
- raise "Date#strftime: InputError"
232
- end
233
- }
234
- )
235
- if defined? @@encoding_converter
236
- @@encoding_converter.iconv(result)
237
- else
238
- result
75
+ #Convert language key to something like en_us, de_at...
76
+ langkey.sub!(/-/, '_')
77
+ #Use language if locale is not defined
78
+ if ! locale?(langkey) and langkey =~ /(.+)[-_](.+)/
79
+ langkey = $1
80
+ end
81
+
82
+ return langkey
83
+
239
84
  end
240
- end #strftime_locale(format = '%F', lang = :en )
241
85
 
86
+ #
87
+ #strftime with the day- and month names in the selected language.
88
+ #This method is used to redefine
89
+ #Date#strftime, Time#strftime and DateTime#strftime .
90
+ #
91
+ #language can be a Symbol, String or a Locale.
92
+ #It is checked with DateTools::Mixin.get_language_key .
93
+ #
94
+ def strftime_locale(format = '%F', language = nil )
95
+
96
+ lang = DateTools::Mixin.get_language_key(language)
97
+
98
+ #Get the texts
99
+ if DATE_TEXTS[lang]
100
+ daynames = DATE_TEXTS[lang]['date']['day_names']
101
+ abbr_daynames = DATE_TEXTS[lang]['date']['abbr_day_names']
102
+ monthnames = DATE_TEXTS[lang]['date']['month_names']
103
+ abbr_monthnames = DATE_TEXTS[lang]['date']['abbr_month_names']
104
+ else
105
+ raise "Missing Support for locale #{lang.inspect}"
106
+ end
107
+
108
+ #Make the original replacements, after....
109
+ result = self.strftime_orig(
110
+ #...you replaced the language dependent parts.
111
+ format.gsub(/%([aAbB])/){|m|
112
+ case $1
113
+ when 'a'; abbr_daynames[self.wday]
114
+ when 'A'; daynames[self.wday]
115
+ when 'b'; abbr_monthnames[self.mon]
116
+ when 'B'; monthnames[self.mon]
117
+ else
118
+ raise "Date#strftime: InputError"
119
+ end
120
+ }
121
+ )
122
+ if defined? @@default_encoding
123
+ result.encode(@@default_encoding)
124
+ else
125
+ result
126
+ end
127
+ end #strftime_locale(format = '%F', lang = :en )
128
+
129
+ end #module Mixin
242
130
  end #module Date_locale
243
131
 
244
132
  class Date
245
133
 
246
- include Date_locale
134
+ include DateTools::Mixin
247
135
  alias :strftime_orig :strftime
248
136
 
249
137
  #Redefine strftime with flexible daynames.
@@ -270,7 +158,7 @@ end
270
158
 
271
159
 
272
160
  class Time
273
- include Date_locale
161
+ include DateTools::Mixin
274
162
  alias :strftime_orig :strftime
275
163
  #Redefine strftime for locale versions.
276
164
  def strftime(format='%F', lang = nil )
@@ -285,16 +173,20 @@ end
285
173
  if __FILE__ == $0
286
174
  #~ require 'date_locale'
287
175
 
288
- d = Date.new(2009,10,21)
289
- puts d.strftime("de: %A {%a} {%A} {%W} %w ", :de ) #=> de: Mittwoch {Mi} {Mittwoch} {42} 3 (loc: en)
290
- puts d.strftime("en: %A {%a} {%A} {%W} %w ", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
176
+ p DateTools::Mixin.get_language_key('de-at')
177
+
178
+ #~ d = Date.new(2009,10,21)
179
+ #~ puts d.strftime("de: %A {%a} {%A} {%W} %w ", :de ) #=> de: Mittwoch {Mi} {Mittwoch} {42} 3 (loc: en)
180
+ #~ puts d.strftime("en: %A {%a} {%A} {%W} %w ", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
181
+
182
+ #~ puts "=======Load locale"
291
183
 
292
- puts "=======Load locale"
293
- require 'locale'
294
- Locale.current = 'de'
295
- puts d.strftime("#{Locale.current}: %A {%a} {%A} {%W} %w") #=> de: Mittwoch {Mi} {Mittwoch} {42} 3
296
- Locale.current = 'en'
297
- puts d.strftime("#{Locale.current}: %A {%a} {%A} {%W} %w") #=> en: Wednesday {Wed} {Wednesday} {42} 3
298
- puts d.strftime("de: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :de ) #=> de: Mittwoch {Mi} {Mittwoch} {42} 3 (loc: en)
299
- puts d.strftime("en: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
184
+ #~ require 'locale'
185
+ #~ Locale.current = 'de-de'
186
+ #~ puts d.strftime("#{Locale.current}: %A {%a} {%A} {%W} %w") #=> de: Mittwoch {Mi} {Mittwoch} {42} 3
187
+ #~ Locale.current = 'en'
188
+ #~ puts d.strftime("#{Locale.current}: %A {%a} {%A} {%W} %w") #=> en: Wednesday {Wed} {Wednesday} {42} 3
189
+ #~ puts d.strftime("de: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :de ) #=> de: Mittwoch {Mi} {Mittwoch} {42} 3 (loc: en)
190
+ #~ puts d.strftime("en: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
191
+ #~ puts d.strftime("en: %A {%a} {%A} {%W} %w (loc: #{Locale.current})", :en ) #=> en: Wednesday {Wed} {Wednesday} {42} 3 (loc: en)
300
192
  end #if __FILE__ == $0