locale 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/COPYING +55 -0
  2. data/ChangeLog +113 -0
  3. data/README +100 -0
  4. data/Rakefile +77 -0
  5. data/doc/classes/Locale/Driver.html +375 -0
  6. data/doc/classes/Locale/Driver/CGI.html +502 -0
  7. data/doc/classes/Locale/Driver/Env.html +440 -0
  8. data/doc/classes/Locale/Driver/JRuby.html +390 -0
  9. data/doc/classes/Locale/Driver/Posix.html +431 -0
  10. data/doc/classes/Locale/Driver/Win32.html +398 -0
  11. data/doc/classes/Locale/Driver/Win32Table.html +356 -0
  12. data/doc/classes/Locale/Info.html +586 -0
  13. data/doc/classes/Locale/Info/Language.html +764 -0
  14. data/doc/classes/Locale/Info/Region.html +470 -0
  15. data/doc/classes/Locale/Tag/Cldr.html +547 -0
  16. data/doc/classes/Locale/Tag/Common.html +655 -0
  17. data/doc/classes/Locale/Tag/Illegular.html +424 -0
  18. data/doc/classes/Locale/Tag/Posix.html +612 -0
  19. data/doc/classes/Locale/Tag/Rfc.html +610 -0
  20. data/doc/classes/Locale/Tag/Simple.html +684 -0
  21. data/doc/classes/Locale/TagList.html +783 -0
  22. data/doc/classes/Locale/Util.html +476 -0
  23. data/doc/classes/Locale/Util/Memoizable.html +356 -0
  24. data/doc/created.rid +1 -0
  25. data/doc/files/ChangeLog.html +497 -0
  26. data/doc/files/README.html +525 -0
  27. data/doc/files/lib/locale/driver/cgi_rb.html +342 -0
  28. data/doc/files/lib/locale/driver/env_rb.html +354 -0
  29. data/doc/files/lib/locale/driver/jruby_rb.html +359 -0
  30. data/doc/files/lib/locale/driver/posix_rb.html +349 -0
  31. data/doc/files/lib/locale/driver/win32_rb.html +359 -0
  32. data/doc/files/lib/locale/driver/win32_table_rb.html +342 -0
  33. data/doc/files/lib/locale/info/language_rb.html +353 -0
  34. data/doc/files/lib/locale/info/region_rb.html +353 -0
  35. data/doc/files/lib/locale/info_rb.html +354 -0
  36. data/doc/files/lib/locale/tag/cldr_rb.html +342 -0
  37. data/doc/files/lib/locale/tag/common_rb.html +342 -0
  38. data/doc/files/lib/locale/tag/illegular_rb.html +349 -0
  39. data/doc/files/lib/locale/tag/posix_rb.html +342 -0
  40. data/doc/files/lib/locale/tag/rfc_rb.html +342 -0
  41. data/doc/files/lib/locale/tag/simple_rb.html +349 -0
  42. data/doc/files/lib/locale/tag_rb.html +374 -0
  43. data/doc/files/lib/locale/taglist_rb.html +342 -0
  44. data/doc/files/lib/locale/util/memoizable_rb.html +360 -0
  45. data/doc/files/lib/locale/version_rb.html +342 -0
  46. data/doc/files/lib/locale_rb.html +379 -0
  47. data/doc/fr_class_index.html +20 -0
  48. data/doc/fr_file_index.html +23 -0
  49. data/doc/fr_method_index.html +80 -0
  50. data/doc/index.html +1 -0
  51. data/doc/rdoc-style.css +320 -0
  52. data/lib/locale.rb +248 -0
  53. data/lib/locale/data/languages.tab.gz +0 -0
  54. data/lib/locale/data/regions.tab.gz +0 -0
  55. data/lib/locale/driver/cgi.rb +132 -0
  56. data/lib/locale/driver/env.rb +64 -0
  57. data/lib/locale/driver/jruby.rb +53 -0
  58. data/lib/locale/driver/posix.rb +49 -0
  59. data/lib/locale/driver/win32.rb +61 -0
  60. data/lib/locale/driver/win32_table.rb +298 -0
  61. data/lib/locale/info.rb +12 -0
  62. data/lib/locale/info/language.rb +134 -0
  63. data/lib/locale/info/region.rb +74 -0
  64. data/lib/locale/tag.rb +36 -0
  65. data/lib/locale/tag/cldr.rb +93 -0
  66. data/lib/locale/tag/common.rb +122 -0
  67. data/lib/locale/tag/illegular.rb +38 -0
  68. data/lib/locale/tag/posix.rb +97 -0
  69. data/lib/locale/tag/rfc.rb +106 -0
  70. data/lib/locale/tag/simple.rb +145 -0
  71. data/lib/locale/taglist.rb +93 -0
  72. data/lib/locale/util/memoizable.rb +76 -0
  73. data/lib/locale/version.rb +12 -0
  74. data/samples/cgi/README +20 -0
  75. data/samples/cgi/cookie.cgi +62 -0
  76. data/samples/cgi/http.rb +52 -0
  77. data/samples/cgi/index.cgi +90 -0
  78. data/samples/cgi/locale.css +115 -0
  79. data/samples/sample_1.rb +25 -0
  80. data/samples/sample_info.rb +6 -0
  81. data/setup.rb +1585 -0
  82. data/test/test_detect_cgi.rb +179 -0
  83. data/test/test_detect_general.rb +159 -0
  84. data/test/test_info.rb +28 -0
  85. data/test/test_tag.rb +1183 -0
  86. data/test/test_thread.rb +37 -0
  87. metadata +162 -0
