@@ -21,6 +21,7 @@ module I18n
21
21
{% end % }
22
22
end
23
23
24
+ # Read files, normalize and merge all the translations
24
25
def load (* args)
25
26
if args[0 ].is_a?(String )
26
27
files = Dir .glob(args[0 ] + " /*.yml" )
@@ -67,9 +68,11 @@ module I18n
67
68
68
69
# Localize a number or a currency
69
70
# Use the format if given
70
- # scope can be one of :number ( default ), :currency
71
+ # scope can be one of `:number` ( default ), `:currency`
72
+ #
71
73
# Following keys are required :
72
74
#
75
+ # ```yaml
73
76
# __formats__:
74
77
# number:
75
78
# decimal_separator: ','
@@ -79,6 +82,7 @@ module I18n
79
82
# symbol: '€'
80
83
# name: 'euro'
81
84
# format: '%s€'
85
+ # ```
82
86
def localize (locale : String , object : Number , scope = :number , format = nil ) : String
83
87
return object.to_s if scope != :number && scope != :currency
84
88
@@ -90,36 +94,39 @@ module I18n
90
94
number
91
95
end
92
96
93
- # Localize a date or a datetime
94
- # Use the format if given
95
- # scope can be one of :time ( default ), :date, :datetime
96
- # Following keys are required :
97
+ # Localize a date or a datetime using the *format* if provided.
98
+ # *scope* can be one of :time ( default ), :date, :datetime
97
99
#
100
+ # NOTE: According to ISO 8601, Monday is the first day of the week
101
+ #
102
+ # Following keys are required :
103
+ # ```yaml
98
104
# __formats__:
99
105
# date:
100
106
# formats:
101
107
# default: "%Y-%m-%d"
102
108
# long: "%A, %d of %B %Y"
103
109
#
104
- # month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
105
- # abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
110
+ # month_names: [January, February, March, April, May, June, July, August, September, October, November, December]
111
+ # abbr_month_names: [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
106
112
#
107
- # day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
108
- # abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
113
+ # day_names: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday ]
114
+ # abbr_day_names: [Mon, Tue, Wed, Thu, Fri, Sat, Sun ]
109
115
#
110
116
# time:
111
117
# formats:
112
118
# default: "%I:%M:%S %p"
119
+ # ```
113
120
def localize (locale : String , object : Time , scope = :time , format = nil ) : String
114
121
base_key = " __formats__." + scope.to_s + (format ? " .formats." + format.to_s : " .formats.default" )
115
122
116
123
format = translate(locale, base_key)
117
124
format = format.to_s.gsub(/%[aAbBpP] / ) do |match |
118
125
case match
119
- when " %a" then translate(locale, " __formats__.date.abbr_day_names" , iter: object.day_of_week.to_i )
120
- when " %A" then translate(locale, " __formats__.date.day_names" , iter: object.day_of_week.to_i )
121
- when " %b" then translate(locale, " __formats__.date.abbr_month_names" , iter: object.month)
122
- when " %B" then translate(locale, " __formats__.date.month_names" , iter: object.month)
126
+ when " %a" then translate(locale, " __formats__.date.abbr_day_names" , iter: object.day_of_week.value - 1 )
127
+ when " %A" then translate(locale, " __formats__.date.day_names" , iter: object.day_of_week.value - 1 )
128
+ when " %b" then translate(locale, " __formats__.date.abbr_month_names" , iter: object.month - 1 )
129
+ when " %B" then translate(locale, " __formats__.date.month_names" , iter: object.month - 1 )
123
130
when " %p" then translate(locale, " __formats__.time.#{ object.hour < 12 ? :am : :pm } " ).upcase if object.responds_to? :hour
124
131
when " %P" then translate(locale, " __formats__.time.#{ object.hour < 12 ? :am : :pm } " ).downcase if object.responds_to? :hour
125
132
end
@@ -182,6 +189,7 @@ module I18n
182
189
end
183
190
end
184
191
192
+ # Flatten paths
185
193
def self.normalize (data : YAML ::Any , path : String = " " , final = Hash (String , YAML ::Any ).new)
186
194
data.as_h.keys.each do |k |
187
195
newp = path.size == 0 ? k.to_s : path + " ." + k.to_s
0 commit comments