locale 2.0.1 → 2.0.2

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.
data/ChangeLog CHANGED
@@ -1,3 +1,19 @@
1
+ = locale-2.0.2 (2009-05-04)
2
+ * Add Locale.set_app_language_tags to restrict the target locales whole the application. [Suggested by Vladimir Dobriakov]
3
+ * locale/driver/cgi.rb: Add Locale.set_request.
4
+ * sample/rack: Add a sample for rack.
5
+ * Improve documents.
6
+ * Fix bugs
7
+ * Illegular -> Irregular [by Denis Defreyne]
8
+ * test_thread.rb doesn't work under the environment where LC_ALL is set .
9
+ [Reported by Hans de Graaff (Bug#24831)]
10
+ * Fixed Locale::Drive::CGI.charset don't work when accept_charset is nil.
11
+ [Reported by hallelujah (Bug#25583)]
12
+ * Fixed Locale::Drive::Win32.charset don't work.
13
+
14
+ Thanks to:
15
+ Denis Defreyne, Hans de Graaff, hallelujah, Vladimir Dobriakov
16
+
1
17
  = locale-2.0.1 (2009-04-18)
2
18
  * Locale::Tag::Common#extensions are shown order by the keys.[reported by
3
19
  Dan Coutu]
@@ -36,15 +36,23 @@ handle major locale ID standards.
36
36
  Download tar-ball from http://rubyforge.org/projects/locale/
37
37
  De-Compress archive and enter its top directory.
38
38
  Then type:
39
- ($ su)
40
- # ruby setup.rb
39
+ ($ su)
40
+ # ruby setup.rb
41
41
 
42
42
  == The simplest usage
43
43
 
44
44
  require 'rubygems'
45
45
  require 'locale'
46
46
 
47
- p Locale.current
47
+ p Locale.candidates
48
+
49
+ == APIs
50
+ The most important APIs are defined in Locale module.
51
+
52
+ * Locale.candidates - Returns the current locale candidates.
53
+ * Locale.current= - Sets the current locale(in a thread).
54
+ * Locale.default= - Sets the default locale(in the whole program).
55
+ * Locale.set_app_language_tags - Sets the locale that is supported by the App.
48
56
 
49
57
  == License
50
58
  This program is licenced under the same licence as Ruby.
@@ -65,13 +73,13 @@ This program is licenced under the same licence as Ruby.
65
73
  == References
66
74
  === Other libraries
67
75
  * langtag-0.1.0
68
- by Martin Dürst <http://rubyforge.org/projects/langtag/>
76
+ * by Martin Dürst <http://rubyforge.org/projects/langtag/>
69
77
 
70
78
  * memoizable.rb
71
- from ActiveSupport-2.2.0 <http://rubyforge.org/projects/activesupport/>
79
+ * from ActiveSupport-2.2.0 <http://rubyforge.org/projects/activesupport/>
72
80
 
73
81
  * Ruby-GetText-Package-1.92.0
74
- by Masao Mutoh <http://www.yotabanana.com/hiki/ruby-gettext.html>
82
+ * by Masao Mutoh <http://www.yotabanana.com/hiki/ruby-gettext.html>
75
83
 
76
84
  === Documents
77
85
  * The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004 Edition
data/Rakefile CHANGED
@@ -28,9 +28,11 @@ Rake::RDocTask.new { |rdoc|
28
28
  end
29
29
  rdoc.rdoc_dir = 'doc'
30
30
  rdoc.title = "Ruby-Locale library"
31
- rdoc.options << '--line-numbers' << '--inline-source'
32
- rdoc.rdoc_files.include('README.rdoc', 'ChangeLog')
33
- rdoc.rdoc_files.include('lib/**/*.rb')
31
+ rdoc.options << "--line-numbers" << "--inline-source" <<
32
+ "--accessor" << "cattr_accessor=object" << "--charset" << "utf-8"
33
+ rdoc.rdoc_files.include('README.rdoc')
34
+ rdoc.rdoc_files.include('ChangeLog')
35
+ rdoc.rdoc_files.add('lib')
34
36
  rdoc.template = allison if allison.size > 0
35
37
  }
36
38
 
@@ -1,7 +1,7 @@
1
1
  =begin
2
2
  locale.rb - Locale module
3
3
 
4
- Copyright (C) 2002-2008 Masao Mutoh
4
+ Copyright (C) 2002-2009 Masao Mutoh
5
5
 
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
@@ -17,23 +17,50 @@ require 'locale/taglist'
17
17
  require 'locale/version'
18
18
 
19
19
  # Locale module manages the locale informations of the application.
20
+ # These functions are the most important APIs in this library.
21
+ # Almost of all i18n/l10n programs use this APIs only.
20
22
  module Locale
21
23
  @@default_tag = nil
22
24
  @@locale_driver_module = nil
23
25
 
26
+ ROOT = File.dirname(__FILE__)
27
+
24
28
  include Locale::Util::Memoizable
25
29
 
26
30
  module_function
31
+ def require_driver(name) #:nodoc:
32
+ require File.join(ROOT, "locale/driver", name.to_s)
33
+ end
34
+
35
+ def create_language_tag(tag) #:nodoc:
36
+ if tag
37
+ if tag.kind_of? Locale::Tag::Simple
38
+ tag
39
+ else
40
+ Locale::Tag.parse(tag)
41
+ end
42
+ else
43
+ nil
44
+ end
45
+ end
46
+
27
47
  # Initialize Locale library.
28
48
  # Usually, you don't need to call this directly, because
29
49
  # this is called when Locale's methods are called.
30
- # If you need to specify the option, call this once first.
31
- # (But Almost of all case, you don't need this, because the
32
- # framework/library such as gettext calls this.)
33
50
  #
34
- # If you use this library with CGI(and not use frameworks/gettext),
51
+ # If you use this library with CGI or the kind of CGI.
35
52
  # You need to call Locale.init(:driver => :cgi).
36
53
  #
54
+ # ==== For Framework designers/programers:
55
+ # If your framework is for WWW, call this once like: Locale.init(:driver => :cgi).
56
+ #
57
+ # ==== To Application programers:
58
+ # If your framework doesn't use ruby-locale and the application is for WWW,
59
+ # call this once like: Locale.init(:driver => :cgi).
60
+ #
61
+ # ==== To Library authors:
62
+ # Don't call this, even if your application is only for WWW.
63
+ #
37
64
  # * opts: Options as a Hash.
38
65
  # * :driver - The driver. :cgi if you use Locale module with CGI,
39
66
  # nil if you use system locale.
@@ -41,14 +68,14 @@ module Locale
41
68
  #
42
69
  def init(opts = {})
43
70
  if opts[:driver]
44
- require "locale/driver/#{opts[:driver]}"
71
+ require_driver opts[:driver]
45
72
  else
46
73
  if /cygwin|mingw|win32/ =~ RUBY_PLATFORM
47
- require 'locale/driver/win32'
74
+ require_driver 'win32'
48
75
  elsif /java/ =~ RUBY_PLATFORM
49
- require 'locale/driver/jruby'
76
+ require_driver 'jruby'
50
77
  else
51
- require 'locale/driver/posix'
78
+ require_driver 'posix'
52
79
  end
53
80
  end
54
81
  end
@@ -58,33 +85,26 @@ module Locale
58
85
  # Usually you don't need to call this method.
59
86
  #
60
87
  # * Returns: the driver module.
61
- def driver_module
88
+ def driver_module
62
89
  unless @@locale_driver_module
63
90
  Locale.init
64
91
  end
65
92
  @@locale_driver_module
66
93
  end
67
94
 
95
+ DEFAULT_LANGUAGE_TAG = Locale::Tag::Simple.new("en") #:nodoc:
96
+
68
97
  # Sets the default locale as the language tag
69
98
  # (Locale::Tag's class or String(such as "ja_JP")).
70
99
  #
71
100
  # * tag: the default language_tag
72
101
  # * Returns: self.
73
102
  def set_default(tag)
74
- default_tag = nil
75
103
  Thread.list.each do |thread|
76
104
  thread[:current_languages] = nil
77
- thread[:candidates_caches] = {}
78
- end
79
-
80
- if tag
81
- if tag.kind_of? Locale::Tag::Simple
82
- default_tag = tag
83
- else
84
- default_tag = Locale::Tag.parse(tag)
85
- end
105
+ thread[:candidates_caches] = nil
86
106
  end
87
- @@default_tag = default_tag
107
+ @@default_tag = create_language_tag(tag)
88
108
  self
89
109
  end
90
110
 
@@ -103,18 +123,19 @@ module Locale
103
123
  #
104
124
  # * Returns: the default locale (Locale::Tag's class).
105
125
  def default
106
- @@default_tag
126
+ @@default_tag || DEFAULT_LANGUAGE_TAG
107
127
  end
108
128
 
109
129
  # Sets the locales of the current thread order by the priority.
110
130
  # Each thread has a current locales.
111
- # The default locale/system locale is used if the thread doesn't have current locales.
131
+ # The system locale/default locale is used if the thread doesn't have current locales.
112
132
  #
113
133
  # * tag: Locale::Language::Tag's class or the language tag as a String. nil if you need to
114
- # clear current locales.
134
+ # clear current locales.
115
135
  # * charset: the charset (override the charset even if the locale name has charset) or nil.
116
136
  # * Returns: self
117
137
  #
138
+ # (e.g.)
118
139
  # Locale.set_current("ja_JP.eucJP")
119
140
  # Locale.set_current("ja-JP")
120
141
  # Locale.set_current("en_AU", "en_US", ...)
@@ -124,15 +145,11 @@ module Locale
124
145
  if tags[0]
125
146
  languages = Locale::TagList.new
126
147
  tags.each do |tag|
127
- if tag.kind_of? Locale::Tag::Simple
128
- languages << tag
129
- else
130
- languages << Locale::Tag.parse(tag)
131
- end
148
+ languages << create_language_tag(tag)
132
149
  end
133
150
  end
134
151
  Thread.current[:current_languages] = languages
135
- Thread.current[:candidates_caches] = {}
152
+ Thread.current[:candidates_caches] = nil
136
153
  self
137
154
  end
138
155
 
@@ -149,11 +166,18 @@ module Locale
149
166
  end
150
167
 
151
168
  # Gets the current locales (Locale::Tag's class).
169
+ # If the current locale is not set, this returns system/default locale.
170
+ #
171
+ # This method returns the current language tags even if it isn't included in app_language_tags.
172
+ #
173
+ # Usually, the programs should use Locale.candidates to find the correct locale, not this method.
152
174
  #
153
- # If the current locale is not set, this returns default/system locale.
154
175
  # * Returns: an Array of the current locales (Locale::Tag's class).
155
176
  def current
156
- Thread.current[:current_languages] ||= (default ? Locale::TagList.new([default]) : driver_module.locales)
177
+ unless Thread.current[:current_languages]
178
+ loc = driver_module.locales
179
+ Thread.current[:current_languages] = loc ? loc : Locale::TagList.new([default])
180
+ end
157
181
  Thread.current[:current_languages]
158
182
  end
159
183
 
@@ -168,21 +192,25 @@ module Locale
168
192
  end
169
193
 
170
194
  # Returns the language tags which are variations of the current locales order by priority.
195
+ #
171
196
  # For example, if the current locales are ["fr", "ja_JP", "en_US", "en-Latn-GB-VARIANT"],
172
197
  # then returns ["fr", "ja_JP", "en_US", "en-Latn-GB-VARIANT", "en_Latn_GB", "en_GB", "ja", "en"].
173
- # "en" is the default locale. It's added at the end of the list even if it isn't exist.
198
+ # "en" is the default locale(You can change it using set_default).
199
+ # The default locale is added at the end of the list even if it isn't exist.
200
+ #
174
201
  # Usually, this method is used to find the locale data as the path(or a kind of IDs).
175
202
  # * options: options as a Hash or nil.
176
- # * :supported_language_tags: an Array of the language tags order by the priority. This option
177
- # filters the locales which are supported by the library/application.
178
- # Default is nil if you don't need to filter the locales.
179
- # * (e.g.1) ["fr_FR", "en_GB", "en_US", ...], (e.g.2) ["fr-FR", "en-GB", "en-US", ...]
180
- # * :type: the type of language tag. :common, :rfc, :cldr, :posix and
181
- # :simple are available. Default value is :common
182
- # * :default_language_tags: the default languages as an Array. Default value is ["en"].
203
+ # * :supported_language_tags -
204
+ # An Array of the language tags order by the priority. This option
205
+ # restricts the locales which are supported by the library/application.
206
+ # Default is nil if you don't need to restrict the locales.
207
+ # (e.g.1) ["fr_FR", "en_GB", "en_US", ...]
208
+ # * :type -
209
+ # The type of language tag. :common, :rfc, :cldr, :posix and
210
+ # :simple are available. Default value is :common
183
211
  def candidates(options = {})
184
- opts = {:supported_language_tags => nil, :type => :common,
185
- :default_language_tags => ["en"]}.merge(options)
212
+ opts = {:supported_language_tags => nil, :current => current,
213
+ :type => :common}.merge(options)
186
214
 
187
215
  if Thread.current[:candidates_caches]
188
216
  cache = Thread.current[:candidates_caches][opts.hash]
@@ -191,31 +219,44 @@ module Locale
191
219
  Thread.current[:candidates_caches] = {}
192
220
  end
193
221
  Thread.current[:candidates_caches][opts.hash] =
194
- collect_candidates(opts[:type], current,
195
- opts[:default_language_tags],
222
+ collect_candidates(opts[:type], opts[:current],
196
223
  opts[:supported_language_tags])
197
224
  end
198
225
 
199
226
  # collect tag candidates and memoize it.
200
227
  # The result is shared from all threads.
201
- def collect_candidates(type, tags, default_tags, supported_tags) # :nodoc:
202
- default_language_tags = default_tags.collect{|v|
203
- Locale::Tag.parse(v).send("to_#{type}")}.flatten.uniq
204
-
228
+ def collect_candidates(type, tags, supported_tags) # :nodoc:
205
229
  candidate_tags = tags.collect{|v| v.send("to_#{type}").candidates}
230
+ default_tags = default.send("to_#{type}").candidates
231
+ if app_language_tags
232
+ app_tags = app_language_tags.collect{|v| v.send("to_#{type}")}.flatten.uniq
233
+ end
234
+ if supported_tags
235
+ supported_tags = supported_tags.collect{|v| Locale::Tag.parse(v).send("to_#{type}")}.flatten.uniq
236
+ end
206
237
 
207
238
  tags = []
208
239
  (0...candidate_tags[0].size).each {|i|
209
240
  tags += candidate_tags.collect{|v| v[i]}
210
241
  }
211
- tags += default_language_tags
242
+ tags += default_tags
212
243
  tags.uniq!
213
244
 
214
- if supported_tags
215
- tags &= supported_tags.collect{|v|
216
- Locale::Tag.parse(v).send("to_#{type}")}.flatten
217
- tags = default_language_tags if tags.size == 0
245
+ all_tags = nil
246
+ if app_tags
247
+ if supported_tags
248
+ all_tags = app_tags & supported_tags
249
+ else
250
+ all_tags = app_tags
251
+ end
252
+ elsif supported_tags
253
+ all_tags = supported_tags
218
254
  end
255
+ if all_tags
256
+ tags &= all_tags
257
+ tags = default_tags.uniq if tags.size == 0
258
+ end
259
+
219
260
  Locale::TagList.new(tags)
220
261
  end
221
262
  memoize :collect_candidates
@@ -227,7 +268,7 @@ module Locale
227
268
  #
228
269
  # * Returns: the current charset.
229
270
  def charset
230
- driver_module.charset
271
+ driver_module.charset || "UTF-8"
231
272
  end
232
273
  memoize :charset
233
274
 
@@ -235,19 +276,54 @@ module Locale
235
276
  # * Returns: self
236
277
  def clear
237
278
  Thread.current[:current_languages] = nil
238
- Thread.current[:candidates_caches] = {}
279
+ Thread.current[:candidates_caches] = nil
239
280
  self
240
281
  end
241
282
 
242
283
  # Clear all locales and charsets of all threads.
243
- # This doesn't clear the default locale.
284
+ # This doesn't clear the default and app_language_tags.
244
285
  # Use Locale.default = nil to unset the default locale.
245
286
  # * Returns: self
246
287
  def clear_all
247
288
  Thread.list.each do |thread|
248
289
  thread[:current_languages] = nil
249
- thread[:candidates_caches] = {}
290
+ thread[:candidates_caches] = nil
291
+ end
292
+ memoize_clear
293
+ self
294
+ end
295
+
296
+ @@app_language_tags = nil
297
+ # Set the language tags which is supported by the Application.
298
+ # This value is same with supported_language_tags in Locale.candidates
299
+ # to restrict the result but is the global setting.
300
+ # If you set a language tag, the application works as the single locale
301
+ # application.
302
+ #
303
+ # If the current locale is not included in app_language_tags,
304
+ # Locale.default value is used.
305
+ # Use Locale.set_default() to set correct language
306
+ # if "en" is not included in the language tags.
307
+ #
308
+ # Set nil if clear the value.
309
+ #
310
+ # Note that the libraries/plugins shouldn't set this value.
311
+ #
312
+ # (e.g.) Locale.set_app_language_tags("fr_FR", "en-GB", "en_US", ...)
313
+ def set_app_language_tags(*tags)
314
+ if tags[0]
315
+ @@app_language_tags = tags.collect{|v| Locale::Tag.parse(v)}
316
+ else
317
+ @@app_language_tags = nil
250
318
  end
319
+
320
+ clear_all
251
321
  self
252
322
  end
323
+
324
+ # Returns the app_language_tags. Default is nil. See set_app_language_tags for more details.
325
+ def app_language_tags
326
+ @@app_language_tags
327
+ end
328
+
253
329
  end
@@ -19,9 +19,6 @@ module Locale
19
19
  module CGI
20
20
  $stderr.puts self.name + " is loaded." if $DEBUG
21
21
 
22
- @@default_locale = Locale::Tag::Simple.new("en")
23
- @@default_charset = "UTF-8"
24
-
25
22
  module_function
26
23
  # Gets required locales from CGI parameters. (Based on RFC2616)
27
24
  #
@@ -29,14 +26,13 @@ module Locale
29
26
  # (QUERY_STRING "lang" > COOKIE "lang" > HTTP_ACCEPT_LANGUAGE > "en")
30
27
  #
31
28
  def locales
32
- return Locale::TagList.new([@@default_locale]) unless cgi
33
- cgi_ = cgi
29
+ req = Thread.current[:current_request]
30
+ return nil unless req
34
31
 
35
- locales = Locale::TagList.new
32
+ locales = []
36
33
 
37
34
  # QUERY_STRING "lang"
38
- langs = cgi_.params["lang"]
39
- if langs
35
+ if langs = req[:query_langs]
40
36
  langs.each do |lang|
41
37
  locales << Locale::Tag.parse(lang)
42
38
  end
@@ -44,8 +40,7 @@ module Locale
44
40
 
45
41
  unless locales.size > 0
46
42
  # COOKIE "lang"
47
- langs = cgi_.cookies["lang"]
48
- if langs
43
+ if langs = req[:cookie_langs]
49
44
  langs.each do |lang|
50
45
  locales << Locale::Tag.parse(lang) if lang.size > 0
51
46
  end
@@ -54,77 +49,88 @@ module Locale
54
49
 
55
50
  unless locales.size > 0
56
51
  # HTTP_ACCEPT_LANGUAGE
57
- if lang = cgi_.accept_language and lang.size > 0
52
+ if lang = req[:accept_language] and lang.size > 0
58
53
  locales += lang.gsub(/\s/, "").split(/,/).map{|v| v.split(";q=")}.map{|j| [j[0], j[1] ? j[1].to_f : 1.0]}.sort{|a,b| -(a[1] <=> b[1])}.map{|v| Locale::Tag.parse(v[0])}
59
54
  end
60
55
  end
61
56
 
62
- unless locales.size > 0
63
- locales << @@default_locale
64
- end
65
- Locale::TagList.new(locales.uniq)
57
+ locales.size > 0 ? Locale::TagList.new(locales.uniq) : nil
66
58
  end
67
59
 
68
60
  # Gets the charset from CGI parameters. (Based on RFC2616)
69
- # * Returns: the charset (HTTP_ACCEPT_CHARSET > "UTF-8").
61
+ # * Returns: the charset (HTTP_ACCEPT_CHARSET or nil).
70
62
  def charset
71
- cgi_ = cgi
72
- charsets = cgi_.accept_charset
63
+ req = Thread.current[:current_request]
64
+ return nil unless req
65
+
66
+ charsets = req[:accept_charset]
73
67
  if charsets and charsets.size > 0
74
68
  num = charsets.index(',')
75
69
  charset = num ? charsets[0, num] : charsets
76
- charset = @@default_charset if charset == "*"
70
+ charset = nil if charset == "*"
77
71
  else
78
- charset = @@default_charset
72
+ charset = nil
79
73
  end
80
74
  charset
81
75
  end
82
76
 
83
- # Sets a CGI object.
84
- # * cgi_: CGI object
85
- # * Returns: self
86
- def set_cgi(cgi_)
87
- Thread.current[:current_cgi] = cgi_
88
- self
89
- end
90
-
91
- def cgi #:nodoc:
92
- Thread.current[:current_cgi]
93
- end
77
+ # Set a request.
78
+ #
79
+ # * query_langs: An Array of QUERY_STRING value "lang".
80
+ # * cookie_langs: An Array of cookie value "lang".
81
+ # * accept_language: The value of HTTP_ACCEPT_LANGUAGE
82
+ # * accept_charset: The value of HTTP_ACCEPT_CHARSET
83
+ def set_request(query_langs, cookie_langs, accept_language, accept_charset)
84
+ Thread.current[:current_request] = {
85
+ :query_langs => query_langs,
86
+ :cookie_langs => cookie_langs,
87
+ :accept_language => accept_language,
88
+ :accept_charset => accept_charset
89
+ }
90
+ self
91
+ end
92
+
93
+ # Clear the current request.
94
+ def clear_current_request
95
+ Thread.current[:current_request] = nil
96
+ end
94
97
  end
95
98
  end
96
99
 
97
100
  @@locale_driver_module = Driver::CGI
98
101
 
99
102
  module_function
100
- # Sets a CGI object.
101
- #
102
- # Call Locale.init(:driver => :cgi) first.
103
+ # Sets a request values for lang/charset.
103
104
  #
104
- # * cgi_: CGI object
105
- # * Returns: self
106
- def set_cgi(cgi_)
107
- @@locale_driver_module.set_cgi(cgi_)
105
+ # * query_langs: An Array of QUERY_STRING value "lang".
106
+ # * cookie_langs: An Array of cookie value "lang".
107
+ # * accept_language: The value of HTTP_ACCEPT_LANGUAGE
108
+ # * accept_charset: The value of HTTP_ACCEPT_CHARSET
109
+ def set_request(query_langs, cookie_langs, accept_language, accept_charset)
110
+ @@locale_driver_module.set_request(query_langs, cookie_langs, accept_language, accept_charset)
108
111
  self
109
112
  end
110
-
111
- # Sets a CGI object.
113
+
114
+ # Sets a CGI object. This is the convenient function of set_request().
112
115
  #
113
- # Call Locale.init(:driver => :cgi) first.
116
+ # This method is appeared when Locale.init(:driver => :cgi) is called.
114
117
  #
115
- # * cgi_: CGI object
116
- # * Returns: cgi_
117
- def cgi=(cgi_)
118
- set_cgi(cgi_)
119
- cgi_
118
+ # * cgi: CGI object
119
+ # * Returns: self
120
+ def set_cgi(cgi)
121
+ set_request(cgi.params["lang"], cgi.cookies["lang"],
122
+ cgi.accept_language, cgi.accept_charset)
123
+ self
120
124
  end
121
125
 
122
- # Gets the CGI object. If it is nil, returns new CGI object.
126
+ # Sets a CGI object.This is the convenient function of set_request().
123
127
  #
124
- # Call Locale.init(:driver => :cgi) first.
128
+ # This method is appeared when Locale.init(:driver => :cgi) is called.
125
129
  #
126
- # * Returns: the CGI object
127
- def cgi
128
- @@locale_driver_module.cgi
130
+ # * cgi: CGI object
131
+ # * Returns: cgi
132
+ def cgi=(cgi)
133
+ set_cgi(cgi)
134
+ cgi
129
135
  end
130
136
  end
@@ -11,7 +11,7 @@
11
11
  $Id: jruby.rb 27 2008-12-03 15:06:50Z mutoh $
12
12
  =end
13
13
 
14
- require 'locale/driver/env'
14
+ require File.join(File.dirname(__FILE__), 'env')
15
15
  require 'java'
16
16
 
17
17
  module Locale
@@ -33,9 +33,14 @@ module Locale
33
33
  locales = ::Locale::Driver::Env.locales
34
34
  unless locales
35
35
  locale = java.util.Locale.getDefault
36
+ variant = Locale.getVariant
37
+ variants = []
38
+ if valiant != nil and variant.size > 0
39
+ valiants = [valiant]
40
+ end
36
41
  locales = TagList.new([Locale::Tag::Common.new(locale.getLanguage, nil,
37
42
  locale.getCountry,
38
- [locale.getVariant])])
43
+ variants)])
39
44
  end
40
45
  locales
41
46
  end
@@ -11,7 +11,7 @@
11
11
  $Id: posix.rb 27 2008-12-03 15:06:50Z mutoh $
12
12
  =end
13
13
 
14
- require 'locale/driver/env'
14
+ require File.join(File.dirname(__FILE__), 'env')
15
15
 
16
16
  module Locale
17
17
  # Locale::Driver::Posix module for Posix OS (Unix)
@@ -30,14 +30,14 @@ module Locale
30
30
  end
31
31
 
32
32
  # Gets the charset from environment variable or the result of
33
- # "locale charmap".
33
+ # "locale charmap" or nil.
34
34
  # * Returns: the system charset.
35
35
  def charset
36
36
  charset = ::Locale::Driver::Env.charset
37
37
  unless charset
38
38
  charset = `locale charmap`.strip
39
39
  unless $? && $?.success?
40
- charset = "UTF-8"
40
+ charset = nil
41
41
  end
42
42
  end
43
43
  charset
@@ -11,8 +11,8 @@
11
11
  $Id: win32.rb 27 2008-12-03 15:06:50Z mutoh $
12
12
  =end
13
13
 
14
- require 'locale/driver/env'
15
- require 'locale/driver/win32_table'
14
+ require File.join(File.dirname(__FILE__), 'env')
15
+ require File.join(File.dirname(__FILE__), 'win32_table')
16
16
  require 'dl/win32'
17
17
 
18
18
 
@@ -34,9 +34,10 @@ module Locale
34
34
  def charset
35
35
  charset = ::Locale::Driver::Env.charset
36
36
  unless charset
37
- loc = LocaleTable.find{|v| v[1] =locale.to_general}
37
+ locale = locales[0]
38
+ loc = LocaleTable.find{|v| v[1] =locale.to_rfc}
38
39
  loc = LocaleTable.find{|v| v[1] =~ /^#{locale.language}-/} unless loc
39
- charset = loc ? loc[2] : "CP1252"
40
+ charset = loc ? loc[2] : nil
40
41
  end
41
42
  charset
42
43
  end
@@ -47,10 +48,10 @@ module Locale
47
48
  lang = LocaleTable.assoc(@@win32.call)
48
49
  if lang
49
50
  ret = Locale::Tag::Common.parse(lang[1])
51
+ locales = Locale::TagList.new([ret])
50
52
  else
51
- ret = Locale::Tag::Common.new("en")
53
+ locales = nil
52
54
  end
53
- locales = Locale::TagList.new([ret])
54
55
  end
55
56
  locales
56
57
  end
@@ -8,7 +8,7 @@
8
8
  =end
9
9
 
10
10
  require 'locale/tag/simple'
11
- require 'locale/tag/illegular'
11
+ require 'locale/tag/irregular'
12
12
  require 'locale/tag/common'
13
13
  require 'locale/tag/rfc'
14
14
  require 'locale/tag/cldr'
@@ -31,7 +31,7 @@ module Locale
31
31
  ret = parser.parse(tag)
32
32
  return ret if ret
33
33
  end
34
- Locale::Tag::Illegular.new(tag)
34
+ Locale::Tag::Irregular.new(tag)
35
35
  end
36
36
  memoize :parse
37
37
  end
@@ -9,8 +9,8 @@
9
9
 
10
10
  require 'locale/tag/common'
11
11
 
12
- module Locale #:nodoc:
13
- module Tag #:nodoc:
12
+ module Locale
13
+ module Tag
14
14
 
15
15
  # Unicode locale identifier class for CLDR-1.6.1.
16
16
  # (Unicode Common Locale Data Repository).
@@ -61,7 +61,7 @@ module Locale #:nodoc:
61
61
  super(language, script, region, variants.map{|v| v.upcase})
62
62
  end
63
63
 
64
- # Sets the extensions.
64
+ # Sets the extensions as an Hash.
65
65
  def extensions=(val)
66
66
  clear
67
67
  @extensions = val
@@ -67,7 +67,7 @@ module Locale
67
67
  @script
68
68
  end
69
69
 
70
- # Set the variants
70
+ # Set the variants as an Array.
71
71
  def variants=(val)
72
72
  clear
73
73
  @variants = val
@@ -1,12 +1,12 @@
1
1
  =begin
2
- locale/tag/illegular.rb - Locale::Tag::Illegular
2
+ locale/tag/irregular.rb - Locale::Tag::Irregular
3
3
 
4
4
  Copyright (C) 2008 Masao Mutoh
5
5
 
6
6
  You may redistribute it and/or modify it under the same
7
7
  license terms as Ruby.
8
8
 
9
- $Id: illegular.rb 27 2008-12-03 15:06:50Z mutoh $
9
+ $Id: irregular.rb 27 2008-12-03 15:06:50Z mutoh $
10
10
  =end
11
11
 
12
12
  require 'locale/tag/simple'
@@ -15,17 +15,17 @@ module Locale
15
15
 
16
16
  module Tag
17
17
  # Broken tag class.
18
- class Illegular < Simple
18
+ class Irregular < Simple
19
19
 
20
20
  def initialize(tag)
21
- tag = "en" if tag == nil || tag.empty?
21
+ tag = "en" if tag == nil or tag == ""
22
22
  @language = tag
23
23
  @tag = tag
24
24
  end
25
25
 
26
26
  # Returns an Array of tag-candidates order by priority.
27
27
  def candidates
28
- [Illegular.new(tag)]
28
+ [Irregular.new(tag)]
29
29
  end
30
30
  memoize :candidates
31
31
 
@@ -9,8 +9,8 @@
9
9
  $Id: posix.rb 27 2008-12-03 15:06:50Z mutoh $
10
10
  =end
11
11
 
12
- module Locale #:nodoc:
13
- module Tag #:nodoc:
12
+ module Locale
13
+ module Tag
14
14
 
15
15
  # Locale tag class for POSIX locale
16
16
  # * ja
@@ -60,7 +60,7 @@ module Locale #:nodoc:
60
60
  @charset = val
61
61
  end
62
62
 
63
- # Set the modifier.
63
+ # Set the modifier as a String
64
64
  def modifier=(val)
65
65
  clear
66
66
  @modifier = val
@@ -9,8 +9,8 @@
9
9
 
10
10
  require 'locale/tag/common'
11
11
 
12
- module Locale #:nodoc:
13
- module Tag #:nodoc:
12
+ module Locale
13
+ module Tag
14
14
 
15
15
  # Language tag class for RFC4646(BCP47).
16
16
  class Rfc < Common
@@ -64,13 +64,13 @@ module Locale #:nodoc:
64
64
  super(language, script, region, variants)
65
65
  end
66
66
 
67
- # Sets the extensions.
67
+ # Sets the extensions as an Array.
68
68
  def extensions=(val)
69
69
  clear
70
70
  @extensions = val
71
71
  end
72
72
 
73
- # Sets the extensions.
73
+ # Sets the privateuse as a String
74
74
  def privateuse=(val)
75
75
  clear
76
76
  @privateuse = val
@@ -91,6 +91,10 @@ module Locale
91
91
  to_s
92
92
  end
93
93
 
94
+ def <=>(other)
95
+ self.to_s <=> other.to_s
96
+ end
97
+
94
98
  def ==(other) #:nodoc:
95
99
  other != nil and hash == other.hash
96
100
  end
@@ -7,9 +7,9 @@
7
7
  module Locale
8
8
  module Util
9
9
  module Memoizable
10
- MEMOIZED_IVAR = Proc.new do |symbol|
10
+ MEMOIZED_IVAR = Proc.new do |symbol|
11
11
  "#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
12
- end
12
+ end
13
13
 
14
14
  def self.included(base)
15
15
  mod = self
@@ -26,8 +26,13 @@ module Locale
26
26
  end
27
27
  end
28
28
 
29
+ # Clear memoized values. Deprecated.
30
+ def clear # :nodoc:
31
+ @_memoized_ivars = {}
32
+ end
33
+
29
34
  # Clear memoized values.
30
- def clear
35
+ def memoize_clear
31
36
  @_memoized_ivars = {}
32
37
  end
33
38
 
@@ -7,6 +7,6 @@
7
7
  license terms as Ruby.
8
8
  =end
9
9
  module Locale
10
- VERSION = "2.0.1"
10
+ VERSION = "2.0.2"
11
11
  end
12
12
 
@@ -0,0 +1,10 @@
1
+ = Install Rack:
2
+ $ sudo gem install rack
3
+
4
+ = Run rack server:
5
+ $rackup hello_rack.ru
6
+
7
+ = Access from WWW browser:
8
+ http://localhost:9292/
9
+ http://localhost:9292/?lang=ja
10
+
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'locale'
4
+
5
+ Locale.init(:driver => :cgi)
6
+
7
+ class HelloRackApp
8
+ def call(env)
9
+ req = Rack::Request.new(env)
10
+ Locale.set_request(req["lang"], req.cookies["lang"],
11
+ env["HTTP_ACCEPT_LANGUAGE"], env["HTTP_ACCEPT_CHARSET"])
12
+ str = "Language tag candidates of your request order by the priority:\n\n"
13
+ str += Locale.candidates(:type => :rfc).map{|v| v.inspect + "\n"}.join
14
+ [200, {"Content-Type" => "text/plain", "Content-Length" => str.length.to_s}, [str]]
15
+ end
16
+ end
17
+
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift "../../lib"
2
+
3
+ require 'hello_rack'
4
+ run HelloRackApp.new
5
+
@@ -62,59 +62,59 @@ class TestDetectCGI < Test::Unit::TestCase
62
62
  end
63
63
 
64
64
  def test_accept_language
65
- setup_cgi("")
66
65
  ENV["HTTP_ACCEPT_LANGUAGE"] = ""
67
66
  ENV["HTTP_ACCEPT_CHARSET"] = ""
67
+ setup_cgi("")
68
68
  lang = Locale.current[0]
69
69
  assert_equal(Locale::Tag::Simple, lang.class)
70
70
  assert_equal("en", lang.to_s)
71
71
  assert_equal("en", lang.to_rfc.to_s)
72
72
 
73
- setup_cgi("")
74
73
  ENV["HTTP_ACCEPT_LANGUAGE"] = "ja,en-us;q=0.7,en;q=0.3"
74
+ setup_cgi("")
75
75
  lang1, lang2, lang3 = Locale.current
76
76
  assert_equal("ja", lang1.to_rfc.to_s)
77
77
  assert_equal("en-US", lang2.to_rfc.to_s)
78
78
  assert_equal("en", lang3.to_rfc.to_s)
79
79
 
80
- setup_cgi("")
81
80
  ENV["HTTP_ACCEPT_LANGUAGE"] = "en-us,ja;q=0.7,en;q=0.3"
81
+ setup_cgi("")
82
82
  lang1, lang2, lang3 = Locale.current
83
83
  assert_equal("en-US", lang1.to_rfc.to_s)
84
84
  assert_equal("ja", lang2.to_rfc.to_s)
85
85
  assert_equal("en", lang3.to_rfc.to_s)
86
86
 
87
- setup_cgi("")
88
87
  ENV["HTTP_ACCEPT_LANGUAGE"] = "en"
88
+ setup_cgi("")
89
89
  lang = Locale.current[0]
90
90
  assert_equal("en", lang.to_rfc.to_s)
91
91
  end
92
92
 
93
93
  def test_accept_charset
94
94
  #accept charset
95
- setup_cgi("")
96
95
  ENV["HTTP_ACCEPT_CHARSET"] = "Shift_JIS"
96
+ setup_cgi("")
97
97
  assert_equal("Shift_JIS", Locale.charset)
98
98
 
99
- setup_cgi("")
100
99
  ENV["HTTP_ACCEPT_CHARSET"] = "EUC-JP,*,utf-8"
100
+ setup_cgi("")
101
101
  assert_equal("EUC-JP", Locale.charset)
102
102
 
103
- setup_cgi("")
104
103
  ENV["HTTP_ACCEPT_CHARSET"] = "*"
104
+ setup_cgi("")
105
105
  assert_equal("UTF-8", Locale.charset)
106
106
 
107
- setup_cgi("")
108
107
  ENV["HTTP_ACCEPT_CHARSET"] = ""
108
+ setup_cgi("")
109
109
  assert_equal("UTF-8", Locale.charset)
110
110
  end
111
111
 
112
112
  def test_default
113
113
  Locale.set_default(nil)
114
114
  Locale.set_default("ja-JP")
115
- setup_cgi("")
116
115
  ENV["HTTP_ACCEPT_LANGUAGE"] = ""
117
116
  ENV["HTTP_ACCEPT_CHARSET"] = ""
117
+ setup_cgi("")
118
118
  assert_equal("ja-JP", Locale.default.to_rfc.to_s)
119
119
  assert_equal("ja-JP", Locale.current.to_rfc.to_s)
120
120
  Locale.set_default(nil)
@@ -137,9 +137,9 @@ class TestDetectCGI < Test::Unit::TestCase
137
137
  end
138
138
 
139
139
  def test_candidates
140
- setup_cgi("")
141
140
 
142
141
  ENV["HTTP_ACCEPT_LANGUAGE"] = "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1"
142
+ setup_cgi("")
143
143
 
144
144
  assert_equal common("fr-FR", "zh-CN", "zh-TW", "ja-JP",
145
145
  "fr", "zh", "ja", "en"), Locale.candidates
@@ -153,27 +153,64 @@ class TestDetectCGI < Test::Unit::TestCase
153
153
  assert_equal simple("fr-FR", "zh-CN", "zh-TW", "ja-JP",
154
154
  "fr", "zh", "ja", "en"), Locale.candidates(:type => :simple)
155
155
 
156
+ taglist = Locale.candidates(:type => :rfc)
157
+ assert_equal Locale::TagList, taglist.class
158
+ assert_equal "fr", taglist.language
159
+ assert_equal "FR", taglist.region
160
+
161
+ end
162
+
163
+ def test_candidates_with_supported_language_tags
164
+ ENV["HTTP_ACCEPT_LANGUAGE"] = "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1"
165
+ setup_cgi("")
166
+
156
167
  assert_equal common("fr_FR", "zh", "ja"), Locale.candidates(:type => :common,
157
168
  :supported_language_tags => ["fr_FR", "ja", "zh"])
158
169
 
159
170
  assert_equal simple("fr-FR", "zh", "ja"), Locale.candidates(:type => :simple,
160
171
  :supported_language_tags => ["fr-FR", "ja", "zh"])
172
+ #supported_language_tags includes "pt" as not in HTTP_ACCEPT_LANGUAGE
173
+ assert_equal simple("fr-FR", "zh", "ja"),
174
+ Locale.candidates(:type => :simple,
175
+ :supported_language_tags => ["fr-FR", "ja", "zh", "pt"])
176
+
177
+ end
161
178
 
162
- assert_equal simple("fr-FR", "zh", "ja", "no", "pt"),
179
+ def test_candidates_with_default
180
+ ENV["HTTP_ACCEPT_LANGUAGE"] = "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1"
181
+ setup_cgi("")
182
+
183
+ Locale.default = "zh_TW"
184
+ assert_equal simple("fr-FR", "zh", "ja"),
163
185
  Locale.candidates(:type => :simple,
164
- :supported_language_tags => ["fr-FR", "ja", "zh", "no", "pt"],
165
- :default_language_tags => ["no", "pt"])
186
+ :supported_language_tags => ["fr-FR", "ja", "zh", "pt"])
166
187
 
167
- assert_equal simple("no", "pt"), Locale.candidates(:type => :simple,
168
- :supported_language_tags => ["aa"],
169
- :default_language_tags => ["no", "pt"])
170
- assert_equal simple("en"), Locale.candidates(:type => :simple,
188
+ Locale.default = "pt"
189
+ assert_equal simple("fr-FR", "zh", "ja", "pt"),
190
+ Locale.candidates(:type => :simple,
191
+ :supported_language_tags => ["fr-FR", "ja", "zh", "pt"])
192
+
193
+ # default value is selected even if default is not in supported_language_tags.
194
+ assert_equal simple("pt"), Locale.candidates(:type => :simple,
171
195
  :supported_language_tags => ["aa"])
172
-
173
- taglist = Locale.candidates(:type => :rfc)
174
- assert_equal Locale::TagList, taglist.class
175
- assert_equal "fr", taglist.language
176
- assert_equal "FR", taglist.region
196
+ Locale.default = "en"
197
+ end
198
+
199
+
200
+ def test_candidates_with_app_language_tags
201
+ Locale.set_app_language_tags("fr-FR", "ja")
202
+
203
+ ENV["HTTP_ACCEPT_LANGUAGE"] = "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1"
204
+ setup_cgi("")
205
+
206
+ assert_equal common("fr-FR", "ja"), Locale.candidates
207
+
208
+ # default value is selected if default is not in app_language_tags.
209
+ Locale.set_app_language_tags("no", "pt")
210
+ Locale.default = "zh"
211
+ assert_equal common("zh"), Locale.candidates
177
212
 
213
+ Locale.default = "en"
214
+ Locale.set_app_language_tags(nil)
178
215
  end
179
216
  end
@@ -123,6 +123,7 @@ class TestDetectGeneral < Test::Unit::TestCase
123
123
  end
124
124
 
125
125
  def test_default
126
+ return unless /linux|bsd/ =~ RUBY_PLATFORM
126
127
  Locale.set_default("yo_NG")
127
128
  assert_equal Locale::Tag.parse("yo_NG"), Locale.default
128
129
  assert_equal Locale::Tag.parse("yo_NG"), Locale.current[0]
@@ -1135,11 +1135,11 @@ class TagTest < Test::Unit::TestCase
1135
1135
 
1136
1136
  def test_invaild
1137
1137
  lang = Locale::Tag.parse("")
1138
- assert_equal Locale::Tag::Illegular, lang.class
1138
+ assert_equal Locale::Tag::Irregular, lang.class
1139
1139
  assert_equal "en", lang.language
1140
1140
  assert_equal nil, lang.region
1141
1141
 
1142
- assert_equal [Locale::Tag::Illegular.new("en"),
1142
+ assert_equal [Locale::Tag::Irregular.new("en"),
1143
1143
  ], lang.candidates
1144
1144
 
1145
1145
  assert_equal Locale::Tag::Simple.parse("en"), lang.to_simple
@@ -1149,11 +1149,11 @@ class TagTest < Test::Unit::TestCase
1149
1149
  assert_equal Locale::Tag::Posix.parse("en"), lang.to_posix
1150
1150
 
1151
1151
  lang = Locale::Tag.parse(nil)
1152
- assert_equal Locale::Tag::Illegular, lang.class
1152
+ assert_equal Locale::Tag::Irregular, lang.class
1153
1153
  assert_equal "en", lang.language
1154
1154
  assert_equal nil, lang.region
1155
1155
 
1156
- assert_equal [Locale::Tag::Illegular.new("en"),
1156
+ assert_equal [Locale::Tag::Irregular.new("en"),
1157
1157
  ], lang.candidates
1158
1158
 
1159
1159
  assert_equal Locale::Tag::Simple.parse("en"), lang.to_simple
@@ -1168,4 +1168,5 @@ class TagTest < Test::Unit::TestCase
1168
1168
  Locale::Tag::Simple.new(nil)
1169
1169
  }
1170
1170
  end
1171
+
1171
1172
  end
@@ -12,7 +12,7 @@ class TestThread < Test::Unit::TestCase
12
12
  def invoke_thread(tag, sleep_time)
13
13
  Thread.start do
14
14
  @mutex.synchronize {
15
- ENV["LANG"] = tag
15
+ ENV["LC_ALL"] = tag
16
16
  Locale.current
17
17
  }
18
18
  (1..10).each do |v|
@@ -27,9 +27,9 @@ class TestThread < Test::Unit::TestCase
27
27
  end
28
28
 
29
29
  def test_thread
30
- th1 = invoke_thread("ja_JP.eucJP", 0.6)
31
- th2 = invoke_thread("zh_CN.UTF-8", 0.4)
32
- th3 = invoke_thread("en", 0.2)
30
+ th1 = invoke_thread("ja_JP.eucJP", 0.3)
31
+ th2 = invoke_thread("zh_CN.UTF-8", 0.2)
32
+ th3 = invoke_thread("en", 0.1)
33
33
  th1.join
34
34
  th2.join
35
35
  th3.join
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masao Mutoh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-19 00:00:00 +09:00
12
+ date: 2009-05-09 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,10 @@ files:
25
25
  - ChangeLog
26
26
  - samples
27
27
  - samples/sample_info.rb
28
+ - samples/rack
29
+ - samples/rack/README
30
+ - samples/rack/hello_rack.rb
31
+ - samples/rack/hello_rack.ru
28
32
  - samples/sample_1.rb
29
33
  - samples/cgi
30
34
  - samples/cgi/cookie.cgi
@@ -49,8 +53,8 @@ files:
49
53
  - lib/locale/tag
50
54
  - lib/locale/tag/cldr.rb
51
55
  - lib/locale/tag/common.rb
52
- - lib/locale/tag/illegular.rb
53
56
  - lib/locale/tag/rfc.rb
57
+ - lib/locale/tag/irregular.rb
54
58
  - lib/locale/tag/posix.rb
55
59
  - lib/locale/tag/simple.rb
56
60
  - lib/locale/info