@@ -0,0 +1,38 @@
1
+ =begin
2
+ locale/tag/illegular.rb - Locale::Tag::Illegular
3
+
4
+ Copyright (C) 2008 Masao Mutoh
5
+
6
+ You may redistribute it and/or modify it under the same
7
+ license terms as Ruby.
8
+
9
+ $Id: illegular.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ require 'locale/tag/simple'
13
+
14
+ module Locale
15
+
16
+ module Tag
17
+ # Broken tag class.
18
+ class Illegular < Simple
19
+
20
+ def initialize(tag)
21
+ tag = "en" if tag == nil || tag.empty?
22
+ @language = tag
23
+ @tag = tag
24
+ end
25
+
26
+ # Returns an Array of tag-candidates order by priority.
27
+ def candidates
28
+ [Illegular.new(tag)]
29
+ end
30
+
31
+ # Conver to the klass(the class of Language::Tag)
32
+ private
33
+ def convert_to(klass)
34
+ klass.new(tag)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,97 @@
1
+ =begin
2
+ locale/tag/posix.rb - Locale::Tag::Posix
3
+
4
+ Copyright (C) 2008 Masao Mutoh
5
+
6
+ You may redistribute it and/or modify it under the same
7
+ license terms as Ruby.
8
+
9
+ $Id: posix.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ module Locale #:nodoc:
13
+ module Tag #:nodoc:
14
+
15
+ # Locale tag class for POSIX locale
16
+ # * ja
17
+ # * ja_JP
18
+ # * ja_JP.UTF-8
19
+ # * ja_JP.UTF-8@Osaka
20
+ # * C/POSIX (-> en_US)
21
+ class Posix < Simple
22
+ LANGUAGE = "([a-z]{2,})"
23
+ TAG_RE = /\A#{LANGUAGE}(?:_#{REGION})?(?:\.([^@]+))?(?:@(.*))?\Z/i
24
+
25
+ attr_reader :charset, :modifier
26
+
27
+ def initialize(language, region = nil, charset = nil, modifier = nil)
28
+ @charset, @modifier = charset, modifier
29
+ super(language, region)
30
+ end
31
+
32
+ def self.parse(tag)
33
+ if tag =~ /^(C|POSIX)$/
34
+ ret = self.new("en", "US")
35
+ ret.tag = tag
36
+ ret
37
+ elsif tag =~ TAG_RE
38
+ ret = self.new($1, $2, $3, $4)
39
+ ret.tag = tag
40
+ ret
41
+ else
42
+ nil
43
+ end
44
+ end
45
+
46
+ # Returns the language tag.
47
+ # <language>_<COUNTRY>.<CHARSET>@<MODIFIER>
48
+ # (e.g.) "ja_JP.EUC-JP@Modifier"
49
+ def to_s
50
+ s = @language.dup
51
+ s << "_#{@region}" if @region
52
+ s << ".#{@charset}" if @charset
53
+ s << "@#{@modifier}" if @modifier
54
+ s
55
+ end
56
+
57
+ # Set the charset.
58
+ def charset=(val)
59
+ clear
60
+ @charset = val
61
+ end
62
+
63
+ # Set the modifier.
64
+ def modifier=(val)
65
+ clear
66
+ @modifier = val
67
+ end
68
+
69
+ # Returns an Array of tag-candidates order by priority.
70
+ # Use Locale.candidates instead of this method.
71
+ def candidates
72
+ [self.class.new(language, region, charset, modifier), #ja_JP.UTF-8@Modifier
73
+ self.class.new(language, region, charset), #ja_JP.UTF-8
74
+ self.class.new(language, region, nil, modifier), #ja_JP@Modifier
75
+ self.class.new(language, region, nil, nil), #ja_JP@Modifier
76
+ self.class.new(language, nil, charset, modifier), #ja.UTF-8@Modifier
77
+ self.class.new(language, nil, charset), #ja.UTF-8
78
+ self.class.new(language, nil, nil, modifier), #ja@Modifier
79
+ self.class.new(language)] #ja
80
+ end
81
+
82
+ # A modifier is converted to a variant.
83
+ # If the modifier is less than 5 characters, it is not canonical value.
84
+ private
85
+ def convert_to(klass)
86
+ if klass == Simple
87
+ super
88
+ elsif klass == Posix
89
+ klass.new(language, region, charset, modifier)
90
+ else
91
+ klass.new(language, nil, region, modifier ? [modifier] : [])
92
+ end
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,106 @@
1
+ =begin
2
+ locale/tag/rfc.rb - Locale::Tag::Rfc
3
+
4
+ Copyright (C) 2008 Masao Mutoh
5
+
6
+ You may redistribute it and/or modify it under the same
7
+ license terms as Ruby.
8
+
9
+ $Id: rfc.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ module Locale #:nodoc:
13
+ module Tag #:nodoc:
14
+
15
+ # Language tag class for RFC4646(BCP47).
16
+ class Rfc < Common
17
+ SINGLETON = '[a-wyz0-9]'
18
+ VARIANT = "(#{ALPHANUM}{5,8}|#{DIGIT}#{ALPHANUM}{3})"
19
+ EXTENSION = "(#{SINGLETON}(?:-#{ALPHANUM}{2,8})+)"
20
+ PRIVATEUSE = "(x(?:-#{ALPHANUM}{1,8})+)"
21
+ GRANDFATHERED = "#{ALPHA}{1,3}(?:-#{ALPHANUM}{2,8}){1,2}"
22
+
23
+ TAG_RE = /\A#{LANGUAGE}(?:-#{SCRIPT})?
24
+ (?:-#{REGION})?((?:-#{VARIANT})*
25
+ (?:-#{EXTENSION})*(?:-#{PRIVATEUSE})?)\Z/ix
26
+
27
+ attr_reader :extensions, :privateuse
28
+
29
+ def initialize(language, script = nil, region = nil, variants = [],
30
+ extensions = [], privateuse = nil)
31
+ @extensions, @privateuse = extensions, privateuse
32
+ super(language, script, region, variants)
33
+ end
34
+
35
+ # Parse the language tag and return the new Locale::Tag::Common.
36
+ def self.parse(tag)
37
+ if tag =~ /\APOSIX\Z/ # This is the special case of POSIX locale but match this regexp.
38
+ nil
39
+ elsif tag =~ TAG_RE
40
+ lang, script, region, subtag = $1, $2, $3, $4
41
+ extensions = []
42
+ variants = []
43
+ if subtag =~ /#{PRIVATEUSE}/
44
+ subtag, privateuse = $`, $1
45
+ # Private use for CLDR.
46
+ if /x-ldml(.*)/ =~ privateuse
47
+ p_subtag = $1
48
+ extensions = p_subtag.scan(/(^|-)#{EXTENSION}/i).collect{|v| p_subtag.sub!(v[1], ""); v[1]}
49
+ variants = p_subtag.scan(/(^|-)#{VARIANT}(?=(-|$))/i).collect{|v| v[1]}
50
+ end
51
+ end
52
+ extensions += subtag.scan(/(^|-)#{EXTENSION}/i).collect{|v| subtag.sub!(v[1], ""); v[1]}
53
+ variants += subtag.scan(/(^|-)#{VARIANT}(?=(-|$))/i).collect{|v| v[1]}
54
+
55
+ ret = self.new(lang, script, region, variants, extensions, privateuse)
56
+ ret.tag = tag
57
+ ret
58
+ else
59
+ nil
60
+ end
61
+ end
62
+
63
+ # Returns the language tag
64
+ # <language>-<Script>-<REGION>-<variants>-<extensions>-<PRIVATEUSE>
65
+ # (e.g.) "ja-Hira-JP-variant"
66
+ def to_s
67
+ s = super.to_s.gsub(/_/, "-")
68
+ @extensions.each do |v|
69
+ s << "-#{v}"
70
+ end
71
+ s << "-#{@privateuse}" if @privateuse
72
+ s
73
+ end
74
+
75
+ # Sets the extensions.
76
+ def extensions=(val)
77
+ clear
78
+ @extensions = val
79
+ end
80
+
81
+ # Sets the extensions.
82
+ def privateuse=(val)
83
+ clear
84
+ @privateuse = val
85
+ end
86
+
87
+ private
88
+ def convert_to(klass)
89
+ if klass == Rfc
90
+ klass.new(language, script, region, variants, extensions, privateuse)
91
+ elsif klass == Cldr
92
+ exts = {}
93
+ extensions.each do |v|
94
+ if v =~ /^k-(#{ALPHANUM}{2,})-(.*)$/i
95
+ exts[$1] = $2
96
+ end
97
+ end
98
+ klass.new(language, script, region, variants, exts)
99
+ else
100
+ super
101
+ end
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,145 @@
1
+ =begin
2
+ locale/tag/simple.rb - Locale::Tag::Simple
3
+
4
+ Copyright (C) 2008 Masao Mutoh
5
+
6
+ You may redistribute it and/or modify it under the same
7
+ license terms as Ruby.
8
+
9
+ $Id: simple.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ require 'locale/util/memoizable'
13
+
14
+ module Locale
15
+ module Tag
16
+ # Abstract language tag class.
17
+ # This class has <language>, <region> which
18
+ # all of language tag specifications have.
19
+ #
20
+ # * ja (language: ISO 639 (2 or 3 alpha))
21
+ # * ja_JP (country: RFC4646 (ISO3166/UN M.49) (2 alpha or 3 digit)
22
+ # * ja-JP
23
+ # * ja-392
24
+ class Simple
25
+ ALPHA = '[a-z]'
26
+ DIGIT = '[0-9]'
27
+ ALPHANUM = "[a-zA-Z0-9]"
28
+
29
+ LANGUAGE = "(#{ALPHA}{2,3})" # ISO 639
30
+ REGION = "(#{ALPHA}{2}|#{DIGIT}{3})" # RFC4646 (ISO3166/UN M.49)
31
+
32
+ TAG_RE = /\A#{LANGUAGE}(?:[_-]#{REGION})?\Z/i
33
+
34
+ include Util::Memoizable
35
+
36
+ attr_reader :language, :region
37
+
38
+ # tag is set when .parse method is called.
39
+ # This value is used when the program want to know the original
40
+ # String.
41
+ attr_accessor :tag
42
+
43
+ # Parse the language tag and return the new Locale::Tag::Simple.
44
+ def self.parse(tag)
45
+ if tag =~ TAG_RE
46
+ ret = self.new($1, $2)
47
+ ret.tag = tag
48
+ ret
49
+ else
50
+ nil
51
+ end
52
+ end
53
+
54
+ # call-seq:
55
+ # to_common
56
+ # to_posix
57
+ # to_rfc
58
+ # to_cldr
59
+ #
60
+ # Convert to each tag classes.
61
+ [:simple, :common, :posix, :rfc, :cldr].each do |name|
62
+ class_eval <<-EOS
63
+ def to_#{name}
64
+ convert_to(#{name.to_s.capitalize})
65
+ end
66
+ memoize :to_#{name}
67
+ EOS
68
+ end
69
+
70
+ # Create a Locale::Tag::Simple
71
+ def initialize(language, region = nil)
72
+ raise "language can't be nil." unless language
73
+ @language, @region = language, region
74
+ @language.downcase! if @language
75
+ @region.upcase! if @region
76
+ end
77
+
78
+ # Returns the language tag as the String.
79
+ # <language>_<REGION>
80
+ # (e.g.) "ja_JP"
81
+ def to_s
82
+ s = @language.dup
83
+ s << "_" << @region if @region
84
+ s
85
+ end
86
+
87
+ def to_str #:nodoc:
88
+ to_s
89
+ end
90
+
91
+ def ==(other) #:nodoc:
92
+ other != nil and hash == other.hash
93
+ end
94
+
95
+ def eql?(other) #:nodoc:
96
+ self.==(other)
97
+ end
98
+
99
+ def hash #:nodoc:
100
+ "#{self.class}:#{to_s}".hash
101
+ end
102
+
103
+ def inspect #:nodoc:
104
+ %Q[#<#{self.class}: #{to_s}>]
105
+ end
106
+
107
+ # For backward compatibility.
108
+ def country; region end
109
+
110
+ # Set the language (with downcase)
111
+ def language=(val)
112
+ clear
113
+ @language = val
114
+ @language.downcase! if @language
115
+ @language
116
+ end
117
+
118
+ # Set the region (with upcase)
119
+ def region=(val)
120
+ clear
121
+ @region = val
122
+ @region.upcase! if @region
123
+ @region
124
+ end
125
+
126
+ # Returns an Array of tag-candidates order by priority.
127
+ # Use Locale.candidates instead of this method.
128
+ def candidates
129
+ [self.class.new(language, region), self.class.new(language)]
130
+ end
131
+
132
+ memoize :to_s, :to_str, :hash, :inspect, :candidates
133
+
134
+ # Conver to the klass(the class of Language::Tag)
135
+ private
136
+ def convert_to(klass)
137
+ if klass == Simple || klass == Posix
138
+ klass.new(language, region)
139
+ else
140
+ klass.new(language, nil, region)
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,93 @@
1
+ =begin
2
+ taglist.rb - Locale module
3
+
4
+ Copyright (C) 2008 Masao Mutoh
5
+
6
+ You may redistribute it and/or modify it under the same
7
+ license terms as Ruby.
8
+
9
+ $Id: taglist.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ module Locale
13
+ # This provides the subclass of Array which behaves like
14
+ # the first(top priority) Locale::Tag object.
15
+ # "Locale.current.language" is same with "Locale.current[0].language".
16
+ #
17
+ # Locale.current returns an Array of Tag(s) now.
18
+ # But the old Locale.current(Ruby-GetText) and Locale.get
19
+ # returns Locale::Object (similier with Locale::Tag::Posix).
20
+ # This is the class for backward compatibility.
21
+ #
22
+ # It is recommanded to use Locale.current[0] or
23
+ # Locale.candidates to find the current locale instead
24
+ # of this function.
25
+ #
26
+ class TagList < Array
27
+ # Returns the top priority language. (simple)
28
+ def language
29
+ self[0].language
30
+ end
31
+ # Returns the top priority region/country. (simple)
32
+ def country
33
+ self[0].region
34
+ end
35
+ # Returns the top priority region/country. (simple)
36
+ def region
37
+ self[0].region
38
+ end
39
+ # Returns the top priority script. (common)
40
+ def script
41
+ self[0].script
42
+ end
43
+ # Returns the top priority charset. (posix)
44
+ def charset
45
+ if self[0].respond_to? :charset
46
+ self[0].charset
47
+ else
48
+ ::Locale.driver_module.charset
49
+ end
50
+ end
51
+ # Returns the top priority modifier. (posix)
52
+ def modifier
53
+ (self[0].respond_to? :modifier) ? self[0].modifier : nil
54
+ end
55
+ # Returns the top priority extensions.(common, rfc, cldr)
56
+ def extensions
57
+ (self[0].respond_to? :extensions) ? self[0].extensions : nil
58
+ end
59
+ # Returns the top priority privateuse(rfc)
60
+ def privateuse
61
+ (self[0].respond_to? :privateuse) ? self[0].privateuse : nil
62
+ end
63
+
64
+ def to_str
65
+ self[0].to_str
66
+ end
67
+
68
+ def to_s
69
+ self[0].to_s
70
+ end
71
+
72
+ def to_common
73
+ self[0].to_common
74
+ end
75
+
76
+ def to_simple
77
+ self[0].to_simple
78
+ end
79
+
80
+ def to_rfc
81
+ self[0].to_rfc
82
+ end
83
+
84
+ def to_cldr
85
+ self[0].to_cldr
86
+ end
87
+
88
+ def to_posix
89
+ self[0].to_posix
90
+ end
91
+
92
+ end
93
+ end