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
17 #include <libxml/xmlmemory.h>
19 #include "xsltlocale.h"
20 #include "xsltutils.h"
24 #define XSLT_LOCALE_POSIX
35 #define XSLT_LOCALE_WINAPI
42 #define XSLT_LOCALE_NONE
46 #define TOUPPER(c) (c & ~0x20)
47 #define TOLOWER(c) (c | 0x20)
48 #define ISALPHA(c) ((unsigned)(TOUPPER(c) - 'A') < 26)
50 /*without terminating null character*/
51 #define XSLTMAX_ISO639LANGLEN 8
52 #define XSLTMAX_ISO3166CNTRYLEN 8
54 #define XSLTMAX_LANGTAGLEN (XSLTMAX_ISO639LANGLEN+1+XSLTMAX_ISO3166CNTRYLEN)
56 static const xmlChar
* xsltDefaultRegion(const xmlChar
*localeName
);
58 #ifdef XSLT_LOCALE_WINAPI
59 xmlRMutexPtr xsltLocaleMutex
= NULL
;
61 struct xsltRFC1766Info_s
{
62 /*note typedef unsigned char xmlChar !*/
63 xmlChar tag
[XSLTMAX_LANGTAGLEN
+1];
66 typedef struct xsltRFC1766Info_s xsltRFC1766Info
;
68 static int xsltLocaleListSize
= 0;
69 static xsltRFC1766Info
*xsltLocaleList
= NULL
;
73 xslt_locale_WINAPI(const xmlChar
*languageTag
) {
75 xsltRFC1766Info
*p
= xsltLocaleList
;
77 for (k
=0; k
<xsltLocaleListSize
; k
++, p
++)
78 if (xmlStrcmp(p
->tag
, languageTag
) == 0)
83 static void xsltEnumSupportedLocales(void);
89 * Cleanup function for the locale support on shutdown
92 xsltFreeLocales(void) {
93 #ifdef XSLT_LOCALE_WINAPI
94 xmlRMutexLock(xsltLocaleMutex
);
95 xmlFree(xsltLocaleList
);
96 xsltLocaleList
= NULL
;
97 xmlRMutexUnlock(xsltLocaleMutex
);
103 * @languageTag: RFC 3066 language tag
105 * Creates a new locale of an opaque system dependent type based on the
108 * Returns the locale or NULL on error or if no matching locale was found
111 xsltNewLocale(const xmlChar
*languageTag
, int lowerFirst ATTRIBUTE_UNUSED
) {
112 #ifdef XSLT_LOCALE_POSIX
114 char localeName
[XSLTMAX_LANGTAGLEN
+7]; /* 7 chars for ".UTF-8\0" */
115 const xmlChar
*p
= languageTag
;
116 const char *region
= NULL
;
117 char *q
= localeName
;
120 /* Convert something like "pt-br" to "pt_BR.UTF-8" */
122 if (languageTag
== NULL
)
125 for (i
=0; i
<XSLTMAX_ISO639LANGLEN
&& ISALPHA(*p
); ++i
)
126 *q
++ = TOLOWER(*p
++);
138 for (i
=0; i
<XSLTMAX_ISO3166CNTRYLEN
&& ISALPHA(*p
); ++i
)
139 *q
++ = TOUPPER(*p
++);
144 memcpy(q
, ".UTF-8", 7);
145 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
149 /* Continue without using country code */
151 q
= localeName
+ llen
;
154 /* Try locale without territory, e.g. for Esperanto (eo) */
156 memcpy(q
, ".UTF-8", 7);
157 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
161 /* Try to find most common country for language */
166 region
= (char *)xsltDefaultRegion((xmlChar
*)localeName
);
170 q
= localeName
+ llen
;
174 memcpy(q
, ".UTF-8", 7);
175 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
180 #ifdef XSLT_LOCALE_WINAPI
183 xmlChar localeName
[XSLTMAX_LANGTAGLEN
+1];
184 xmlChar
*q
= localeName
;
185 const xmlChar
*p
= languageTag
;
187 const xmlChar
*region
= NULL
;
189 if (languageTag
== NULL
) goto end
;
191 xsltEnumSupportedLocales();
193 for (i
=0; i
<XSLTMAX_ISO639LANGLEN
&& ISALPHA(*p
); ++i
)
194 *q
++ = TOLOWER(*p
++);
195 if (i
== 0) goto end
;
199 if (*p
) { /*if country tag is given*/
200 if (*p
++ != '-') goto end
;
202 for (i
=0; i
<XSLTMAX_ISO3166CNTRYLEN
&& ISALPHA(*p
); ++i
)
203 *q
++ = TOUPPER(*p
++);
204 if (i
== 0 || *p
) goto end
;
207 locale
= xslt_locale_WINAPI(localeName
);
208 if (locale
!= (xsltLocale
)0) goto end
;
210 /* Try to find most common country for language */
211 region
= xsltDefaultRegion(localeName
);
212 if (region
== NULL
) goto end
;
214 strcpy((char *) localeName
+ llen
+ 1, (char *) region
);
215 locale
= xslt_locale_WINAPI(localeName
);
221 #ifdef XSLT_LOCALE_NONE
226 static const xmlChar
*
227 xsltDefaultRegion(const xmlChar
*localeName
) {
229 /* region should be xmlChar, but gcc warns on all string assignments */
230 const char *region
= NULL
;
233 /* This is based on the locales from glibc 2.3.3 */
235 switch (localeName
[0]) {
237 if (c
== 'a' || c
== 'm') region
= "ET";
238 else if (c
== 'f') region
= "ZA";
239 else if (c
== 'n') region
= "ES";
240 else if (c
== 'r') region
= "AE";
241 else if (c
== 'z') region
= "AZ";
244 if (c
== 'e') region
= "BY";
245 else if (c
== 'g') region
= "BG";
246 else if (c
== 'n') region
= "BD";
247 else if (c
== 'r') region
= "FR";
248 else if (c
== 's') region
= "BA";
251 if (c
== 'a') region
= "ES";
252 else if (c
== 's') region
= "CZ";
253 else if (c
== 'y') region
= "GB";
256 if (c
== 'a') region
= "DK";
257 else if (c
== 'e') region
= "DE";
260 if (c
== 'l') region
= "GR";
261 else if (c
== 'n' || c
== 'o') region
= "US";
262 else if (c
== 's' || c
== 'u') region
= "ES";
263 else if (c
== 't') region
= "EE";
266 if (c
== 'a') region
= "IR";
267 else if (c
== 'i') region
= "FI";
268 else if (c
== 'o') region
= "FO";
269 else if (c
== 'r') region
= "FR";
272 if (c
== 'a') region
= "IE";
273 else if (c
== 'l') region
= "ES";
274 else if (c
== 'v') region
= "GB";
277 if (c
== 'e') region
= "IL";
278 else if (c
== 'i') region
= "IN";
279 else if (c
== 'r') region
= "HT";
280 else if (c
== 'u') region
= "HU";
283 if (c
== 'd') region
= "ID";
284 else if (c
== 's') region
= "IS";
285 else if (c
== 't') region
= "IT";
286 else if (c
== 'w') region
= "IL";
289 if (c
== 'a') region
= "JP";
292 if (c
== 'l') region
= "GL";
293 else if (c
== 'o') region
= "KR";
294 else if (c
== 'w') region
= "GB";
297 if (c
== 't') region
= "LT";
298 else if (c
== 'v') region
= "LV";
301 if (c
== 'k') region
= "MK";
302 else if (c
== 'l' || c
== 'r') region
= "IN";
303 else if (c
== 'n') region
= "MN";
304 else if (c
== 's') region
= "MY";
305 else if (c
== 't') region
= "MT";
308 if (c
== 'b' || c
== 'n' || c
== 'o') region
= "NO";
309 else if (c
== 'e') region
= "NP";
310 else if (c
== 'l') region
= "NL";
313 if (c
== 'm') region
= "ET";
316 if (c
== 'a') region
= "IN";
317 else if (c
== 'l') region
= "PL";
318 else if (c
== 't') region
= "PT";
321 if (c
== 'o') region
= "RO";
322 else if (c
== 'u') region
= "RU";
326 case 'e': region
= "NO"; break;
327 case 'h': region
= "YU"; break;
328 case 'k': region
= "SK"; break;
329 case 'l': region
= "SI"; break;
330 case 'o': region
= "ET"; break;
331 case 'q': region
= "AL"; break;
332 case 't': region
= "ZA"; break;
333 case 'v': region
= "SE"; break;
337 if (c
== 'a' || c
== 'e') region
= "IN";
338 else if (c
== 'h') region
= "TH";
339 else if (c
== 'i') region
= "ER";
340 else if (c
== 'r') region
= "TR";
341 else if (c
== 't') region
= "RU";
344 if (c
== 'k') region
= "UA";
345 else if (c
== 'r') region
= "PK";
348 if (c
== 'i') region
= "VN";
351 if (c
== 'a') region
= "BE";
354 if (c
== 'h') region
= "ZA";
357 if (c
== 'h') region
= "CN";
358 else if (c
== 'u') region
= "ZA";
361 return((xmlChar
*)region
);
366 * @locale: the locale to free
368 * Frees a locale created with xsltNewLocale
371 xsltFreeLocale(void *locale
) {
372 #ifdef XSLT_LOCALE_POSIX
382 * @locale: locale created with xsltNewLocale
383 * @string: UTF-8 string to transform
385 * Transforms a string according to locale. The transformed string must be
386 * freed with xmlFree.
388 * Returns the transformed string or NULL on error
391 xsltStrxfrm(void *vlocale
, const xmlChar
*string
)
393 #ifdef XSLT_LOCALE_NONE
398 #ifdef XSLT_LOCALE_POSIX
401 xstrlen
= strxfrm_l(NULL
, (const char *)string
, 0, vlocale
) + 1;
402 xstr
= (xmlChar
*) xmlMalloc(xstrlen
);
404 xsltTransformError(NULL
, NULL
, NULL
,
405 "xsltStrxfrm : out of memory error\n");
409 r
= strxfrm_l((char *)xstr
, (const char *)string
, xstrlen
, vlocale
);
412 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : strxfrm failed\n");
418 #ifdef XSLT_LOCALE_WINAPI
419 int wstrlen
, xstrlen
, r
;
421 LCID
*lcid
= vlocale
;
423 wstrlen
= MultiByteToWideChar(CP_UTF8
, 0, (char *) string
, -1, NULL
, 0);
425 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : MultiByteToWideChar check failed\n");
428 wstr
= (wchar_t *) xmlMalloc(wstrlen
* sizeof(wchar_t));
430 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : out of memory\n");
433 r
= MultiByteToWideChar(CP_UTF8
, 0, (char *) string
, -1, wstr
, wstrlen
);
435 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : MultiByteToWideChar failed\n");
439 /* This returns the size in bytes. */
440 xstrlen
= LCMapStringW(*lcid
, LCMAP_SORTKEY
, wstr
, wstrlen
, NULL
, 0);
442 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : LCMapStringW failed\n");
446 xstr
= (xmlChar
*) xmlMalloc(xstrlen
);
448 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : out of memory\n");
452 r
= LCMapStringW(*lcid
, LCMAP_SORTKEY
, wstr
, wstrlen
, (wchar_t *) xstr
,
456 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : LCMapStringW failed\n");
460 #endif /* XSLT_LOCALE_WINAPI */
463 #endif /* XSLT_LOCALE_NONE */
469 * @str1: a string transformed with xsltStrxfrm
470 * @str2: a string transformed with xsltStrxfrm
472 * DEPRECATED: Same as xmlStrcmp.
474 * Compares two strings transformed with xsltStrxfrm.
476 * Returns a value < 0 if str1 sorts before str2,
477 * a value > 0 if str1 sorts after str2,
478 * 0 if str1 and str2 are equal wrt sorting
481 xsltLocaleStrcmp(void *locale
, const xmlChar
*str1
, const xmlChar
*str2
) {
483 return(xmlStrcmp(str1
, str2
));
486 #ifdef XSLT_LOCALE_WINAPI
488 * xsltCountSupportedLocales:
491 * callback used to count locales
496 xsltCountSupportedLocales(LPSTR lcid
) {
498 ++xsltLocaleListSize
;
503 * xsltIterateSupportedLocales:
506 * callback used to track locales
508 * Returns TRUE if not at the end of the array
511 xsltIterateSupportedLocales(LPSTR lcid
) {
512 static int count
= 0;
513 xmlChar iso639lang
[XSLTMAX_ISO639LANGLEN
+1];
514 xmlChar iso3136ctry
[XSLTMAX_ISO3166CNTRYLEN
+1];
516 xsltRFC1766Info
*p
= xsltLocaleList
+ count
;
518 k
= sscanf(lcid
, "%lx", (unsigned long*)&p
->lcid
);
520 /*don't count terminating null character*/
521 k
= GetLocaleInfoA(p
->lcid
, LOCALE_SISO639LANGNAME
,
522 (char *) iso639lang
, sizeof(iso639lang
));
523 if (--k
< 1) goto end
;
524 l
= GetLocaleInfoA(p
->lcid
, LOCALE_SISO3166CTRYNAME
,
525 (char *) iso3136ctry
, sizeof(iso3136ctry
));
526 if (--l
< 1) goto end
;
530 memcpy(q
, iso639lang
, k
);
533 memcpy(q
, iso3136ctry
, l
);
539 return((count
< xsltLocaleListSize
) ? TRUE
: FALSE
);
544 xsltEnumSupportedLocales(void) {
545 xmlRMutexLock(xsltLocaleMutex
);
546 if (xsltLocaleListSize
<= 0) {
549 EnumSystemLocalesA(xsltCountSupportedLocales
, LCID_SUPPORTED
);
551 len
= xsltLocaleListSize
* sizeof(xsltRFC1766Info
);
552 xsltLocaleList
= xmlMalloc(len
);
553 memset(xsltLocaleList
, 0, len
);
554 EnumSystemLocalesA(xsltIterateSupportedLocales
, LCID_SUPPORTED
);
556 xmlRMutexUnlock(xsltLocaleMutex
);
559 #endif /*def XSLT_LOCALE_WINAPI*/