icu 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +20 -0
  6. data/README.md +69 -0
  7. data/Rakefile +38 -0
  8. data/benchmark/normalization.rb +106 -0
  9. data/benchmark/normalization_phrases.txt +1031 -0
  10. data/benchmark/normalization_result.txt +45 -0
  11. data/benchmark/normalization_wikip.txt +2838 -0
  12. data/ext/icu/extconf.rb +242 -0
  13. data/ext/icu/icu.c +18 -0
  14. data/ext/icu/icu.h +78 -0
  15. data/ext/icu/icu_charset_detector.c +192 -0
  16. data/ext/icu/icu_collator.c +138 -0
  17. data/ext/icu/icu_locale.c +852 -0
  18. data/ext/icu/icu_normalizer.c +122 -0
  19. data/ext/icu/icu_number_format.c +0 -0
  20. data/ext/icu/icu_spoof_checker.c +194 -0
  21. data/ext/icu/icu_transliterator.c +159 -0
  22. data/ext/icu/internal_encoding.c +38 -0
  23. data/ext/icu/internal_ustring.c +304 -0
  24. data/ext/icu/internal_utils.c +50 -0
  25. data/ext/icu/rb_errors.c +14 -0
  26. data/icu.gemspec +22 -0
  27. data/lib/icu.rb +6 -18
  28. data/lib/icu/charset_detector.rb +5 -0
  29. data/lib/icu/collator.rb +24 -0
  30. data/lib/icu/locale.rb +19 -0
  31. data/lib/icu/transliterator.rb +8 -0
  32. data/lib/icu/version.rb +3 -0
  33. data/spec/charset_detector_spec.rb +47 -0
  34. data/spec/collator_spec.rb +73 -0
  35. data/spec/locale_spec.rb +312 -0
  36. data/spec/normalizer_spec.rb +35 -0
  37. data/spec/spec_helper.rb +8 -0
  38. data/spec/spoof_checker_spec.rb +56 -0
  39. data/spec/transliterator_spec.rb +41 -0
  40. metadata +132 -55
  41. data/COPYING +0 -674
  42. data/COPYING.LESSER +0 -165
  43. data/README +0 -81
  44. data/ext/extconf.rb +0 -31
  45. data/ext/icu.c +0 -128
  46. data/ext/icu.h +0 -34
  47. data/ext/icu_locale.c +0 -330
  48. data/ext/icu_locale_country.c +0 -99
  49. data/ext/icu_locale_language.c +0 -99
  50. data/ext/icu_numeric.c +0 -161
  51. data/ext/icu_time.c +0 -391
  52. data/test/test_locale.rb +0 -73
  53. data/test/test_numeric.rb +0 -78
  54. data/test/test_time.rb +0 -75
