icu 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,21 @@
1
+ = ICU -- International Components for Unicode for Ruby
2
+
3
+
4
+
5
+ == Download
6
+
7
+ The latest version of ICU can be found at
8
+
9
+ * http://rubyforge.org/
10
+
11
+ == Installation
12
+
13
+ The prefered method of installing ICU is through its GEM file. You'll need to have
14
+ RubyGems[http://rubygems.rubyforge.org/wiki/wiki.pl] installed for that, though. If you have,
15
+ then use:
16
+
17
+ % [sudo] gem install icu
18
+
19
+ == License
20
+
21
+ Active Record is released under the Ruby license.
@@ -0,0 +1,31 @@
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
@@ -0,0 +1,111 @@
1
+ #include "icu.h"
2
+
3
+ VALUE mICU;
4
+
5
+ int validLocale(const char *locale_id)
6
+ {
7
+ const char *language, *country;
8
+
9
+ language = uloc_getISO3Language(locale_id);
10
+ country = uloc_getISO3Country(locale_id);
11
+
12
+ return language[0] == 0x00 && country[0] == 0x00 ? 0 : 1;
13
+ }
14
+
15
+ int validLanguage(const char *code)
16
+ {
17
+ const char *language;
18
+
19
+ if (strlen(code) > 3)
20
+ return 0;
21
+ language = uloc_getISO3Language(code);
22
+
23
+ return language[0] == 0x00 ? 0 : 1;
24
+ }
25
+
26
+ int validCountry(const char *code)
27
+ {
28
+ const char *country;
29
+ char fakeLocale[4] = "_";
30
+
31
+ if (strlen(code) > 2)
32
+ return 0;
33
+ strcat(fakeLocale, code);
34
+ country = uloc_getISO3Country(fakeLocale);
35
+
36
+ return country[0] == 0x00 ? 0 : 1;
37
+ }
38
+
39
+ UChar *u_strFromRString(VALUE rStr, int32_t *resultLen)
40
+ {
41
+ UErrorCode errorCode;
42
+ UChar *uStr = ALLOC_N(UChar, 64);
43
+
44
+ if (resultLen == NULL) {
45
+ resultLen = ALLOC_N(int32_t, 1);
46
+ }
47
+ errorCode = U_ZERO_ERROR;
48
+ u_strFromUTF8(uStr, 64, resultLen, RSTRING(rStr)->ptr, RSTRING(rStr)->len, &errorCode);
49
+ if (errorCode == U_STRING_NOT_TERMINATED_WARNING || errorCode == U_BUFFER_OVERFLOW_ERROR) {
50
+ uStr = REALLOC_N(uStr, UChar, *resultLen + 1);
51
+ errorCode = U_ZERO_ERROR;
52
+ u_strFromUTF8(uStr, *resultLen + 1, resultLen, RSTRING(rStr)->ptr, RSTRING(rStr)->len, &errorCode);
53
+ }
54
+ RAISE_ON_ERROR(errorCode);
55
+
56
+ return uStr;
57
+ }
58
+
59
+ VALUE u_strToRString(const UChar *uStr, int32_t uStrLen)
60
+ {
61
+ UErrorCode errorCode;
62
+ char *result = ALLOC_N(char, 64);
63
+ int32_t resultLen;
64
+
65
+ errorCode = U_ZERO_ERROR;
66
+ u_strToUTF8(result, 64, &resultLen, uStr, uStrLen, &errorCode);
67
+ if (errorCode == U_STRING_NOT_TERMINATED_WARNING || errorCode == U_BUFFER_OVERFLOW_ERROR) {
68
+ REALLOC_N(result, char, resultLen + 1);
69
+ errorCode = U_ZERO_ERROR;
70
+ u_strToUTF8(result, resultLen + 1, NULL, uStr, uStrLen, &errorCode);
71
+ }
72
+ RAISE_ON_ERROR(errorCode);
73
+
74
+ return rb_str_new(result, resultLen);
75
+ }
76
+
77
+ VALUE collateByDisplayName(VALUE *a, VALUE *b, UCollator *collator)
78
+ {
79
+ UCharIterator sourceIter, targetIter;
80
+ UErrorCode status = U_ZERO_ERROR;
81
+ UCollationResult result;
82
+
83
+ if (TYPE(*a) == T_ARRAY) {
84
+ uiter_setUTF8(&sourceIter, RSTRING(RARRAY(*a)->ptr[0])->ptr, RSTRING(RARRAY(*a)->ptr[0])->len);
85
+ uiter_setUTF8(&targetIter, RSTRING(RARRAY(*b)->ptr[0])->ptr, RSTRING(RARRAY(*b)->ptr[0])->len);
86
+ } else {
87
+ uiter_setUTF8(&sourceIter, RSTRING(*a)->ptr, RSTRING(*a)->len);
88
+ uiter_setUTF8(&targetIter, RSTRING(*b)->ptr, RSTRING(*b)->len);
89
+ }
90
+ result = ucol_strcollIter(collator, &sourceIter, &targetIter, &status);
91
+ RAISE_ON_ERROR(status);
92
+
93
+ return result;
94
+ }
95
+
96
+ void Init_icu(void)
97
+ {
98
+ extern void Init_ICU_Locale(void);
99
+ extern void Init_ICU_Numeric(void);
100
+ extern void Init_ICU_Time(void);
101
+ extern VALUE rb_numeric_localize(int argc, VALUE *argv, VALUE self);
102
+
103
+ /* ICU */
104
+ mICU = rb_define_module("ICU");
105
+ /* ICU::Locale */
106
+ Init_ICU_Locale();
107
+ /* Numeric */
108
+ Init_ICU_Numeric();
109
+ /* Time */
110
+ Init_ICU_Time();
111
+ }
@@ -0,0 +1,17 @@
1
+ #include "ruby.h"
2
+
3
+ #include "unicode/uloc.h"
4
+ #include "unicode/ucol.h"
5
+
6
+ #define RAISE_ON_ERROR(e) if (U_FAILURE(e)) rb_raise(rb_eRuntimeError, u_errorName(e));
7
+ #define SYMBOL2CSTR(s) rb_id2name(SYM2ID(s)));
8
+ #define CheckLocaleID(x) if (!validLocale(x)) rb_raise(rb_eArgError, "Invalid locale id '%s'", x);
9
+ #define CheckLanguageCode(x) if (!validLanguage(x)) rb_raise(rb_eArgError, "Invalid language code '%s'. Must be ISO639 2/3-letter.", x);
10
+ #define CheckCountryCode(x) if (!validCountry(x)) rb_raise(rb_eArgError, "Invalid country code '%s'. Must be ISO3166 2-letter.", x);
11
+
12
+ int validLocale(const char *locale_id);
13
+ int validLanguage(const char *code);
14
+ int validCountry(const char *code);
15
+ UChar *u_strFromRString(VALUE rStr, int32_t *resultLen);
16
+ VALUE u_strToRString(const UChar *uStr, int32_t uStrLen);
17
+ VALUE collateByDisplayName(VALUE *a, VALUE *b, UCollator *collator);
@@ -0,0 +1,313 @@
1
+ #include "icu.h"
2
+
3
+ #include "unicode/ures.h"
4
+
5
+ VALUE cLocale;
6
+
7
+ /* Returns the default locale
8
+ * call-seq:
9
+ * ICU::Locale.default => locale
10
+ */
11
+ VALUE rb_ICU_Locale_singleton_default(VALUE self)
12
+ {
13
+ return rb_funcall(self, rb_intern("get"), 1, rb_str_new2(uloc_getDefault()));
14
+ }
15
+
16
+ /**
17
+ * call-seq:
18
+ * ICU::Locale.set(locale) => true
19
+ *
20
+ * Sets default locale.
21
+ *
22
+ * By default (without calling this function), default locale will be based on information obtained from the underlying system environment.
23
+ * A value of NULL will try to get the system's default locale.
24
+ *
25
+ */
26
+ VALUE rb_ICU_Locale_singleton_default_setter(VALUE self, VALUE locale)
27
+ {
28
+ char *cpLocale = NULL;
29
+ UErrorCode status = U_ZERO_ERROR;
30
+
31
+ if (locale != Qnil) {
32
+ cpLocale = StringValuePtr(locale);
33
+ CheckLocaleID(cpLocale);
34
+ }
35
+ uloc_setDefault(cpLocale, &status);
36
+ RAISE_ON_ERROR(status);
37
+
38
+ return Qnil;
39
+ }
40
+
41
+ /**
42
+ * call-seq:
43
+ * ICU::Locale.get => locale
44
+ */
45
+ VALUE rb_ICU_Locale_singleton_get(VALUE self, VALUE locale_id)
46
+ {
47
+ VALUE locales, locale;
48
+
49
+ Check_Type(locale_id, T_STRING);
50
+ locales = rb_iv_get(self, "@@locales");
51
+ locale = rb_hash_aref(locales, locale_id);
52
+ if (locale == Qnil) {
53
+ VALUE args[1];
54
+
55
+ args[0] = locale_id;
56
+ locale = rb_class_new_instance(1, args, self);
57
+ rb_hash_aset(locales, locale_id, locale);
58
+ }
59
+ return locale;
60
+ }
61
+
62
+ /**
63
+ * call-seq:
64
+ * ICU::Locale.availables([locale]) => Array
65
+ */
66
+ VALUE rb_ICU_Locale_singleton_available(int argc, VALUE *argv, VALUE self)
67
+ {
68
+ VALUE options;
69
+ char *locale = NULL;
70
+ VALUE allowed = Qfalse, languages = Qfalse, countries = Qfalse, group = Qfalse;
71
+
72
+ long i;
73
+ int32_t length = uloc_countAvailable();
74
+ UErrorCode status = U_ZERO_ERROR;
75
+ VALUE select = rb_ary_new2(length);
76
+
77
+ UCollator *collator;
78
+
79
+ rb_scan_args(argc, argv, "01", &options);
80
+ if (options != Qnil) {
81
+ VALUE rb_locale;
82
+
83
+ Check_Type(options, T_HASH);
84
+ rb_locale = rb_hash_aref(options, ID2SYM(rb_intern("locale")));
85
+ if (rb_locale != Qnil) {
86
+ locale = StringValuePtr(rb_locale);
87
+ }
88
+ allowed = rb_hash_aref(options, ID2SYM(rb_intern("allowed")));
89
+ if (allowed == Qnil) {
90
+ allowed = Qfalse;
91
+ } else {
92
+ Check_Type(allowed, T_ARRAY);
93
+ }
94
+ languages = rb_hash_aref(options, ID2SYM(rb_intern("languages")));
95
+ if (languages == Qnil) {
96
+ languages = Qfalse;
97
+ } else {
98
+ Check_Type(languages, T_ARRAY);
99
+ }
100
+ countries = rb_hash_aref(options, ID2SYM(rb_intern("countries")));
101
+ if (countries == Qnil) {
102
+ countries = Qfalse;
103
+ } else {
104
+ Check_Type(countries, T_ARRAY);
105
+ }
106
+ group = rb_hash_aref(options, ID2SYM(rb_intern("group")));
107
+ group = group && group != Qnil ? Qtrue : Qfalse;
108
+ }
109
+
110
+ for (i = 0; i < length; i++) {
111
+ const char *availableLocale = uloc_getAvailable(i);
112
+
113
+ UChar *displayName = ALLOC_N(UChar, 64);
114
+ int32_t displayNameLen = 64;
115
+ VALUE option;
116
+
117
+ if (allowed && !rb_ary_includes(allowed, rb_str_new2(availableLocale)))
118
+ continue;
119
+ if (languages) {
120
+ char language[3];
121
+ int32_t languageLen = 3;
122
+
123
+ languageLen = uloc_getLanguage(availableLocale, language, languageLen, &status);
124
+ RAISE_ON_ERROR(status);
125
+ if (!rb_ary_includes(languages, rb_str_new(language, languageLen)))
126
+ continue;
127
+ }
128
+ if (countries) {
129
+ char country[3];
130
+ int32_t countryLen = 3;
131
+
132
+ countryLen = uloc_getCountry(availableLocale, country, countryLen, &status);
133
+ RAISE_ON_ERROR(status);
134
+ if (!rb_ary_includes(countries, rb_str_new(country, countryLen)))
135
+ continue;
136
+ }
137
+ displayNameLen = uloc_getDisplayName(availableLocale, locale, displayName, 64, &status);
138
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
139
+ status = U_ZERO_ERROR;
140
+ REALLOC_N(displayName, UChar, displayNameLen);
141
+ uloc_getDisplayName(availableLocale, locale, displayName, displayNameLen, &status);
142
+ }
143
+ RAISE_ON_ERROR(status);
144
+
145
+ option = rb_ary_new2(2);
146
+ rb_ary_push(option, u_strToRString(displayName, displayNameLen));
147
+ rb_ary_push(option, rb_str_new2(availableLocale));
148
+ rb_ary_push(select, option);
149
+ }
150
+ /* */
151
+
152
+ collator = ucol_open(locale, &status);
153
+ RAISE_ON_ERROR(status);
154
+ if (group) {
155
+ char groupLanguageCode[3] = "";
156
+ VALUE tmpSelect = rb_ary_new();
157
+ VALUE options;
158
+
159
+ //ruby_qsort(RARRAY(select)->ptr, RARRAY(select)->len, sizeof(VALUE), collateByLocaleID, collator);
160
+ for (i = 0; i < RARRAY(select)->len; i++) {
161
+ char languageCode[4];
162
+ int32_t languageCodeLen = 4;
163
+
164
+ languageCodeLen = uloc_getLanguage(RSTRING(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, languageCode, languageCodeLen, &status);
165
+ RAISE_ON_ERROR(status);
166
+
167
+ if (strcmp(groupLanguageCode, languageCode)) {
168
+ UChar *languageName = ALLOC_N(UChar, 32);
169
+ int32_t languageNameLen = 32;
170
+ VALUE group = rb_ary_new2(2);
171
+
172
+ //get display name
173
+ languageNameLen = uloc_getDisplayLanguage(RSTRING(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, locale, languageName, languageNameLen, &status);
174
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
175
+ status = U_ZERO_ERROR;
176
+ REALLOC_N(languageName, UChar, languageNameLen);
177
+ uloc_getDisplayLanguage(RSTRING(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, locale, languageName, languageNameLen, &status);
178
+ }
179
+ RAISE_ON_ERROR(status);
180
+ //create group
181
+ rb_ary_push(group, u_strToRString(languageName, languageNameLen));
182
+ options = rb_ary_new();
183
+ rb_ary_push(group, options);
184
+ rb_ary_push(tmpSelect, group);
185
+
186
+ strcpy(groupLanguageCode, languageCode);
187
+ }
188
+ //add option
189
+ rb_ary_push(options, RARRAY(select)->ptr[i]);
190
+ }
191
+ select = tmpSelect;
192
+ for (i = 0; i < RARRAY(select)->len; i++) {
193
+ ruby_qsort(RARRAY(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->ptr, RARRAY(RARRAY(RARRAY(select)->ptr[i])->ptr[1])->len, sizeof(VALUE), collateByDisplayName, collator);
194
+ }
195
+ }
196
+ ruby_qsort(RARRAY(select)->ptr, RARRAY(select)->len, sizeof(VALUE), collateByDisplayName, collator);
197
+ ucol_close(collator);
198
+
199
+ return select;
200
+ }
201
+
202
+ /**
203
+ * call-seq:
204
+ * ICU::Locale.determine_from_http(accept_language_header) => locale
205
+ */
206
+ VALUE rb_ICU_Locale_singleton_determine_from_http(VALUE self, VALUE acceptLanguageHeader)
207
+ {
208
+ UErrorCode status = U_ZERO_ERROR;
209
+ UEnumeration *availableLocales;
210
+ char *locale = ALLOC_N(char, 16);
211
+ int32_t localeLen;
212
+ UAcceptResult outResult;
213
+
214
+ availableLocales = ures_openAvailableLocales(NULL, &status);
215
+ RAISE_ON_ERROR(status);
216
+ localeLen = uloc_acceptLanguageFromHTTP(locale, 16, &outResult, RSTRING(acceptLanguageHeader)->ptr, availableLocales, &status);
217
+ if (status == U_BUFFER_OVERFLOW_ERROR) {
218
+ status = U_ZERO_ERROR;
219
+ REALLOC_N(locale, char, localeLen);
220
+ uloc_acceptLanguageFromHTTP(locale, localeLen, &outResult, RSTRING(acceptLanguageHeader)->ptr, availableLocales, &status);
221
+ }
222
+ RAISE_ON_ERROR(status);
223
+
224
+ return rb_str_new(locale, localeLen);
225
+ }
226
+
227
+ VALUE rb_ICU_Locale_initialize(VALUE self, VALUE locale_id)
228
+ {
229
+ Check_Type(locale_id, T_STRING);
230
+ CheckLocaleID(StringValuePtr(locale_id));
231
+ rb_iv_set(self, "@locale_id", locale_id);
232
+
233
+ return self;
234
+ }
235
+
236
+ VALUE rb_ICU_Locale_parent(VALUE self)
237
+ {
238
+ char parent[16];
239
+ int32_t parentLen = 16;
240
+ UErrorCode status = U_ZERO_ERROR;
241
+
242
+ parentLen = uloc_getParent(RSTRING(rb_iv_get(self, "@locale_id"))->ptr, parent, parentLen, &status);
243
+
244
+ return parentLen ? rb_funcall(cLocale, rb_intern("get"), 1, rb_str_new(parent, parentLen)) : Qnil;
245
+ }
246
+
247
+ VALUE rb_ICU_Locale_country(VALUE self)
248
+ {
249
+ extern VALUE cCountry;
250
+
251
+ VALUE country;
252
+
253
+ country = rb_iv_get(self, "@country");
254
+ if (country == Qnil) {
255
+ char country[3];
256
+ int32_t countryLen = 3;
257
+ UErrorCode status = U_ZERO_ERROR;
258
+
259
+ countryLen = uloc_getCountry(RSTRING(rb_iv_get(self, "@locale_id"))->ptr, country, countryLen, &status);
260
+ RAISE_ON_ERROR(status);
261
+
262
+ return rb_iv_set(self, "@country", countryLen ? rb_funcall(cCountry, rb_intern("get"), 1, rb_str_new(country, countryLen)) : Qnil);
263
+ } else {
264
+ return country;
265
+ }
266
+ }
267
+
268
+ VALUE rb_ICU_Locale_language(VALUE self)
269
+ {
270
+ extern VALUE cLanguage;
271
+
272
+ VALUE language;
273
+
274
+ language = rb_iv_get(self, "@language");
275
+ if (language == Qnil) {
276
+ char language[3];
277
+ int32_t languageLen = 3;
278
+ UErrorCode status = U_ZERO_ERROR;
279
+
280
+ languageLen = uloc_getLanguage(RSTRING(rb_iv_get(self, "@locale_id"))->ptr, language, languageLen, &status);
281
+ RAISE_ON_ERROR(status);
282
+
283
+ return rb_iv_set(self, "@language", languageLen ? rb_funcall(cLanguage, rb_intern("get"), 1, rb_str_new(language, languageLen)) : Qnil);
284
+ } else {
285
+ return language;
286
+ }
287
+ }
288
+
289
+ void Init_ICU_Locale(void)
290
+ {
291
+ extern VALUE mICU;
292
+ extern void Init_ICU_Locale_Country(void);
293
+ extern void Init_ICU_Locale_Language(void);
294
+
295
+ cLocale = rb_define_class_under(mICU, "Locale", rb_cObject);
296
+ rb_define_singleton_method(cLocale, "default", rb_ICU_Locale_singleton_default, 0);
297
+ rb_define_singleton_method(cLocale, "default=", rb_ICU_Locale_singleton_default_setter, 1);
298
+ rb_define_singleton_method(cLocale, "get", rb_ICU_Locale_singleton_get, 1);
299
+ rb_define_singleton_method(cLocale, "available", rb_ICU_Locale_singleton_available, -1);
300
+ rb_define_singleton_method(cLocale, "determine_from_http", rb_ICU_Locale_singleton_determine_from_http, 1);
301
+ rb_funcall(cLocale, rb_intern("private_class_method"), 1, ID2SYM(rb_intern("new")));
302
+ rb_iv_set(cLocale, "@@locales", rb_hash_new());
303
+ rb_define_private_method(cLocale, "initialize", rb_ICU_Locale_initialize, 1);
304
+ rb_define_attr(cLocale, "locale_id", 1, 0);
305
+ rb_define_alias(cLocale, "to_s", "locale_id");
306
+ rb_define_alias(cLocale, "to_str", "locale_id");
307
+ rb_define_method(cLocale, "parent", rb_ICU_Locale_parent, 0);
308
+ rb_define_method(cLocale, "country", rb_ICU_Locale_country, 0);
309
+ rb_define_method(cLocale, "language", rb_ICU_Locale_language, 0);
310
+
311
+ Init_ICU_Locale_Country();
312
+ Init_ICU_Locale_Language();
313
+ }