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,12 @@
1
+ =begin
2
+
3
+ info.rb - Load Locale::Info::Language and Locale::Info::Region.
4
+
5
+ Copyright (C) 2008 Masao Mutoh
6
+
7
+ $Id: info.rb 27 2008-12-03 15:06:50Z mutoh $
8
+ =end
9
+
10
+ require 'locale/info/language'
11
+ require 'locale/info/region'
12
+
@@ -0,0 +1,134 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+
4
+ language.rb - Locale::Info::Language class
5
+
6
+ Copyright (C) 2008 Masao Mutoh
7
+
8
+ Original Author:: Brian Pontarelli
9
+
10
+ $Id: language.rb 27 2008-12-03 15:06:50Z mutoh $
11
+ =end
12
+
13
+ require 'zlib'
14
+
15
+ module Locale
16
+
17
+ module Info
18
+ # This class contains all the of the ISO information for the ISO 639-3
19
+ # languages. This class is immutable once constructed.
20
+ class Language
21
+ attr_reader :two_code, :three_code, :scope, :type, :name
22
+
23
+ #
24
+ # Constructs a new Language instance.
25
+ #
26
+ # * code The 2 or 3 digit ISO 639-3 language code.
27
+ # * scope A single character that defines the ISO scope of the language - <tt>(I)ndividual</tt>,
28
+ # <tt>(M)acrolanguage</tt>, or <tt>(S)pecial</tt>.
29
+ # * type A single character that defines the ISO type of the language - <tt>(A)ncient</tt>,
30
+ # <tt>(C)onstructed</tt>, <tt>(E)xtinct</tt>, <tt>(H)istorical</tt>, <tt>(L)iving</tt>,
31
+ # or <tt>(S)pecial</tt>.
32
+ # * name The name of the language.
33
+ #
34
+ def initialize(two_code, three_code, scope, type, name)
35
+ @two_code, @three_code, @scope, @type, @name = two_code, three_code, scope, type, name
36
+
37
+ @individual = (scope == "I")
38
+ @macro = (scope == "M")
39
+ @special = (scope == "S")
40
+ @constructed = (type == "C")
41
+ @living = (type == "L")
42
+ @extinct = (type == "E")
43
+ @ancient = (type == "A")
44
+ @historical = (type == "H")
45
+ @special_type = (type == "S")
46
+ end
47
+
48
+ # Returns true if the language is an individual language according to the ISO 639-3 data.
49
+ def individual?; @individual; end
50
+
51
+ # Returns true if the language is a macro language according to the ISO 639-3 data.
52
+ def macro?; @macro; end
53
+
54
+ # Returns true if the language is a special language according to the ISO 639-3 data.
55
+ def special?; @special; end
56
+
57
+ # Returns true if the language is a constructed language according to the ISO 639-3 data.
58
+ def constructed?; @constructed; end
59
+
60
+ # Returns true if the language is a living language according to the ISO 639-3 data.
61
+ def living?; @living; end
62
+
63
+ # Returns true if the language is an extinct language according to the ISO 639-3 data.
64
+ def extinct?; @extinct; end
65
+
66
+ # Returns true if the language is an ancient language according to the ISO 639-3 data.
67
+ def ancient?; @ancient; end
68
+
69
+ # Returns true if the language is an historical language according to the ISO 639-3 data.
70
+ def historical?; @historical; end
71
+
72
+ # Returns true if the language is a special type language according to the ISO 639-3 data.
73
+ def special_type?; @special_type; end
74
+
75
+ # Returns the two or three code.
76
+ def to_s
77
+ two_code || tree_code
78
+ end
79
+
80
+ # Returns this object is valid as ISO 639 data.
81
+ def iso_language?
82
+ @@lang_two_codes[two_code] != nil || @@lang_three_codes[three_code] != nil
83
+ end
84
+ end
85
+
86
+ @@lang_two_codes = Hash.new
87
+ @@lang_three_codes = Hash.new
88
+
89
+ Zlib::GzipReader.open(File.dirname(__FILE__) + "/../data/languages.tab.gz") do |gz|
90
+ gz.readlines.each do |l|
91
+ unless l =~ /^\s*$/
92
+ parts = l.split(/\t/)
93
+ lang = Language.new(parts[2], parts[0], parts[3], parts[4], parts[5].strip)
94
+ @@lang_three_codes[parts[0]] = lang
95
+ @@lang_two_codes[parts[2]] = lang if parts[2].length > 0
96
+ end
97
+ end
98
+ end
99
+
100
+ module_function
101
+
102
+ # Returns a hash of all the ISO languages. The hash is {String, language} where
103
+ # the string is the 3 digit language code from the ISO 639 data. This contains
104
+ # all of the data from the ISO 639-3 data (7600 Languages).
105
+ #
106
+ # Need to require 'locale/info' or 'locale/language'.
107
+ def three_languages
108
+ @@lang_three_codes
109
+ end
110
+
111
+ # Returns a hash of all the ISO languages. The hash is {String, language} where
112
+ # the string is the 2 digit language code from the ISO 639-1 data. This contains
113
+ # all of the data from the ISO 639-1 data (186 Languages).
114
+ #
115
+ # Need to require 'locale/info' or 'locale/language'.
116
+ def two_languages
117
+ @@lang_two_codes
118
+ end
119
+
120
+ # Returns the language for the given 2 or 3 digit code.
121
+ #
122
+ # Need to require 'locale/info' or 'locale/language'.
123
+ def get_language(code)
124
+ @@lang_three_codes[code] || @@lang_two_codes[code]
125
+ end
126
+
127
+ # Returns the language code is valid.
128
+ #
129
+ # Need to require 'locale/info' or 'locale/language'.
130
+ def language_code?(code)
131
+ get_language(code) != nil
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+
4
+ region.rb - Locale::Info::Region class
5
+
6
+ Copyright (C) 2008 Masao Mutoh
7
+
8
+ First Author:: Brian Pontarelli
9
+
10
+ $Id: region.rb 27 2008-12-03 15:06:50Z mutoh $
11
+ =end
12
+
13
+ require 'zlib'
14
+
15
+ module Locale
16
+
17
+ module Info
18
+ # This class models out a region/country from the ISO 3166 standard for region codes.
19
+ # In ISO3166, it's called "Country" but Ruby/Locale the word "Region" instead.
20
+ class Region
21
+ attr_reader :code, :name
22
+
23
+ # code:: The 2 or 3 digit ISO 3166 region code.
24
+ # name:: The name of the region.
25
+ def initialize(code, name)
26
+ @code = code
27
+ @name = name
28
+ end
29
+
30
+ def iso_region?
31
+ @@regions[code] != nil
32
+ end
33
+
34
+ def to_s
35
+ "#{code}"
36
+ end
37
+ end
38
+
39
+ @@regions = Hash.new
40
+ Zlib::GzipReader.open(File.dirname(__FILE__) + "/../data/regions.tab.gz") do |gz|
41
+ gz.readlines.each do |l|
42
+ unless l =~ /^\s*$/
43
+ parts = l.split(/\t/)
44
+ region = Region.new(parts[0], parts[1].strip)
45
+ @@regions[parts[0]] = region
46
+ end
47
+ end
48
+ end
49
+
50
+ module_function
51
+
52
+ # Returns a hash of all the ISO regions. The hash is {String, Region} where
53
+ # the string is the 2 digit region code from the ISO 3166 data.
54
+ #
55
+ # You need to require 'locale/info' or 'locale/region'.
56
+ def regions
57
+ @@regions
58
+ end
59
+
60
+ # Returns the region for the given code.
61
+ #
62
+ # You need to require 'locale/info' or 'locale/info/region'.
63
+ def get_region(code)
64
+ @@regions[code]
65
+ end
66
+
67
+ # Returns the region code is valid.
68
+ #
69
+ # You need to require 'locale/info' or 'locale/info/region'.
70
+ def valid_region_code?(code)
71
+ @@regions[code] != nil
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,36 @@
1
+ =begin
2
+
3
+ tag.rb - Locale::Tag module
4
+
5
+ Copyright (C) 2008 Masao Mutoh
6
+
7
+ $Id: tag.rb 27 2008-12-03 15:06:50Z mutoh $
8
+ =end
9
+
10
+ require 'locale/tag/simple'
11
+ require 'locale/tag/illegular'
12
+ require 'locale/tag/common'
13
+ require 'locale/tag/rfc'
14
+ require 'locale/tag/cldr'
15
+ require 'locale/tag/posix'
16
+
17
+ module Locale
18
+
19
+ # Language tag / locale identifiers.
20
+ module Tag
21
+ module_function
22
+ # Parse a language tag/locale name and return Locale::Tag
23
+ # object.
24
+ # * tag: a tag as a String. e.g.) ja-Hira-JP
25
+ # * Returns: a Locale::Tag subclass.
26
+ def parse(tag)
27
+ # Common is not used here.
28
+ [Simple, Common, Rfc, Cldr, Posix].each do |parser|
29
+ ret = parser.parse(tag)
30
+ return ret if ret
31
+ end
32
+ Locale::Tag::Illegular.new(tag)
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,93 @@
1
+ =begin
2
+ locale/tag/cldr.rb - Locale::Tag::CLDR
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: cldr.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ module Locale #:nodoc:
13
+ module Tag #:nodoc:
14
+
15
+ # Unicode locale identifier class for CLDR-1.6.1.
16
+ # (Unicode Common Locale Data Repository).
17
+ class Cldr < Common
18
+
19
+ VARIANT = "(#{ALPHANUM}{5,8}|#{DIGIT}#{ALPHANUM}{3})"
20
+ EXTENSION = "#{ALPHANUM}+=[a-z0-9\-]+"
21
+
22
+ TAG_RE = /\A#{LANGUAGE}(?:[-_]#{SCRIPT})?
23
+ (?:[-_]#{REGION})?((?:[-_]#{VARIANT})*
24
+ (?:@(#{EXTENSION};?)+)*)\Z/ix
25
+
26
+ attr_reader :extensions
27
+
28
+ # Create Locale::Tag::Cldr.
29
+ #
30
+ # variants should be upcase.
31
+ def initialize(language, script = nil, region = nil,
32
+ variants = [], extensions = {})
33
+ @extensions = extensions
34
+ super(language, script, region, variants.map{|v| v.upcase})
35
+ end
36
+
37
+ # Parse the language tag and return the new Locale::Tag::CLDR.
38
+ def self.parse(tag)
39
+ if tag =~ /\APOSIX\Z/ # This is the special case of POSIX locale but match this regexp.
40
+ nil
41
+ elsif tag =~ TAG_RE
42
+ lang, script, region, subtag = $1, $2, $3, $4
43
+
44
+ extensions = {}
45
+ subtag.scan(/#{EXTENSION}/i).each{|v|
46
+ subtag.sub!(v, "")
47
+ key, type = v.split("=")
48
+ extensions[key] = type
49
+ }
50
+ variants = subtag.scan(/#{VARIANT}/i).collect{|v| v[0].upcase}
51
+
52
+ ret = self.new(lang, script, region, variants, extensions)
53
+ ret.tag = tag
54
+ ret
55
+ else
56
+ nil
57
+ end
58
+ end
59
+
60
+ # Returns the language tag.
61
+ # (e.g.) "ja_Hira_JP_VARIANT1_VARIANT2@foo1=var1;foo2=var2"
62
+ def to_s
63
+ s = super
64
+ if @extensions.size > 0
65
+ s << "@" << @extensions.to_a.sort.map{|k, v| "#{k}=#{v}"}.join(";")
66
+ end
67
+ s
68
+ end
69
+
70
+ # Sets the extensions.
71
+ def extensions=(val)
72
+ clear
73
+ @extensions = val
74
+ end
75
+
76
+ private
77
+ def convert_to(klass)
78
+ if klass == Cldr
79
+ klass.new(language, script, region, variants, extensions)
80
+ elsif klass == Rfc
81
+ exts = []
82
+ @extensions.each do |k, v|
83
+ exts << "k-#{k[0,8]}-#{v[0,8]}"
84
+ end
85
+
86
+ klass.new(language, script, region, variants, exts)
87
+ else
88
+ super
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,122 @@
1
+ =begin
2
+ locale/tag/common.rb - Locale::Tag::Common
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: common.rb 27 2008-12-03 15:06:50Z mutoh $
10
+ =end
11
+
12
+ module Locale
13
+ module Tag
14
+ # Common Language tag class for Ruby.
15
+ # Java and MS Windows use this format.
16
+ #
17
+ # * ja (language: RFC4646)
18
+ # * ja_JP (country: RFC4646(2 alpha or 3 digit))
19
+ # * ja-JP
20
+ # * ja_Hira_JP (script: 4 characters)
21
+ # * ja-Hira-JP
22
+ # * ja_Hira_JP_MOBILE (variants: more than 2 characters or 3 digit)
23
+ # * ja_Hira_JP_MOBILE_IPHONE (2 variants example)
24
+ #
25
+ class Common < Simple
26
+ LANGUAGE = "(#{ALPHA}{2,3}|#{ALPHA}{4}|#{ALPHA}{5,8})" #RFC4646 (ISO639/reserved/registered)
27
+ SCRIPT = "(#{ALPHA}{4})"
28
+ VARIANT = "(#{ALPHANUM}{3,}|#{DIGIT}#{ALPHANUM}{3})" #RFC3066 compatible
29
+
30
+ TAG_RE = /\A#{LANGUAGE}(?:[-_]#{SCRIPT})?
31
+ (?:[-_]#{REGION})?((?:[-_]#{VARIANT})*)\Z/ix
32
+
33
+ attr_reader :script, :variants
34
+
35
+ # Create a Locale::Tag::Common.
36
+ def initialize(language, script = nil, region = nil, variants = [])
37
+ @script, @variants = script, variants
38
+ @script.capitalize! if @script
39
+ super(language, region)
40
+ end
41
+
42
+ # Parse the language tag and return the new Locale::Tag::Common.
43
+ def self.parse(tag)
44
+ if tag =~ /\APOSIX\Z/ # This is the special case of POSIX locale but match this regexp.
45
+ nil
46
+ elsif tag =~ TAG_RE
47
+ lang, script, region, subtag = $1, $2, $3, $4
48
+ variants = subtag.scan(/(^|[-_])#{VARIANT}(?=([-_]|$))/i).collect{|v| v[1]}
49
+
50
+ ret = self.new(lang, script, region, variants)
51
+ ret.tag = tag
52
+ ret
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ # Set the script (with capitalize)
59
+ def script=(val)
60
+ clear
61
+ @script = val
62
+ @script.capitalize! if @script
63
+ @script
64
+ end
65
+
66
+ # Set the variants
67
+ def variants=(val)
68
+ clear
69
+ @variants = val
70
+ end
71
+
72
+ # Returns the common language tag with "_".
73
+ # <language>_<Script>_<REGION>_VARIANTS1_VARIANTS2
74
+ # (e.g.) "ja_Hira_JP_VARIANTS1_VARIANTS2"
75
+ def to_s
76
+ s = @language.dup
77
+
78
+ s << "_" << @script if @script
79
+ s << "_" << @region if @region
80
+
81
+ @variants.each do |v|
82
+ s << "_#{v}"
83
+ end
84
+ s
85
+ end
86
+
87
+ # Returns an Array of tag-candidates order by priority.
88
+ # Use Locale.candidates instead of this method.
89
+ #
90
+ # Locale::Tag::Rfc, Cldr don't have their own candidates,
91
+ # because it's meaningless to compare the extensions, privateuse, etc.
92
+ def candidates
93
+ [self.class.new(language, script, region, variants), #ja-Kana-JP-FOO
94
+ self.class.new(language, script, region), #ja-Kana-JP
95
+ self.class.new(language, nil, region, variants), #ja-JP-FOO
96
+ self.class.new(language, nil, region), #ja-JP
97
+ self.class.new(language, script, nil, variants), #ja-Kana-FOO
98
+ self.class.new(language, script), #ja-Kana
99
+ self.class.new(language, nil, nil, variants), #ja-FOO
100
+ self.class.new(language)] #ja
101
+ end
102
+
103
+ private
104
+ def convert_to(klass)
105
+ if klass == Simple
106
+ super
107
+ elsif klass == Posix
108
+ if variants.size > 0
109
+ var = variants.join("-")
110
+ else
111
+ var = nil
112
+ end
113
+ klass.new(language, region, nil, var)
114
+ elsif klass == Cldr
115
+ klass.new(language, script, region, variants.map{|v| v.upcase})
116
+ else
117
+ klass.new(language, script, region, variants)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end