matta-globalite 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,357 @@
1
+ locale_name: 台灣 (Taiwan)
2
+ # ActiveRecord::Errors.default_error_messages
3
+ error_message_inclusion: 不在指定的名單上
4
+ error_message_exclusion: 已在指定的名單上
5
+ error_message_invalid: 是錯誤的資料
6
+ error_message_confirmation: 與指定值不同
7
+ error_message_accepted: 必須要選擇接受指定的條款
8
+ error_message_empty: 不能不填
9
+ error_message_blank: 不能是空白
10
+ error_message_too_long: 太長 (最多 %d 個字元)
11
+ error_message_too_short: 太短 (最少 %d 個字元)
12
+ error_message_wrong_length: 長度不正確 (應該要 %d 個字元)
13
+ error_message_taken: 已經有人使用
14
+ error_message_not_a_number: 不是數字
15
+
16
+ # used by ActionView::Helpers::ActiveRecordHelper.error_messages_for
17
+ active_record_helper_header_message: 輸入資訊錯誤
18
+ active_record_helper_error_description: 請更正下列問題,
19
+
20
+ # used by ActionView::Helpers::DateHelper.distance_of_time_in_words
21
+ date_helper_less_than_x_seconds: 少於 %d 秒
22
+ date_helper_half_a_minute: 30 秒
23
+ date_helper_less_than_a_minute: 少於一分鐘
24
+ date_helper_one_minute: 一分鐘
25
+ date_helper_x_minutes: %d 分鐘
26
+ date_helper_one_hour: 約 1 小時
27
+ date_helper_x_hours: 約 %d 小時
28
+ date_helper_one_day: 1 天
29
+ date_helper_x_days: %d 天
30
+ date_helper_one_month: 1 個月
31
+ date_helper_x_months: %d 個月
32
+ date_helper_one_year: 1 年
33
+ date_helper_x_years: %d 年
34
+
35
+ # DON'T TRANSLATE, just order
36
+ date_helper_order: [:day, :month, :year] #default Rails is US ordered: :order => [:year, :month, :day]
37
+
38
+ date_helper_date_formats: # DON'T TRANSLATE, RE ORDER
39
+ default: "%Y/%m/%d"
40
+ short: "%b %e"
41
+ long: "%b %e, %Y"
42
+
43
+ date_helper_time_formats: # DON'T TRANSLATE, RE ORDER
44
+ default: "%a, %Y %b %d %H:%M:%S %z"
45
+ short: "%b %d, %H:%M"
46
+ long: "%Y/%b/%d %H:%M"
47
+
48
+ # added at the end of date if your date system doesn't use a 24 hours system
49
+ date_helper_am: am
50
+ date_helper_pm: pm
51
+
52
+ # used by ActionView::Helpers::NumberHelper.number_to_currency
53
+ number_helper_unit: "NT$"
54
+ number_helper_separator: "." #unit separator (between integer part and fraction part)
55
+ number_helper_delimiter: "," #delimiter between each group of thousands. Example: 1.234.567
56
+ number_helper_order: [ unit, number ]
57
+ #to support for instance French format, the order is different: Unit comes last (ex. "1.234,00 €")
58
+
59
+ # use by +Array#to_sentence()+
60
+ array_connector: '和'
61
+ array_skip_last_comma: 'true'
62
+
63
+ date_helper_month_names:
64
+ - # Leave that line
65
+ - # Leave that line
66
+ - 一月
67
+ - 二月
68
+ - 三月
69
+ - 四月
70
+ - 五月
71
+ - 六月
72
+ - 七月
73
+ - 八月
74
+ - 九月
75
+ - 十月
76
+ - 十一月
77
+ - 十二月
78
+
79
+ date_helper_abbr_month_names:
80
+ - # Leave that line
81
+ - # Leave that line
82
+ - 1月
83
+ - 2月
84
+ - 3月
85
+ - 4月
86
+ - 5月
87
+ - 6月
88
+ - 7月
89
+ - 8月
90
+ - 9月
91
+ - 10月
92
+ - 11月
93
+ - 12月
94
+
95
+ date_helper_day_names:
96
+ - 星期天
97
+ - 星期一
98
+ - 星期二
99
+ - 星期三
100
+ - 星期四
101
+ - 星期五
102
+ - 星期六
103
+
104
+ date_helper_abbr_day_names:
105
+ - 周日
106
+ - 周一
107
+ - 周二
108
+ - 周三
109
+ - 周四
110
+ - 周五
111
+ - 周六
112
+
113
+ countries_list:
114
+ - Afghanistan
115
+ - Albania
116
+ - Algeria
117
+ - American Samoa
118
+ - Andorra
119
+ - Angola
120
+ - Anguilla
121
+ - Antarctica
122
+ - Antigua And Barbuda
123
+ - Argentina
124
+ - Armenia
125
+ - Aruba
126
+ - Australia
127
+ - Austria
128
+ - Azerbaijan
129
+ - Bahamas
130
+ - Bahrain
131
+ - Bangladesh
132
+ - Barbados
133
+ - Belarus
134
+ - Belgium
135
+ - Belize
136
+ - Benin
137
+ - Bermuda
138
+ - Bhutan
139
+ - Bolivia
140
+ - Bosnia and Herzegowina
141
+ - Botswana
142
+ - Bouvet Island
143
+ - Brazil
144
+ - British Indian Ocean Territory
145
+ - Brunei Darussalam
146
+ - Bulgaria
147
+ - Burkina Faso
148
+ - Burma
149
+ - Burundi
150
+ - Cambodia
151
+ - Cameroon
152
+ - Canada
153
+ - Cape Verde
154
+ - Cayman Islands
155
+ - Central African Republic
156
+ - Chad
157
+ - Chile
158
+ - China
159
+ - Christmas Island
160
+ - Cocos (Keeling) Islands
161
+ - Colombia
162
+ - Comoros
163
+ - Congo
164
+ - Congo, the Democratic Republic of the
165
+ - Cook Islands
166
+ - Costa Rica
167
+ - Cote d'Ivoire
168
+ - Croatia
169
+ - Cuba
170
+ - Cyprus
171
+ - Czech Republic
172
+ - Denmark
173
+ - Djibouti
174
+ - Dominica
175
+ - Dominican Republic
176
+ - East Timor
177
+ - Ecuador
178
+ - Egypt
179
+ - El Salvador
180
+ - England
181
+ - Equatorial Guinea
182
+ - Eritrea
183
+ - Espana
184
+ - Estonia
185
+ - Ethiopia
186
+ - Falkland Islands
187
+ - Faroe Islands
188
+ - Fiji
189
+ - Finland
190
+ - France
191
+ - French Guiana
192
+ - French Polynesia
193
+ - French Southern Territories
194
+ - Gabon
195
+ - Gambia
196
+ - Georgia
197
+ - Germany
198
+ - Ghana
199
+ - Gibraltar
200
+ - Great Britain
201
+ - Greece
202
+ - Greenland
203
+ - Grenada
204
+ - Guadeloupe
205
+ - Guam
206
+ - Guatemala
207
+ - Guinea
208
+ - Guinea-Bissau
209
+ - Guyana
210
+ - Haiti
211
+ - Heard and Mc Donald Islands
212
+ - Honduras
213
+ - Hong Kong
214
+ - Hungary
215
+ - Iceland
216
+ - India
217
+ - Indonesia
218
+ - Ireland
219
+ - Israel
220
+ - Italy
221
+ - Iran
222
+ - Iraq
223
+ - Jamaica
224
+ - Japan
225
+ - Jordan
226
+ - Kazakhstan
227
+ - Kenya
228
+ - Kiribati
229
+ - Korea, Republic of
230
+ - Korea (South)
231
+ - Kuwait
232
+ - Kyrgyzstan
233
+ - Lao People's Democratic Republic
234
+ - Latvia
235
+ - Lebanon
236
+ - Lesotho
237
+ - Liberia
238
+ - Liechtenstein
239
+ - Lithuania
240
+ - Luxembourg
241
+ - Macau
242
+ - Macedonia
243
+ - Madagascar
244
+ - Malawi
245
+ - Malaysia
246
+ - Maldives
247
+ - Mali
248
+ - Malta
249
+ - Marshall Islands
250
+ - Martinique
251
+ - Mauritania
252
+ - Mauritius
253
+ - Mayotte
254
+ - Mexico
255
+ - Micronesia, Federated States of
256
+ - Moldova, Republic of
257
+ - Monaco
258
+ - Mongolia
259
+ - Montserrat
260
+ - Morocco
261
+ - Mozambique
262
+ - Myanmar
263
+ - Namibia
264
+ - Nauru
265
+ - Nepal
266
+ - Netherlands
267
+ - Netherlands Antilles
268
+ - New Caledonia
269
+ - New Zealand
270
+ - Nicaragua
271
+ - Niger
272
+ - Nigeria
273
+ - Niue
274
+ - Norfolk Island
275
+ - Northern Ireland
276
+ - Northern Mariana Islands
277
+ - Norway
278
+ - Oman
279
+ - Pakistan
280
+ - Palau
281
+ - Panama
282
+ - Papua New Guinea
283
+ - Paraguay
284
+ - Peru
285
+ - Philippines
286
+ - Pitcairn
287
+ - Poland
288
+ - Portugal
289
+ - Puerto Rico
290
+ - Qatar
291
+ - Reunion
292
+ - Romania
293
+ - Russia
294
+ - Rwanda
295
+ - Saint Kitts and Nevis
296
+ - Saint Lucia
297
+ - Saint Vincent and the Grenadines
298
+ - Samoa (Independent)
299
+ - San Marino
300
+ - Sao Tome and Principe
301
+ - Saudi Arabia
302
+ - Scotland
303
+ - Senegal
304
+ - Serbia and Montenegro
305
+ - Seychelles
306
+ - Sierra Leone
307
+ - Singapore
308
+ - Slovakia
309
+ - Slovenia
310
+ - Solomon Islands
311
+ - Somalia
312
+ - South Africa
313
+ - South Georgia and the South Sandwich Islands
314
+ - South Korea
315
+ - Spain
316
+ - Sri Lanka
317
+ - St. Helena
318
+ - St. Pierre and Miquelon
319
+ - Suriname
320
+ - Svalbard and Jan Mayen Islands
321
+ - Swaziland
322
+ - Sweden
323
+ - Switzerland
324
+ - Taiwan
325
+ - Tajikistan
326
+ - Tanzania
327
+ - Thailand
328
+ - Togo
329
+ - Tokelau
330
+ - Tonga
331
+ - Trinidad
332
+ - Trinidad and Tobago
333
+ - Tunisia
334
+ - Turkey
335
+ - Turkmenistan
336
+ - Turks and Caicos Islands
337
+ - Tuvalu
338
+ - Uganda
339
+ - Ukraine
340
+ - United Arab Emirates
341
+ - United Kingdom
342
+ - United States
343
+ - United States Minor Outlying Islands
344
+ - Uruguay
345
+ - Uzbekistan
346
+ - Vanuatu
347
+ - Vatican City State (Holy See)
348
+ - Venezuela
349
+ - Viet Nam
350
+ - Virgin Islands (British)
351
+ - Virgin Islands (U.S.)
352
+ - Wales
353
+ - Wallis and Futuna Islands
354
+ - Western Sahara
355
+ - Yemen
356
+ - Zambia
357
+ - Zimbabwe
data/lib/globalite.rb ADDED
@@ -0,0 +1,26 @@
1
+ # Add support for localization keys
2
+ require 'rails/core_ext'
3
+ require 'globalite/l10n'
4
+ require 'globalite/locale'
5
+ Symbol.send :include, SymbolExtension
6
+
7
+ module Globalite
8
+ extend L10n
9
+ def self.localize_rails
10
+ ActiveRecord::Errors.relocalize
11
+ end
12
+ end
13
+
14
+ # Localize Rails
15
+ require 'rails/localization.rb'
16
+ require 'rails/localized_action_view'
17
+ require 'rails/localized_active_record'
18
+
19
+ # added Boolean function to 'boolean' a string
20
+ module Kernel
21
+ def Boolean(string)
22
+ return true if string == true || string =~ /^true$/i
23
+ return false if string == false || string.nil? || string =~ /^false$/i
24
+ raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
25
+ end
26
+ end
@@ -0,0 +1,300 @@
1
+ module Globalite
2
+
3
+ module L10n
4
+
5
+ @@default_language = :en
6
+ attr_reader :default_language
7
+
8
+ @@default_country = :*
9
+ mattr_reader :default_country
10
+
11
+ @@reserved_keys = [ :limit ]
12
+ mattr_reader :reserved_keys
13
+
14
+ @@languages = []
15
+ def languages
16
+ @@languages
17
+ end
18
+
19
+ def default_language
20
+ @@default_language
21
+ end
22
+
23
+ @@countries = []
24
+ def countries
25
+ @@countries
26
+ end
27
+
28
+ @@locales = {}
29
+ def locales
30
+ @@locales.keys
31
+ end
32
+
33
+ @@rails_locales = {}
34
+ def rails_locales
35
+ @@rails_locales
36
+ end
37
+
38
+ @@ui_locales = {}
39
+ def ui_locales
40
+ @@ui_locales
41
+ end
42
+
43
+ @@current_language = nil
44
+ def current_language
45
+ @@current_language || default_language
46
+ end
47
+ alias :language :current_language
48
+
49
+ @@current_country = nil
50
+ def country
51
+ @@current_country || default_country
52
+ end
53
+ alias :current_country :country
54
+
55
+ def current_locale
56
+ "#{current_language}-#{current_country}".to_sym
57
+ end
58
+ alias :locale :current_locale
59
+
60
+ # Set the current language ( ISO 639-1 language code in lowercase letters)
61
+ # Usage:
62
+ # Globalite.language = 'fr' or Globalite.language = :Fr
63
+ # Will save the current language code if available, otherwise nada, switching back to the previous language
64
+ #
65
+ def language=(language)
66
+
67
+ language = language.to_s.downcase.to_sym if language.class == Symbol
68
+ language = language.downcase.to_sym if language.class == String && !language.empty?
69
+
70
+ if @@languages.include?(language)
71
+ @@current_language = language
72
+ if !@@locales.include?("#{language}-#{@@current_country}".to_sym)
73
+ @@current_country = :*
74
+ end
75
+ end
76
+
77
+ #Locale.update_session_locale
78
+ localize_rails
79
+ @@current_language
80
+ end
81
+ alias :current_language= :language=
82
+
83
+ # Set the current country code (ISO 3166 country code in uppercase letters)
84
+ # Usage:
85
+ # Globalite.country = 'US' or Globalite.country = :fr
86
+ # Will store the current country code if supported
87
+ # Will try to automatically find the language for your country
88
+ # If the country isn't unknown to the system, the country will be set as :*
89
+ #
90
+ def country=(country)
91
+ load_localization! if defined? RAILS_ENV && RAILS_ENV == 'development'
92
+ country = country.to_s.upcase.to_sym if country.class == Symbol
93
+ country = country.upcase.to_sym if country.class == String && !country.empty?
94
+
95
+ if @@locales.include?("#{current_language}-#{country}".to_sym)
96
+ @@current_country = country
97
+ elsif locales.each {|locale| locale =~ /[a-z][a-z]-#{country.to_s}/ }
98
+ locales.each do |key|
99
+ if key.to_s.include?(country.to_s)
100
+ @new_language = key.to_s.split('-')[0].downcase.to_sym
101
+ end
102
+ end
103
+ if @new_language && @@locales.include?("#{@new_language}-#{country}".to_sym)
104
+ @@current_language = @new_language
105
+ @@current_country = country
106
+ end
107
+ else
108
+ @@current_country = :*
109
+ end
110
+ #Locale.update_session_locale
111
+ @@current_country
112
+ end
113
+ alias :current_country= :country=
114
+
115
+ def locale=(locale)
116
+ Locale.set_code(locale)
117
+ end
118
+ alias :current_locale= :locale=
119
+
120
+ @@localization_sources = []
121
+ def add_localization_source(path)
122
+ @@localization_sources << path
123
+ load_localization!
124
+ end
125
+
126
+ def localization_sources
127
+ @@localization_sources
128
+ end
129
+
130
+ # List localizations for the current locale
131
+ def localizations
132
+ @@locales[Locale.code] || {}
133
+ end
134
+
135
+ # Return the translation for the key, a string can be passed to replaced a missing translation
136
+ def localize(key, error_msg='__localization_missing__', args={}, locale=nil)
137
+ return if reserved_keys.include? key
138
+
139
+ # Set a temporary Locale to support the localized_in method
140
+ #
141
+ unless locale.nil?
142
+ @original_locale = Locale.code
143
+ Locale.set_code(locale)
144
+ end
145
+ localized = localizations[key] || error_msg
146
+ # Get translations from another country but in the same language if Globalite can't find a translation for my locale
147
+ #
148
+ if localized == error_msg
149
+ locales.each do |t_locale|
150
+ if t_locale.to_s.include?("#{current_language.to_s}-") && t_locale != Locale.code
151
+ localized = @@locales[t_locale][key] || error_msg
152
+ end
153
+ end
154
+ end
155
+ localized = interpolate_string(localized.dup, args.dup) if localized.class == String && localized != error_msg
156
+
157
+ # let's handle pluralization if needed
158
+ # the translation must include pluralize{count, singular string} to be translated
159
+ # the translator can also pass the plural form if needed:
160
+ # pluralize{3, goose, geese}
161
+ localized = localized.gsub( /pluralize\{(.*)\}/){ |erb| pluralize(Regexp.last_match(1)) } if localized.is_a?(String) && (localized=~ /pluralize\{(.*)\}/)
162
+
163
+ # Set the locale back to normal
164
+ #
165
+ unless locale.nil?
166
+ Locale.code = @original_locale
167
+ end
168
+
169
+ return localized
170
+ end
171
+ alias :loc :localize
172
+
173
+ def localize_with_args(key, args={})
174
+ localize(key, '_localization missing_', args)
175
+ end
176
+ alias :l_with_args :localize_with_args
177
+
178
+ def add_reserved_key(*key)
179
+ (@@reserved_keys += key.flatten).uniq!
180
+ end
181
+ alias :add_reserved_keys :add_reserved_key
182
+
183
+ # modified version of the Rails pluralize method from ActionView::Helpers::TextHelper module
184
+ # TODO: load custom inflector based on the language one uses.
185
+ def pluralize(l_string) #count, singular, plural = nil)
186
+ # map the arguments like in the original pluralize method
187
+ count, singular, plural = l_string.split(',').map{ |arg| arg.strip}
188
+
189
+ "#{count} " + if count == 1 || count == '1'
190
+ singular
191
+ elsif plural
192
+ plural
193
+ elsif Object.const_defined?("Inflector")
194
+ Inflector.pluralize(singular)
195
+ else
196
+ singular + "s"
197
+ end
198
+ end
199
+
200
+ def reset_l10n_data
201
+ @@languages = []
202
+ @@countries = []
203
+ @@locales = {}
204
+ @@rails_locales = {}
205
+ @@ui_locales = {}
206
+ end
207
+
208
+ # Loads ALL the UI localization in memory, I might want to refactor this later on.
209
+ # (can be hard on the memory if you load 25 languages with 900 entries each)
210
+ def load_localization!
211
+ reset_l10n_data
212
+
213
+ # Load the rails localization
214
+ if rails_localization_files
215
+ rails_localization_files.each do |file|
216
+ lang = File.basename(file, '.*')[0,2].downcase.to_sym
217
+ # if a country is defined
218
+ if File.basename(file, '.*')[3,5]
219
+ country = File.basename(file, '.*')[3,5].upcase.to_sym
220
+ @@countries << country if ( country != :* && !@@countries.include?(country) )
221
+ if locales.include?("#{lang}-#{country}".to_sym)
222
+ @@locales["#{lang}-#{country}".to_sym].merge(YAML.load_file(file).symbolize_keys)
223
+ else
224
+ @@locales["#{lang}-#{country}".to_sym] = YAML.load_file(file).symbolize_keys
225
+ @@rails_locales[locale_name("#{lang}-#{country}")] = "#{lang}-#{country}".to_sym
226
+ end
227
+ @@languages << lang unless @@languages.include? lang
228
+ else
229
+ @@languages << lang unless @@languages.include? lang
230
+ @f_locale = "#{lang}-*".to_sym
231
+ @@locales[@f_locale] = @@locales[@f_locale].merge(YAML.load_file(file).symbolize_keys) if locales.include?(@f_locale)
232
+ @@locales[@f_locale] = YAML.load_file(file).symbolize_keys unless locales.include?(@f_locale)
233
+ end
234
+ end
235
+ end
236
+ alias :load_translations! :load_localization!
237
+ alias :load_localizations! :load_localization!
238
+
239
+ # Load the UI localization
240
+ if ui_localization_files
241
+ ui_localization_files.each do |file|
242
+ lang = File.basename(file, '.*')[0,2].downcase.to_sym
243
+ if File.basename(file, '.*')[3,5]
244
+ country = File.basename(file, '.*')[3,5].upcase.to_sym
245
+ else
246
+ country = '*'.to_sym
247
+ end
248
+ @@languages << lang unless @@languages.include? lang
249
+ @@countries << country if ( country != :* && !@@countries.include?(country) )
250
+ @file_locale = "#{lang}-#{country}".to_sym
251
+ if locales.include?(@file_locale)
252
+ @@locales[@file_locale] = @@locales[@file_locale].merge(YAML.load_file(file).symbolize_keys)
253
+ @@ui_locales[locale_name("#{lang}-#{country}")] = "#{lang}-#{country}".to_sym
254
+ else
255
+ @@locales[@file_locale] = YAML.load_file(file).symbolize_keys
256
+ @@ui_locales[locale_name("#{lang}-#{country}")] = "#{lang}-#{country}".to_sym
257
+ end
258
+ end
259
+ end
260
+ localize_rails
261
+ # Return the path of the localization files
262
+ return "#{ui_localization_files} | #{rails_localization_files}".to_s
263
+ end
264
+
265
+ def locale_name(locale_code)
266
+ locale_code = locale_code.to_sym
267
+ if locales.include?(locale_code)
268
+ @@locales[locale_code][:locale_name] || nil
269
+ end
270
+ end
271
+
272
+ protected
273
+ # Return the list of UI files used by Globalite
274
+ def ui_localization_files
275
+ loc_files = Dir[File.join(RAILS_ROOT, 'lang/ui/', '*.{yml,yaml}')]
276
+ unless @@localization_sources.empty?
277
+ @@localization_sources.each do |path|
278
+ loc_files += Dir[File.join(path, '*.{yml,yaml}')]
279
+ end
280
+ end
281
+ loc_files
282
+ end
283
+
284
+ # Return a list of the Rails localization files
285
+ def rails_localization_files
286
+ loc_files = Dir[File.join( RAILS_ROOT, '/vendor/plugins/globalite/lang/rails/', '*.{yml,yaml}')]
287
+ end
288
+
289
+ # Interpolate a string using the passed arguments
290
+ def interpolate_string(string, args={})
291
+ if args.length > 0
292
+ args.each do |arg|
293
+ string = string.gsub("{#{arg[0].to_s}}", arg[1].to_s)
294
+ end
295
+ end
296
+ string
297
+ end
298
+
299
+ end
300
+ end