2 * xsltlocale.c: locale handling
5 * RFC 3066: Tags for the Identification of Languages
6 * http://www.ietf.org/rfc/rfc3066.txt
7 * ISO 639-1, ISO 3166-1
9 * Author: Nick Wellnhofer
10 * winapi port: Roumen Petrov
15 #include "xsltlocale.h"
17 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 2
18 #define newlocale __newlocale
19 #define freelocale __freelocale
20 #define strxfrm_l __strxfrm_l
21 #define LC_COLLATE_MASK (1 << LC_COLLATE)
24 #define TOUPPER(c) (c & ~0x20)
25 #define TOLOWER(c) (c | 0x20)
26 #define ISALPHA(c) ((unsigned)(TOUPPER(c) - 'A') < 26)
28 /*without terminating null character*/
29 #define XSLTMAX_ISO639LANGLEN 8
30 #define XSLTMAX_ISO3166CNTRYLEN 8
32 #define XSLTMAX_LANGTAGLEN (XSLTMAX_ISO639LANGLEN+1+XSLTMAX_ISO3166CNTRYLEN)
34 static const xmlChar
* xsltDefaultRegion(const xmlChar
*localeName
);
36 #ifdef XSLT_LOCALE_WINAPI
37 xmlRMutexPtr xsltLocaleMutex
= NULL
;
39 struct xsltRFC1766Info_s
{
40 /*note typedef unsigned char xmlChar !*/
41 xmlChar tag
[XSLTMAX_LANGTAGLEN
+1];
42 /*note typedef LCID xsltLocale !*/
45 typedef struct xsltRFC1766Info_s xsltRFC1766Info
;
47 static int xsltLocaleListSize
= 0;
48 static xsltRFC1766Info
*xsltLocaleList
= NULL
;
52 xslt_locale_WINAPI(const xmlChar
*languageTag
) {
54 xsltRFC1766Info
*p
= xsltLocaleList
;
56 for (k
=0; k
<xsltLocaleListSize
; k
++, p
++)
57 if (xmlStrcmp(p
->tag
, languageTag
) == 0) return p
->lcid
;
58 return((xsltLocale
)0);
61 static void xsltEnumSupportedLocales(void);
67 * Cleanup function for the locale support on shutdown
70 xsltFreeLocales(void) {
71 #ifdef XSLT_LOCALE_WINAPI
72 xmlRMutexLock(xsltLocaleMutex
);
73 xmlFree(xsltLocaleList
);
74 xsltLocaleList
= NULL
;
75 xmlRMutexUnlock(xsltLocaleMutex
);
81 * @languageTag: RFC 3066 language tag
83 * Creates a new locale of an opaque system dependent type based on the
86 * Returns the locale or NULL on error or if no matching locale was found
89 xsltNewLocale(const xmlChar
*languageTag
) {
90 #ifdef XSLT_LOCALE_XLOCALE
92 char localeName
[XSLTMAX_LANGTAGLEN
+6]; /* 6 chars for ".utf8\0" */
93 const xmlChar
*p
= languageTag
;
94 const char *region
= NULL
;
98 /* Convert something like "pt-br" to "pt_BR.utf8" */
100 if (languageTag
== NULL
)
103 for (i
=0; i
<XSLTMAX_ISO639LANGLEN
&& ISALPHA(*p
); ++i
)
104 *q
++ = TOLOWER(*p
++);
116 for (i
=0; i
<XSLTMAX_ISO3166CNTRYLEN
&& ISALPHA(*p
); ++i
)
117 *q
++ = TOUPPER(*p
++);
122 memcpy(q
, ".utf8", 6);
123 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
127 /* Continue without using country code */
129 q
= localeName
+ llen
;
132 /* Try locale without territory, e.g. for Esperanto (eo) */
134 memcpy(q
, ".utf8", 6);
135 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
139 /* Try to find most common country for language */
144 region
= (char *)xsltDefaultRegion((xmlChar
*)localeName
);
148 q
= localeName
+ llen
;
152 memcpy(q
, ".utf8", 6);
153 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
158 #ifdef XSLT_LOCALE_WINAPI
160 xsltLocale locale
= (xsltLocale
)0;
161 xmlChar localeName
[XSLTMAX_LANGTAGLEN
+1];
162 xmlChar
*q
= localeName
;
163 const xmlChar
*p
= languageTag
;
165 const xmlChar
*region
= NULL
;
167 if (languageTag
== NULL
) goto end
;
169 xsltEnumSupportedLocales();
171 for (i
=0; i
<XSLTMAX_ISO639LANGLEN
&& ISALPHA(*p
); ++i
)
172 *q
++ = TOLOWER(*p
++);
173 if (i
== 0) goto end
;
177 if (*p
) { /*if country tag is given*/
178 if (*p
++ != '-') goto end
;
180 for (i
=0; i
<XSLTMAX_ISO3166CNTRYLEN
&& ISALPHA(*p
); ++i
)
181 *q
++ = TOUPPER(*p
++);
182 if (i
== 0 || *p
) goto end
;
185 locale
= xslt_locale_WINAPI(localeName
);
186 if (locale
!= (xsltLocale
)0) goto end
;
188 /* Try to find most common country for language */
189 region
= xsltDefaultRegion(localeName
);
190 if (region
== NULL
) goto end
;
192 strcpy(localeName
+ llen
+ 1, region
);
193 locale
= xslt_locale_WINAPI(localeName
);
199 #ifdef XSLT_LOCALE_NONE
204 static const xmlChar
*
205 xsltDefaultRegion(const xmlChar
*localeName
) {
207 /* region should be xmlChar, but gcc warns on all string assignments */
208 const char *region
= NULL
;
211 /* This is based on the locales from glibc 2.3.3 */
213 switch (localeName
[0]) {
215 if (c
== 'a' || c
== 'm') region
= "ET";
216 else if (c
== 'f') region
= "ZA";
217 else if (c
== 'n') region
= "ES";
218 else if (c
== 'r') region
= "AE";
219 else if (c
== 'z') region
= "AZ";
222 if (c
== 'e') region
= "BY";
223 else if (c
== 'g') region
= "BG";
224 else if (c
== 'n') region
= "BD";
225 else if (c
== 'r') region
= "FR";
226 else if (c
== 's') region
= "BA";
229 if (c
== 'a') region
= "ES";
230 else if (c
== 's') region
= "CZ";
231 else if (c
== 'y') region
= "GB";
234 if (c
== 'a') region
= "DK";
235 else if (c
== 'e') region
= "DE";
238 if (c
== 'l') region
= "GR";
239 else if (c
== 'n' || c
== 'o') region
= "US";
240 else if (c
== 's' || c
== 'u') region
= "ES";
241 else if (c
== 't') region
= "EE";
244 if (c
== 'a') region
= "IR";
245 else if (c
== 'i') region
= "FI";
246 else if (c
== 'o') region
= "FO";
247 else if (c
== 'r') region
= "FR";
250 if (c
== 'a') region
= "IE";
251 else if (c
== 'l') region
= "ES";
252 else if (c
== 'v') region
= "GB";
255 if (c
== 'e') region
= "IL";
256 else if (c
== 'i') region
= "IN";
257 else if (c
== 'r') region
= "HT";
258 else if (c
== 'u') region
= "HU";
261 if (c
== 'd') region
= "ID";
262 else if (c
== 's') region
= "IS";
263 else if (c
== 't') region
= "IT";
264 else if (c
== 'w') region
= "IL";
267 if (c
== 'a') region
= "JP";
270 if (c
== 'l') region
= "GL";
271 else if (c
== 'o') region
= "KR";
272 else if (c
== 'w') region
= "GB";
275 if (c
== 't') region
= "LT";
276 else if (c
== 'v') region
= "LV";
279 if (c
== 'k') region
= "MK";
280 else if (c
== 'l' || c
== 'r') region
= "IN";
281 else if (c
== 'n') region
= "MN";
282 else if (c
== 's') region
= "MY";
283 else if (c
== 't') region
= "MT";
286 if (c
== 'b' || c
== 'n' || c
== 'o') region
= "NO";
287 else if (c
== 'e') region
= "NP";
288 else if (c
== 'l') region
= "NL";
291 if (c
== 'm') region
= "ET";
294 if (c
== 'a') region
= "IN";
295 else if (c
== 'l') region
= "PL";
296 else if (c
== 't') region
= "PT";
299 if (c
== 'o') region
= "RO";
300 else if (c
== 'u') region
= "RU";
304 case 'e': region
= "NO"; break;
305 case 'h': region
= "YU"; break;
306 case 'k': region
= "SK"; break;
307 case 'l': region
= "SI"; break;
308 case 'o': region
= "ET"; break;
309 case 'q': region
= "AL"; break;
310 case 't': region
= "ZA"; break;
311 case 'v': region
= "SE"; break;
315 if (c
== 'a' || c
== 'e') region
= "IN";
316 else if (c
== 'h') region
= "TH";
317 else if (c
== 'i') region
= "ER";
318 else if (c
== 'r') region
= "TR";
319 else if (c
== 't') region
= "RU";
322 if (c
== 'k') region
= "UA";
323 else if (c
== 'r') region
= "PK";
326 if (c
== 'i') region
= "VN";
329 if (c
== 'a') region
= "BE";
332 if (c
== 'h') region
= "ZA";
335 if (c
== 'h') region
= "CN";
336 else if (c
== 'u') region
= "ZA";
339 return((xmlChar
*)region
);
344 * @locale: the locale to free
346 * Frees a locale created with xsltNewLocale
349 xsltFreeLocale(xsltLocale locale
) {
350 #ifdef XSLT_LOCALE_XLOCALE
357 * @locale: locale created with xsltNewLocale
358 * @string: UTF-8 string to transform
360 * Transforms a string according to locale. The transformed string must then be
361 * compared with xsltLocaleStrcmp and freed with xmlFree.
363 * Returns the transformed string or NULL on error
366 xsltStrxfrm(xsltLocale locale
, const xmlChar
*string
)
368 #ifdef XSLT_LOCALE_NONE
372 xsltLocaleChar
*xstr
;
374 #ifdef XSLT_LOCALE_XLOCALE
375 xstrlen
= strxfrm_l(NULL
, (const char *)string
, 0, locale
) + 1;
376 xstr
= (xsltLocaleChar
*) xmlMalloc(xstrlen
);
378 xsltTransformError(NULL
, NULL
, NULL
,
379 "xsltStrxfrm : out of memory error\n");
383 r
= strxfrm_l((char *)xstr
, (const char *)string
, xstrlen
, locale
);
386 #ifdef XSLT_LOCALE_WINAPI
387 xstrlen
= MultiByteToWideChar(CP_UTF8
, 0, string
, -1, NULL
, 0);
389 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : MultiByteToWideChar check failed\n");
392 xstr
= (xsltLocaleChar
*) xmlMalloc(xstrlen
* sizeof(xsltLocaleChar
));
394 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : out of memory\n");
397 r
= MultiByteToWideChar(CP_UTF8
, 0, string
, -1, xstr
, xstrlen
);
399 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : MultiByteToWideChar failed\n");
404 #endif /* XSLT_LOCALE_WINAPI */
407 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : strxfrm failed\n");
413 #endif /* XSLT_LOCALE_NONE */
418 * @locale: a locale identifier
419 * @str1: a string transformed with xsltStrxfrm
420 * @str2: a string transformed with xsltStrxfrm
422 * Compares two strings transformed with xsltStrxfrm
424 * Returns a value < 0 if str1 sorts before str2,
425 * a value > 0 if str1 sorts after str2,
426 * 0 if str1 and str2 are equal wrt sorting
429 xsltLocaleStrcmp(xsltLocale locale
, const xsltLocaleChar
*str1
, const xsltLocaleChar
*str2
) {
431 #ifdef XSLT_LOCALE_WINAPI
434 if (str1
== str2
) return(0);
435 if (str1
== NULL
) return(-1);
436 if (str2
== NULL
) return(1);
437 ret
= CompareStringW(locale
, 0, str1
, -1, str2
, -1);
439 xsltTransformError(NULL
, NULL
, NULL
, "xsltLocaleStrcmp : CompareStringW fail\n");
445 return(xmlStrcmp(str1
, str2
));
449 #ifdef XSLT_LOCALE_WINAPI
451 * xsltCountSupportedLocales:
454 * callback used to count locales
459 xsltCountSupportedLocales(LPSTR lcid
) {
461 ++xsltLocaleListSize
;
466 * xsltIterateSupportedLocales:
469 * callback used to track locales
471 * Returns TRUE if not at the end of the array
474 xsltIterateSupportedLocales(LPSTR lcid
) {
475 static int count
= 0;
476 xmlChar iso639lang
[XSLTMAX_ISO639LANGLEN
+1];
477 xmlChar iso3136ctry
[XSLTMAX_ISO3166CNTRYLEN
+1];
479 xsltRFC1766Info
*p
= xsltLocaleList
+ count
;
481 k
= sscanf(lcid
, "%lx", (long*)&p
->lcid
);
483 /*don't count terminating null character*/
484 k
= GetLocaleInfoA(p
->lcid
, LOCALE_SISO639LANGNAME
, iso639lang
, sizeof(iso639lang
));
485 if (--k
< 1) goto end
;
486 l
= GetLocaleInfoA(p
->lcid
, LOCALE_SISO3166CTRYNAME
, iso3136ctry
, sizeof(iso3136ctry
));
487 if (--l
< 1) goto end
;
491 memcpy(q
, iso639lang
, k
);
494 memcpy(q
, iso3136ctry
, l
);
500 return((count
< xsltLocaleListSize
) ? TRUE
: FALSE
);
505 xsltEnumSupportedLocales(void) {
506 xmlRMutexLock(xsltLocaleMutex
);
507 if (xsltLocaleListSize
<= 0) {
510 EnumSystemLocalesA(xsltCountSupportedLocales
, LCID_SUPPORTED
);
512 len
= xsltLocaleListSize
* sizeof(xsltRFC1766Info
);
513 xsltLocaleList
= xmlMalloc(len
);
514 memset(xsltLocaleList
, 0, len
);
515 EnumSystemLocalesA(xsltIterateSupportedLocales
, LCID_SUPPORTED
);
517 xmlRMutexUnlock(xsltLocaleMutex
);
520 #endif /*def XSLT_LOCALE_WINAPI*/