@@ -1,165 +0,0 @@
1
- GNU LESSER GENERAL PUBLIC LICENSE
2
- Version 3, 29 June 2007
3
-
4
- Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
- Everyone is permitted to copy and distribute verbatim copies
6
- of this license document, but changing it is not allowed.
7
-
8
-
9
- This version of the GNU Lesser General Public License incorporates
10
- the terms and conditions of version 3 of the GNU General Public
11
- License, supplemented by the additional permissions listed below.
12
-
13
- 0. Additional Definitions.
14
-
15
- As used herein, "this License" refers to version 3 of the GNU Lesser
16
- General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
- General Public License.
18
-
19
- "The Library" refers to a covered work governed by this License,
20
- other than an Application or a Combined Work as defined below.
21
-
22
- An "Application" is any work that makes use of an interface provided
23
- by the Library, but which is not otherwise based on the Library.
24
- Defining a subclass of a class defined by the Library is deemed a mode
25
- of using an interface provided by the Library.
26
-
27
- A "Combined Work" is a work produced by combining or linking an
28
- Application with the Library. The particular version of the Library
29
- with which the Combined Work was made is also called the "Linked
30
- Version".
31
-
32
- The "Minimal Corresponding Source" for a Combined Work means the
33
- Corresponding Source for the Combined Work, excluding any source code
34
- for portions of the Combined Work that, considered in isolation, are
35
- based on the Application, and not on the Linked Version.
36
-
37
- The "Corresponding Application Code" for a Combined Work means the
38
- object code and/or source code for the Application, including any data
39
- and utility programs needed for reproducing the Combined Work from the
40
- Application, but excluding the System Libraries of the Combined Work.
41
-
42
- 1. Exception to Section 3 of the GNU GPL.
43
-
44
- You may convey a covered work under sections 3 and 4 of this License
45
- without being bound by section 3 of the GNU GPL.
46
-
47
- 2. Conveying Modified Versions.
48
-
49
- If you modify a copy of the Library, and, in your modifications, a
50
- facility refers to a function or data to be supplied by an Application
51
- that uses the facility (other than as an argument passed when the
52
- facility is invoked), then you may convey a copy of the modified
53
- version:
54
-
55
- a) under this License, provided that you make a good faith effort to
56
- ensure that, in the event an Application does not supply the
57
- function or data, the facility still operates, and performs
58
- whatever part of its purpose remains meaningful, or
59
-
60
- b) under the GNU GPL, with none of the additional permissions of
61
- this License applicable to that copy.
62
-
63
- 3. Object Code Incorporating Material from Library Header Files.
64
-
65
- The object code form of an Application may incorporate material from
66
- a header file that is part of the Library. You may convey such object
67
- code under terms of your choice, provided that, if the incorporated
68
- material is not limited to numerical parameters, data structure
69
- layouts and accessors, or small macros, inline functions and templates
70
- (ten or fewer lines in length), you do both of the following:
71
-
72
- a) Give prominent notice with each copy of the object code that the
73
- Library is used in it and that the Library and its use are
74
- covered by this License.
75
-
76
- b) Accompany the object code with a copy of the GNU GPL and this license
77
- document.
78
-
79
- 4. Combined Works.
80
-
81
- You may convey a Combined Work under terms of your choice that,
82
- taken together, effectively do not restrict modification of the
83
- portions of the Library contained in the Combined Work and reverse
84
- engineering for debugging such modifications, if you also do each of
85
- the following:
86
-
87
- a) Give prominent notice with each copy of the Combined Work that
88
- the Library is used in it and that the Library and its use are
89
- covered by this License.
90
-
91
- b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
- document.
93
-
94
- c) For a Combined Work that displays copyright notices during
95
- execution, include the copyright notice for the Library among
96
- these notices, as well as a reference directing the user to the
97
- copies of the GNU GPL and this license document.
98
-
99
- d) Do one of the following:
100
-
101
- 0) Convey the Minimal Corresponding Source under the terms of this
102
- License, and the Corresponding Application Code in a form
103
- suitable for, and under terms that permit, the user to
104
- recombine or relink the Application with a modified version of
105
- the Linked Version to produce a modified Combined Work, in the
106
- manner specified by section 6 of the GNU GPL for conveying
107
- Corresponding Source.
108
-
109
- 1) Use a suitable shared library mechanism for linking with the
110
- Library. A suitable mechanism is one that (a) uses at run time
111
- a copy of the Library already present on the user's computer
112
- system, and (b) will operate properly with a modified version
113
- of the Library that is interface-compatible with the Linked
114
- Version.
115
-
116
- e) Provide Installation Information, but only if you would otherwise
117
- be required to provide such information under section 6 of the
118
- GNU GPL, and only to the extent that such information is
119
- necessary to install and execute a modified version of the
120
- Combined Work produced by recombining or relinking the
121
- Application with a modified version of the Linked Version. (If
122
- you use option 4d0, the Installation Information must accompany
123
- the Minimal Corresponding Source and Corresponding Application
124
- Code. If you use option 4d1, you must provide the Installation
125
- Information in the manner specified by section 6 of the GNU GPL
126
- for conveying Corresponding Source.)
127
-
128
- 5. Combined Libraries.
129
-
130
- You may place library facilities that are a work based on the
131
- Library side by side in a single library together with other library
132
- facilities that are not Applications and are not covered by this
133
- License, and convey such a combined library under terms of your
134
- choice, if you do both of the following:
135
-
136
- a) Accompany the combined library with a copy of the same work based
137
- on the Library, uncombined with any other library facilities,
138
- conveyed under the terms of this License.
139
-
140
- b) Give prominent notice with the combined library that part of it
141
- is a work based on the Library, and explaining where to find the
142
- accompanying uncombined form of the same work.
143
-
144
- 6. Revised Versions of the GNU Lesser General Public License.
145
-
146
- The Free Software Foundation may publish revised and/or new versions
147
- of the GNU Lesser General Public License from time to time. Such new
148
- versions will be similar in spirit to the present version, but may
149
- differ in detail to address new problems or concerns.
150
-
151
- Each version is given a distinguishing version number. If the
152
- Library as you received it specifies that a certain numbered version
153
- of the GNU Lesser General Public License "or any later version"
154
- applies to it, you have the option of following the terms and
155
- conditions either of that published version or of any later version
156
- published by the Free Software Foundation. If the Library as you
157
- received it does not specify a version number of the GNU Lesser
158
- General Public License, you may choose any version of the GNU Lesser
159
- General Public License ever published by the Free Software Foundation.
160
-
161
- If the Library as you received it specifies that a proxy can decide
162
- whether future versions of the GNU Lesser General Public License shall
163
- apply, that proxy's public statement of acceptance of any version is
164
- permanent authorization for you to choose that version for the
165
- Library.
data/README DELETED
@@ -1,81 +0,0 @@
1
- = ICU -- International Components for Unicode for Ruby
2
-
3
- Pragmatic integration of ICU (International Components for Unicode) into Ruby. Format numbers, dates, times and currency amounts according the conventions of a chosen locale.
4
-
5
- == Examples
6
-
7
- ICU::Locale.default = 'en_US'
8
- => 'en_US'
9
- locale = ICU::Locale.default
10
- => #<ICU::Locale:0x2ac572348c00 @locale_id="en_US">
11
- locale.country.name
12
- => 'Spain'
13
- locale.language.name
14
- => 'Spanish'
15
- locale.country.name('es_ES')
16
- => 'España'
17
- locale.language.name('es_ES')
18
- => 'español'
19
-
20
- ICU::Locale.available
21
- => [all installed locales]
22
- ICU::Locale.available(:allowed => ['es_ES', 'en_US'])
23
- => [["English (United States)", "en_US"], ["Spanish (Spain)", "es_ES"]]
24
-
25
- ICU::Locale.determine_from_http(request.env['HTTP_ACCEPT_LANGUAGE'])
26
- => 'es_ES'
27
-
28
- 1073741823.localize
29
- => '1,073,741,823'
30
- -1073741823.323.localize(:currency, :locale => 'he_IL')
31
- => '-1,073,741,823.32 ₪'
32
- 1.1239.localize(:spellout)
33
- => 'one point one two three nine'
34
- 43289.localize(:spellout, :locale => 'es_ES')
35
- => 'cuarenta y tres mil doscientos ochenta y nueve'
36
-
37
- Time.utc(2006, 5, 8, 8, 31, 34).localize(:date => :full, :locale => 'es_ES')
38
- => 'lunes 8 de mayo de 2006 8:31:34'
39
- Time.pattern(:time => :full, :locale => 'es_ES')
40
- => "dd/MM/yyyy HH'H'mm''ss\" z"
41
-
42
- See more in units tests in test/
43
-
44
-
45
- == Download
46
-
47
- The latest version of ICU can be found at
48
-
49
- * http://rubyforge.org/frs/?group_id=5009
50
-
51
-
52
- == Requeriments
53
-
54
- You must install ICU 3.6 or 3.8 from http://icu-project.org/download/
55
-
56
- See ext/extconf.rb
57
-
58
-
59
- == Installation
60
-
61
- The prefered method of installing ICU is through its GEM file. You'll need to have
62
- RubyGems[http://rubygems.rubyforge.org/wiki/wiki.pl] installed for that, though. If you have,
63
- then use:
64
-
65
- % [sudo] gem install icu-x.x.x.gem
66
-
67
- If fails try with:
68
-
69
- % [sudo] gem install icu-x.x.x.gem -- --with-icu-lib=/usr/local/lib --with-icu-include=/usr/local/src/icu/source/i18n
70
-
71
- See ext/extconf.rb
72
-
73
-
74
- == Support
75
-
76
- You can find the ICU RubyForge page at http://rubyforge.org/projects/icu.
77
- For other information, feel free to contact mailto:josh@vectrice.com.
78
-
79
- == License
80
-
81
- Copyright (c) 2007 Joshua Llorach, released under the GNU Lesser General Public License
@@ -1,31 +0,0 @@
1
- # CentOS:
2
- # cd /usr/local/src
3
- # wget http://download.icu-project.org/files/icu4c/3.8/icu4c-3_8-src.tgz
4
- # tar -xzvf icu4c-3_8-src.tgz
5
- # cd icu/source
6
- # ./runConfigureICU Linux
7
- # make
8
- # make install
9
- # cd /web/bandaancha.eu/www/vendor/plugins/g11n/lib
10
- # ruby extconf.rb --with-icu-lib=/usr/local/lib --with-icu-include=/usr/local/src/icu/source/i18n
11
- # make
12
- # ldd g11n.so y comprobar que no faltan librerias, si faltan:
13
- # export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/bin
14
- # vi /etc/profile y añado la anterior linea al final
15
-
16
-
17
- #$LDFLAGS = "icuuc.lib icuin.lib"
18
- #$LDFLAGS = "-licuuc -licui18n -licudata"
19
- #$LDFLAGS = "-licuuc -licui18n -licudata"
20
- #$CFLAGS = "-rpath=/usr/local/lib -L/usr/local/lib"
21
-
22
- require 'mkmf'
23
-
24
- dir_config('icu')
25
-
26
- if have_library('icui18n')
27
- create_makefile('icu')
28
- else
29
- puts "ICU v3.x required -- not found."
30
- exit 1
31
- end
data/ext/icu.c DELETED
@@ -1,128 +0,0 @@
1
- // Copyright (c) 2007 Joshua Llorach
2
- //
3
- // This file is part of ICU Ruby extension.
4
- //
5
- // ICU is free software: you can redistribute it and/or modify
6
- // it under the terms of the GNU Lesser General Public License as published by
7
- // the Free Software Foundation, either version 3 of the License, or
8
- // (at your option) any later version.
9
- //
10
- // ICU is distributed in the hope that it will be useful,
11
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- // GNU Lesser General Public License for more details.
14
- //
15
- // You should have received a copy of the GNU Lesser General Public License
16
- // along with ICU. If not, see <http://www.gnu.org/licenses/>.
17
-
18
- #include "icu.h"
19
-
20
- VALUE mICU;
21
-
22
- int validLocale(const char *locale_id)
23
- {
24
- const char *language, *country;
25
-
26
- language = uloc_getISO3Language(locale_id);
27
- country = uloc_getISO3Country(locale_id);
28
-
29
- return language[0] == 0x00 && country[0] == 0x00 ? 0 : 1;
30
- }
31
-
32
- int validLanguage(const char *code)
33
- {
34
- const char *language;
35
-
36
- if (strlen(code) > 3)
37
- return 0;
38
- language = uloc_getISO3Language(code);
39
-
40
- return language[0] == 0x00 ? 0 : 1;
41
- }
42
-
43
- int validCountry(const char *code)
44
- {
45
- const char *country;
46
- char fakeLocale[4] = "_";
47
-
48
- if (strlen(code) > 2)
49
- return 0;
50
- strcat(fakeLocale, code);
51
- country = uloc_getISO3Country(fakeLocale);
52
-
53
- return country[0] == 0x00 ? 0 : 1;
54
- }
55
-
56
- UChar *u_strFromRString(VALUE rStr, int32_t *resultLen)
57
- {
58
- UErrorCode errorCode;
59
- UChar *uStr = ALLOC_N(UChar, 64);
60
-
61
- if (resultLen == NULL) {
62
- resultLen = ALLOC_N(int32_t, 1);
63
- }
64
- errorCode = U_ZERO_ERROR;
65
- u_strFromUTF8(uStr, 64, resultLen, RSTRING(rStr)->ptr, RSTRING(rStr)->len, &errorCode);
66
- if (errorCode == U_STRING_NOT_TERMINATED_WARNING || errorCode == U_BUFFER_OVERFLOW_ERROR) {
67
- uStr = REALLOC_N(uStr, UChar, *resultLen + 1);
68
- errorCode = U_ZERO_ERROR;
69
- u_strFromUTF8(uStr, *resultLen + 1, resultLen, RSTRING(rStr)->ptr, RSTRING(rStr)->len, &errorCode);
70
- }
71
- RAISE_ON_ERROR(errorCode);
72
-
73
- return uStr;
74
- }
75
-
76
- VALUE u_strToRString(const UChar *uStr, int32_t uStrLen)
77
- {
78
- UErrorCode errorCode;
79
- char *result = ALLOC_N(char, 64);
80
- int32_t resultLen;
81
-
82
- errorCode = U_ZERO_ERROR;
83
- u_strToUTF8(result, 64, &resultLen, uStr, uStrLen, &errorCode);
84
- if (errorCode == U_STRING_NOT_TERMINATED_WARNING || errorCode == U_BUFFER_OVERFLOW_ERROR) {
85
- REALLOC_N(result, char, resultLen + 1);
86
- errorCode = U_ZERO_ERROR;
87
- u_strToUTF8(result, resultLen + 1, NULL, uStr, uStrLen, &errorCode);
88
- }
89
- RAISE_ON_ERROR(errorCode);
90
-
91
- return rb_str_new(result, resultLen);
92
- }
93
-
94
- VALUE collateByDisplayName(VALUE *a, VALUE *b, UCollator *collator)
95
- {
96
- UCharIterator sourceIter, targetIter;
97
- UErrorCode status = U_ZERO_ERROR;
98
- UCollationResult result;
99
-
100
- if (TYPE(*a) == T_ARRAY) {
101
- uiter_setUTF8(&sourceIter, RSTRING(RARRAY(*a)->ptr[0])->ptr, RSTRING(RARRAY(*a)->ptr[0])->len);
102
- uiter_setUTF8(&targetIter, RSTRING(RARRAY(*b)->ptr[0])->ptr, RSTRING(RARRAY(*b)->ptr[0])->len);
103
- } else {
104
- uiter_setUTF8(&sourceIter, RSTRING(*a)->ptr, RSTRING(*a)->len);
105
- uiter_setUTF8(&targetIter, RSTRING(*b)->ptr, RSTRING(*b)->len);
106
- }
107
- result = ucol_strcollIter(collator, &sourceIter, &targetIter, &status);
108
- RAISE_ON_ERROR(status);
109
-
110
- return result;
111
- }
112
-
113
- void Init_icu(void)
114
- {
115
- extern void Init_ICU_Locale(void);
116
- extern void Init_ICU_Numeric(void);
117
- extern void Init_ICU_Time(void);
118
- extern VALUE rb_numeric_localize(int argc, VALUE *argv, VALUE self);
119
-
120
- /* ICU */
121
- mICU = rb_define_module("ICU");
122
- /* ICU::Locale */
123
- Init_ICU_Locale();
124
- /* Numeric */
125
- Init_ICU_Numeric();
126
- /* Time */
127
- Init_ICU_Time();
128
- }
data/ext/icu.h DELETED
@@ -1,34 +0,0 @@
1
- // Copyright (c) 2007 Joshua Llorach
2
- //
3
- // This file is part of ICU Ruby extension.
4
- //
5
- // ICU is free software: you can redistribute it and/or modify
6
- // it under the terms of the GNU Lesser General Public License as published by
7
- // the Free Software Foundation, either version 3 of the License, or
8
- // (at your option) any later version.
9
- //
10
- // ICU is distributed in the hope that it will be useful,
11
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- // GNU Lesser General Public License for more details.
14
- //
15
- // You should have received a copy of the GNU Lesser General Public License
16
- // along with ICU. If not, see <http://www.gnu.org/licenses/>.
17
-
18
- #include "ruby.h"
19
-
20
- #include "unicode/uloc.h"
21
- #include "unicode/ucol.h"
22
-
23
- #define RAISE_ON_ERROR(e) if (U_FAILURE(e)) rb_raise(rb_eRuntimeError, u_errorName(e));
24
- #define SYMBOL2CSTR(s) rb_id2name(SYM2ID(s)));
25
- #define CheckLocaleID(x) if (!validLocale(x)) rb_raise(rb_eArgError, "Invalid locale id '%s'", x);
26
- #define CheckLanguageCode(x) if (!validLanguage(x)) rb_raise(rb_eArgError, "Invalid language code '%s'. Must be ISO639 2/3-letter.", x);
27
- #define CheckCountryCode(x) if (!validCountry(x)) rb_raise(rb_eArgError, "Invalid country code '%s'. Must be ISO3166 2-letter.", x);
28
-
29
- int validLocale(const char *locale_id);
30
- int validLanguage(const char *code);
31
- int validCountry(const char *code);
32
- UChar *u_strFromRString(VALUE rStr, int32_t *resultLen);
33
- VALUE u_strToRString(const UChar *uStr, int32_t uStrLen);
34
- VALUE collateByDisplayName(VALUE *a, VALUE *b, UCollator *collator);
@@ -1,330 +0,0 @@
1
- // Copyright (c) 2007 Joshua Llorach
2
- //
3
- // This file is part of ICU Ruby extension.
4
- //
5
- // ICU is free software: you can redistribute it and/or modify
6
- // it under the terms of the GNU Lesser General Public License as published by
7
- // the Free Software Foundation, either version 3 of the License, or
8
- // (at your option) any later version.
9
- //
10
- // ICU is distributed in the hope that it will be useful,
11
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- // GNU Lesser General Public License for more details.
14
- //
15
- // You should have received a copy of the GNU Lesser General Public License
16
- // along with ICU. If not, see <http://www.gnu.org/licenses/>.
17
-
18
- #include "icu.h"
19
-
20
- #include "unicode/ures.h"
21
-
22
- VALUE cLocale;
23
-
24
- /* Returns the default locale
25
- * call-seq:
26
- * ICU::Locale.default => locale
27
- */
28
- VALUE rb_ICU_Locale_singleton_default(VALUE self)
29
- {
30
- return rb_funcall(self, rb_intern("get"), 1, rb_str_new2(uloc_getDefault()));
31
- }
32
-
33
- /**
34
- * call-seq:
35
- * ICU::Locale.set(locale) => true
36
- *
37
- * Sets default locale.
38
- *
39
- * By default (without calling this function), default locale will be based on information obtained from the underlying system environment.
40
- * A value of NULL will try to get the system's default locale.
41
- *
42
- */
43
- VALUE rb_ICU_Locale_singleton_default_setter(VALUE self, VALUE locale)
44
- {
45
- char *cpLocale = NULL;
46
- UErrorCode status = U_ZERO_ERROR;
47
-
48
- if (locale != Qnil) {
49
- cpLocale = StringValuePtr(locale);
50
- CheckLocaleID(cpLocale);
51
- }
52
- uloc_setDefault(cpLocale, &status);
53
- RAISE_ON_ERROR(status);
54
-
55
- return Qnil;
56
- }
57
-
58
- /**
59
- * call-seq:
60
- * ICU::Locale.get => locale
61
- */
62
- VALUE rb_ICU_Locale_singleton_get(VALUE self, VALUE locale_id)
63
- {
64
- VALUE locales, locale;
65
-
66
- Check_Type(locale_id, T_STRING);
67
- locales = rb_iv_get(self, "@@locales");
68
- locale = rb_hash_aref(locales, locale_id);
69
- if (locale == Qnil) {
70
- VALUE args[1];
71
-
72
- args[0] = locale_id;
73
- locale = rb_class_new_instance(1, args, self);
74
- rb_hash_aset(locales, locale_id, locale);
75
- }
76
- return locale;
77
- }
78
-
79
- /**
80
- * call-seq:
81
- * ICU::Locale.availables([locale]) => Array
82
- */
83
- VALUE rb_ICU_Locale_singleton_available(int argc, VALUE *argv, VALUE self)
84
- {
85
- VALUE options;
86
- char *locale = NULL;
87
- VALUE allowed = Qfalse, languages = Qfalse, countries = Qfalse, group = Qfalse;
88
-
89
- long i;
90
- int32_t length = uloc_countAvailable();
91
- UErrorCode status = U_ZERO_ERROR;
92
- VALUE select = rb_ary_new2(length);
93
-
94
- UCollator *collator;
95
-
96
- rb_scan_args(argc, argv, "01", &options);
97
- if (options != Qnil) {
98
- VALUE rb_locale;
99
-
100
- Check_Type(options, T_HASH);
101
- rb_locale = rb_hash_aref(options, ID2SYM(rb_intern("locale")));
102
- if (rb_locale != Qnil) {
103
- locale = StringValuePtr(rb_locale);
104
- }
105
- allowed = rb_hash_aref(options, ID2SYM(rb_intern("allowed")));
106
- if (allowed == Qnil) {
107
- allowed = Qfalse;
108
- } else {
109
- Check_Type(allowed, T_ARRAY);
110
- }
111
- languages = rb_hash_aref(options, ID2SYM(rb_intern("languages")));
112
- if (languages == Qnil) {
113
- languages = Qfalse;
114
- } else {
115
- Check_Type(languages, T_ARRAY);
116
- }
117
- countries = rb_hash_aref(options, ID2SYM(rb_intern("countries")));
118
- if (countries == Qnil) {
119
- countries = Qfalse;
120
- } else {
121
- Check_Type(countries, T_ARRAY);
122
- }
123
- group = rb_hash_aref(options, ID2SYM(rb_intern("group")));
124
- group = group && group != Qnil ? Qtrue : Qfalse;
125
- }
126
-
127
- for (i = 0; i < length; i++) {
128
- const char *availableLocale = uloc_getAvailable(i);
129
-
130
- UChar *displayName = ALLOC_N(UChar, 64);
131
- int32_t displayNameLen = 64;
132
- VALUE option;
133
-
134
- if (allowed && !rb_ary_includes(allowed, rb_str_new2(availableLocale)))
135
- continue;
136
- if (languages) {
137
- char language[3];
138
- int32_t languageLen = 3;
139
-
140
- languageLen = uloc_getLanguage(availableLocale, language, languageLen, &status);
141
- RAISE_ON_ERROR(status);
142
- if (!rb_ary_includes(languages, rb_str_new(language, languageLen)))
143
- continue;
144
- }
145
- if (countries) {
146
- char country[3];
147
- int32_t countryLen = 3;
148
-
149
- countryLen = uloc_getCountry(availableLocale, country, countryLen, &status);
150
- RAISE_ON_ERROR(status);
151
- if (!rb_ary_includes(countries, rb_str_new(country, countryLen)))
152
- continue;
153
- }
154
- displayNameLen = uloc_getDisplayName(availableLocale, locale, displayName, 64, &status);
155
- if (status == U_BUFFER_OVERFLOW_ERROR) {
156
- status = U_ZERO_ERROR;
157
- REALLOC_N(displayName, UChar, displayNameLen);
158
- uloc_getDisplayName(availableLocale, locale, displayName, displayNameLen, &status);
159
- }
160
- RAISE_ON_ERROR(status);
161
-
162
- option = rb_ary_new2(2);
163
- rb_ary_push(option, u_strToRString(displayName, displayNameLen));
164
- rb_ary_push(option, rb_str_new2(availableLocale));
165
- rb_ary_push(select, option);
166
- }
167
- /* */
168
-
169
- collator = ucol_open(locale, &status);
170
- RAISE_ON_ERROR(status);
171
- if (group) {
172
- char groupLanguageCode[3] = "";
173
- VALUE tmpSelect = rb_ary_new();
174
- VALUE options;
175
-
176
- //ruby_qsort(RARRAY(select)->ptr, RARRAY(select)->len, sizeof(VALUE), collateByLocaleID, collator);
177
- for (i = 0; i < RARRAY(select)->len; i++) {
178
- char languageCode[4];
179
- int32_t languageCodeLen = 4;
180
-
181
- languageCodeLen = uloc_getLanguage(RSTRING(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, languageCode, languageCodeLen, &status);
182
- RAISE_ON_ERROR(status);
183
-
184
- if (strcmp(groupLanguageCode, languageCode)) {
185
- UChar *languageName = ALLOC_N(UChar, 32);
186
- int32_t languageNameLen = 32;
187
- VALUE group = rb_ary_new2(2);
188
-
189
- //get display name
190
- languageNameLen = uloc_getDisplayLanguage(RSTRING(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, locale, languageName, languageNameLen, &status);
191
- if (status == U_BUFFER_OVERFLOW_ERROR) {
192
- status = U_ZERO_ERROR;
193
- REALLOC_N(languageName, UChar, languageNameLen);
194
- uloc_getDisplayLanguage(RSTRING(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, locale, languageName, languageNameLen, &status);
195
- }
196
- RAISE_ON_ERROR(status);
197
- //create group
198
- rb_ary_push(group, u_strToRString(languageName, languageNameLen));
199
- options = rb_ary_new();
200
- rb_ary_push(group, options);
201
- rb_ary_push(tmpSelect, group);
202
-
203
- strcpy(groupLanguageCode, languageCode);
204
- }
205
- //add option
206
- rb_ary_push(options, RARRAY(select)->ptr[i]);
207
- }
208
- select = tmpSelect;
209
- for (i = 0; i < RARRAY(select)->len; i++) {
210
- ruby_qsort(RARRAY(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, RARRAY(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->len, sizeof(VALUE), collateByDisplayName, collator);
211
- }
212
- }
213
- ruby_qsort(RARRAY(select)->ptr, RARRAY(select)->len, sizeof(VALUE), collateByDisplayName, collator);
214
- ucol_close(collator);
215
-
216
- return select;
217
- }
218
-
219
- /**
220
- * call-seq:
221
- * ICU::Locale.determine_from_http(accept_language_header) => locale
222
- */
223
- VALUE rb_ICU_Locale_singleton_determine_from_http(VALUE self, VALUE acceptLanguageHeader)
224
- {
225
- UErrorCode status = U_ZERO_ERROR;
226
- UEnumeration *availableLocales;
227
- char *locale = ALLOC_N(char, 16);
228
- int32_t localeLen;
229
- UAcceptResult outResult;
230
-
231
- availableLocales = ures_openAvailableLocales(NULL, &status);
232
- RAISE_ON_ERROR(status);
233
- localeLen = uloc_acceptLanguageFromHTTP(locale, 16, &outResult, RSTRING(acceptLanguageHeader)->ptr, availableLocales, &status);
234
- if (status == U_BUFFER_OVERFLOW_ERROR) {
235
- status = U_ZERO_ERROR;
236
- REALLOC_N(locale, char, localeLen);
237
- uloc_acceptLanguageFromHTTP(locale, localeLen, &outResult, RSTRING(acceptLanguageHeader)->ptr, availableLocales, &status);
238
- }
239
- RAISE_ON_ERROR(status);
240
-
241
- return rb_str_new(locale, localeLen);
242
- }
243
-
244
- VALUE rb_ICU_Locale_initialize(VALUE self, VALUE locale_id)
245
- {
246
- Check_Type(locale_id, T_STRING);
247
- CheckLocaleID(StringValuePtr(locale_id));
248
- rb_iv_set(self, "@locale_id", locale_id);
249
-
250
- return self;
251
- }
252
-
253
- VALUE rb_ICU_Locale_parent(VALUE self)
254
- {
255
- char parent[16];
256
- int32_t parentLen = 16;
257
- UErrorCode status = U_ZERO_ERROR;
258
-
259
- parentLen = uloc_getParent(RSTRING(rb_iv_get(self, "@locale_id"))->ptr, parent, parentLen, &status);
260
-
261
- return parentLen ? rb_funcall(cLocale, rb_intern("get"), 1, rb_str_new(parent, parentLen)) : Qnil;
262
- }
263
-
264
- VALUE rb_ICU_Locale_country(VALUE self)
265
- {
266
- extern VALUE cCountry;
267
-
268
- VALUE country;
269
-
270
- country = rb_iv_get(self, "@country");
271
- if (country == Qnil) {
272
- char country[3];
273
- int32_t countryLen = 3;
274
- UErrorCode status = U_ZERO_ERROR;
275
-
276
- countryLen = uloc_getCountry(RSTRING(rb_iv_get(self, "@locale_id"))->ptr, country, countryLen, &status);
277
- RAISE_ON_ERROR(status);
278
-
279
- return rb_iv_set(self, "@country", countryLen ? rb_funcall(cCountry, rb_intern("get"), 1, rb_str_new(country, countryLen)) : Qnil);
280
- } else {
281
- return country;
282
- }
283
- }
284
-
285
- VALUE rb_ICU_Locale_language(VALUE self)
286
- {
287
- extern VALUE cLanguage;
288
-
289
- VALUE language;
290
-
291
- language = rb_iv_get(self, "@language");
292
- if (language == Qnil) {
293
- char language[3];
294
- int32_t languageLen = 3;
295
- UErrorCode status = U_ZERO_ERROR;
296
-
297
- languageLen = uloc_getLanguage(RSTRING(rb_iv_get(self, "@locale_id"))->ptr, language, languageLen, &status);
298
- RAISE_ON_ERROR(status);
299
-
300
- return rb_iv_set(self, "@language", languageLen ? rb_funcall(cLanguage, rb_intern("get"), 1, rb_str_new(language, languageLen)) : Qnil);
301
- } else {
302
- return language;
303
- }
304
- }
305
-
306
- void Init_ICU_Locale(void)
307
- {
308
- extern VALUE mICU;
309
- extern void Init_ICU_Locale_Country(void);
310
- extern void Init_ICU_Locale_Language(void);
311
-
312
- cLocale = rb_define_class_under(mICU, "Locale", rb_cObject);
313
- rb_define_singleton_method(cLocale, "default", rb_ICU_Locale_singleton_default, 0);
314
- rb_define_singleton_method(cLocale, "default=", rb_ICU_Locale_singleton_default_setter, 1);
315
- rb_define_singleton_method(cLocale, "get", rb_ICU_Locale_singleton_get, 1);
316
- rb_define_singleton_method(cLocale, "available", rb_ICU_Locale_singleton_available, -1);
317
- rb_define_singleton_method(cLocale, "determine_from_http", rb_ICU_Locale_singleton_determine_from_http, 1);
318
- rb_funcall(cLocale, rb_intern("private_class_method"), 1, ID2SYM(rb_intern("new")));
319
- rb_iv_set(cLocale, "@@locales", rb_hash_new());
320
- rb_define_private_method(cLocale, "initialize", rb_ICU_Locale_initialize, 1);
321
- rb_define_attr(cLocale, "locale_id", 1, 0);
322
- rb_define_alias(cLocale, "to_s", "locale_id");
323
- rb_define_alias(cLocale, "to_str", "locale_id");
324
- rb_define_method(cLocale, "parent", rb_ICU_Locale_parent, 0);
325
- rb_define_method(cLocale, "country", rb_ICU_Locale_country, 0);
326
- rb_define_method(cLocale, "language", rb_ICU_Locale_language, 0);
327
-
328
- Init_ICU_Locale_Country();
329
- Init_ICU_Locale_Language();
330
- }