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 #define TOUPPER(c) (c & ~0x20)
18 #define TOLOWER(c) (c | 0x20)
19 #define ISALPHA(c) ((unsigned)(TOUPPER(c) - 'A') < 26)
21 /*without terminating null character*/
22 #define XSLTMAX_ISO639LANGLEN 8
23 #define XSLTMAX_ISO3166CNTRYLEN 8
25 #define XSLTMAX_LANGTAGLEN (XSLTMAX_ISO639LANGLEN+1+XSLTMAX_ISO3166CNTRYLEN)
27 static const xmlChar
* xsltDefaultRegion(const xmlChar
*localeName
);
29 #ifdef XSLT_LOCALE_WINAPI
30 xmlRMutexPtr xsltLocaleMutex
= NULL
;
32 struct xsltRFC1766Info_s
{
33 /*note typedef unsigned char xmlChar !*/
34 xmlChar tag
[XSLTMAX_LANGTAGLEN
+1];
35 /*note typedef LCID xsltLocale !*/
38 typedef struct xsltRFC1766Info_s xsltRFC1766Info
;
40 static int xsltLocaleListSize
= 0;
41 static xsltRFC1766Info
*xsltLocaleList
= NULL
;
45 xslt_locale_WINAPI(const xmlChar
*languageTag
) {
47 xsltRFC1766Info
*p
= xsltLocaleList
;
49 for (k
=0; k
<xsltLocaleListSize
; k
++, p
++)
50 if (xmlStrcmp(p
->tag
, languageTag
) == 0) return p
->lcid
;
51 return((xsltLocale
)0);
54 static void xsltEnumSupportedLocales(void);
60 * Cleanup function for the locale support on shutdown
63 xsltFreeLocales(void) {
64 #ifdef XSLT_LOCALE_WINAPI
65 xmlRMutexLock(xsltLocaleMutex
);
66 xmlFree(xsltLocaleList
);
67 xsltLocaleList
= NULL
;
68 xmlRMutexUnlock(xsltLocaleMutex
);
74 * @languageTag: RFC 3066 language tag
76 * Creates a new locale of an opaque system dependent type based on the
79 * Returns the locale or NULL on error or if no matching locale was found
82 xsltNewLocale(const xmlChar
*languageTag
) {
83 #ifdef XSLT_LOCALE_POSIX
85 char localeName
[XSLTMAX_LANGTAGLEN
+6]; /* 6 chars for ".utf8\0" */
86 const xmlChar
*p
= languageTag
;
87 const char *region
= NULL
;
91 /* Convert something like "pt-br" to "pt_BR.utf8" */
93 if (languageTag
== NULL
)
96 for (i
=0; i
<XSLTMAX_ISO639LANGLEN
&& ISALPHA(*p
); ++i
)
109 for (i
=0; i
<XSLTMAX_ISO3166CNTRYLEN
&& ISALPHA(*p
); ++i
)
110 *q
++ = TOUPPER(*p
++);
115 memcpy(q
, ".utf8", 6);
116 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
120 /* Continue without using country code */
122 q
= localeName
+ llen
;
125 /* Try locale without territory, e.g. for Esperanto (eo) */
127 memcpy(q
, ".utf8", 6);
128 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
132 /* Try to find most common country for language */
137 region
= (char *)xsltDefaultRegion((xmlChar
*)localeName
);
141 q
= localeName
+ llen
;
145 memcpy(q
, ".utf8", 6);
146 locale
= newlocale(LC_COLLATE_MASK
, localeName
, NULL
);
151 #ifdef XSLT_LOCALE_WINAPI
153 xsltLocale locale
= (xsltLocale
)0;
154 xmlChar localeName
[XSLTMAX_LANGTAGLEN
+1];
155 xmlChar
*q
= localeName
;
156 const xmlChar
*p
= languageTag
;
158 const xmlChar
*region
= NULL
;
160 if (languageTag
== NULL
) goto end
;
162 xsltEnumSupportedLocales();
164 for (i
=0; i
<XSLTMAX_ISO639LANGLEN
&& ISALPHA(*p
); ++i
)
165 *q
++ = TOLOWER(*p
++);
166 if (i
== 0) goto end
;
170 if (*p
) { /*if country tag is given*/
171 if (*p
++ != '-') goto end
;
173 for (i
=0; i
<XSLTMAX_ISO3166CNTRYLEN
&& ISALPHA(*p
); ++i
)
174 *q
++ = TOUPPER(*p
++);
175 if (i
== 0 || *p
) goto end
;
178 locale
= xslt_locale_WINAPI(localeName
);
179 if (locale
!= (xsltLocale
)0) goto end
;
181 /* Try to find most common country for language */
182 region
= xsltDefaultRegion(localeName
);
183 if (region
== NULL
) goto end
;
185 strcpy((char *) localeName
+ llen
+ 1, (char *) region
);
186 locale
= xslt_locale_WINAPI(localeName
);
192 #ifdef XSLT_LOCALE_NONE
197 static const xmlChar
*
198 xsltDefaultRegion(const xmlChar
*localeName
) {
200 /* region should be xmlChar, but gcc warns on all string assignments */
201 const char *region
= NULL
;
204 /* This is based on the locales from glibc 2.3.3 */
206 switch (localeName
[0]) {
208 if (c
== 'a' || c
== 'm') region
= "ET";
209 else if (c
== 'f') region
= "ZA";
210 else if (c
== 'n') region
= "ES";
211 else if (c
== 'r') region
= "AE";
212 else if (c
== 'z') region
= "AZ";
215 if (c
== 'e') region
= "BY";
216 else if (c
== 'g') region
= "BG";
217 else if (c
== 'n') region
= "BD";
218 else if (c
== 'r') region
= "FR";
219 else if (c
== 's') region
= "BA";
222 if (c
== 'a') region
= "ES";
223 else if (c
== 's') region
= "CZ";
224 else if (c
== 'y') region
= "GB";
227 if (c
== 'a') region
= "DK";
228 else if (c
== 'e') region
= "DE";
231 if (c
== 'l') region
= "GR";
232 else if (c
== 'n' || c
== 'o') region
= "US";
233 else if (c
== 's' || c
== 'u') region
= "ES";
234 else if (c
== 't') region
= "EE";
237 if (c
== 'a') region
= "IR";
238 else if (c
== 'i') region
= "FI";
239 else if (c
== 'o') region
= "FO";
240 else if (c
== 'r') region
= "FR";
243 if (c
== 'a') region
= "IE";
244 else if (c
== 'l') region
= "ES";
245 else if (c
== 'v') region
= "GB";
248 if (c
== 'e') region
= "IL";
249 else if (c
== 'i') region
= "IN";
250 else if (c
== 'r') region
= "HT";
251 else if (c
== 'u') region
= "HU";
254 if (c
== 'd') region
= "ID";
255 else if (c
== 's') region
= "IS";
256 else if (c
== 't') region
= "IT";
257 else if (c
== 'w') region
= "IL";
260 if (c
== 'a') region
= "JP";
263 if (c
== 'l') region
= "GL";
264 else if (c
== 'o') region
= "KR";
265 else if (c
== 'w') region
= "GB";
268 if (c
== 't') region
= "LT";
269 else if (c
== 'v') region
= "LV";
272 if (c
== 'k') region
= "MK";
273 else if (c
== 'l' || c
== 'r') region
= "IN";
274 else if (c
== 'n') region
= "MN";
275 else if (c
== 's') region
= "MY";
276 else if (c
== 't') region
= "MT";
279 if (c
== 'b' || c
== 'n' || c
== 'o') region
= "NO";
280 else if (c
== 'e') region
= "NP";
281 else if (c
== 'l') region
= "NL";
284 if (c
== 'm') region
= "ET";
287 if (c
== 'a') region
= "IN";
288 else if (c
== 'l') region
= "PL";
289 else if (c
== 't') region
= "PT";
292 if (c
== 'o') region
= "RO";
293 else if (c
== 'u') region
= "RU";
297 case 'e': region
= "NO"; break;
298 case 'h': region
= "YU"; break;
299 case 'k': region
= "SK"; break;
300 case 'l': region
= "SI"; break;
301 case 'o': region
= "ET"; break;
302 case 'q': region
= "AL"; break;
303 case 't': region
= "ZA"; break;
304 case 'v': region
= "SE"; break;
308 if (c
== 'a' || c
== 'e') region
= "IN";
309 else if (c
== 'h') region
= "TH";
310 else if (c
== 'i') region
= "ER";
311 else if (c
== 'r') region
= "TR";
312 else if (c
== 't') region
= "RU";
315 if (c
== 'k') region
= "UA";
316 else if (c
== 'r') region
= "PK";
319 if (c
== 'i') region
= "VN";
322 if (c
== 'a') region
= "BE";
325 if (c
== 'h') region
= "ZA";
328 if (c
== 'h') region
= "CN";
329 else if (c
== 'u') region
= "ZA";
332 return((xmlChar
*)region
);
337 * @locale: the locale to free
339 * Frees a locale created with xsltNewLocale
342 xsltFreeLocale(xsltLocale locale
) {
343 #ifdef XSLT_LOCALE_POSIX
350 * @locale: locale created with xsltNewLocale
351 * @string: UTF-8 string to transform
353 * Transforms a string according to locale. The transformed string must then be
354 * compared with xsltLocaleStrcmp and freed with xmlFree.
356 * Returns the transformed string or NULL on error
359 xsltStrxfrm(xsltLocale locale
, const xmlChar
*string
)
361 #ifdef XSLT_LOCALE_NONE
365 xsltLocaleChar
*xstr
;
367 #ifdef XSLT_LOCALE_POSIX
368 xstrlen
= strxfrm_l(NULL
, (const char *)string
, 0, locale
) + 1;
369 xstr
= (xsltLocaleChar
*) xmlMalloc(xstrlen
);
371 xsltTransformError(NULL
, NULL
, NULL
,
372 "xsltStrxfrm : out of memory error\n");
376 r
= strxfrm_l((char *)xstr
, (const char *)string
, xstrlen
, locale
);
379 #ifdef XSLT_LOCALE_WINAPI
380 xstrlen
= MultiByteToWideChar(CP_UTF8
, 0, (char *) string
, -1, NULL
, 0);
382 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : MultiByteToWideChar check failed\n");
385 xstr
= (xsltLocaleChar
*) xmlMalloc(xstrlen
* sizeof(xsltLocaleChar
));
387 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : out of memory\n");
390 r
= MultiByteToWideChar(CP_UTF8
, 0, (char *) string
, -1, xstr
, xstrlen
);
392 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : MultiByteToWideChar failed\n");
397 #endif /* XSLT_LOCALE_WINAPI */
400 xsltTransformError(NULL
, NULL
, NULL
, "xsltStrxfrm : strxfrm failed\n");
406 #endif /* XSLT_LOCALE_NONE */
411 * @locale: a locale identifier
412 * @str1: a string transformed with xsltStrxfrm
413 * @str2: a string transformed with xsltStrxfrm
415 * Compares two strings transformed with xsltStrxfrm
417 * Returns a value < 0 if str1 sorts before str2,
418 * a value > 0 if str1 sorts after str2,
419 * 0 if str1 and str2 are equal wrt sorting
422 xsltLocaleStrcmp(xsltLocale locale
, const xsltLocaleChar
*str1
, const xsltLocaleChar
*str2
) {
424 #ifdef XSLT_LOCALE_WINAPI
427 if (str1
== str2
) return(0);
428 if (str1
== NULL
) return(-1);
429 if (str2
== NULL
) return(1);
430 ret
= CompareStringW(locale
, 0, str1
, -1, str2
, -1);
432 xsltTransformError(NULL
, NULL
, NULL
, "xsltLocaleStrcmp : CompareStringW fail\n");
438 return(xmlStrcmp(str1
, str2
));
442 #ifdef XSLT_LOCALE_WINAPI
444 * xsltCountSupportedLocales:
447 * callback used to count locales
452 xsltCountSupportedLocales(LPSTR lcid
) {
454 ++xsltLocaleListSize
;
459 * xsltIterateSupportedLocales:
462 * callback used to track locales
464 * Returns TRUE if not at the end of the array
467 xsltIterateSupportedLocales(LPSTR lcid
) {
468 static int count
= 0;
469 xmlChar iso639lang
[XSLTMAX_ISO639LANGLEN
+1];
470 xmlChar iso3136ctry
[XSLTMAX_ISO3166CNTRYLEN
+1];
472 xsltRFC1766Info
*p
= xsltLocaleList
+ count
;
474 k
= sscanf(lcid
, "%lx", (long*)&p
->lcid
);
476 /*don't count terminating null character*/
477 k
= GetLocaleInfoA(p
->lcid
, LOCALE_SISO639LANGNAME
,
478 (char *) iso639lang
, sizeof(iso639lang
));
479 if (--k
< 1) goto end
;
480 l
= GetLocaleInfoA(p
->lcid
, LOCALE_SISO3166CTRYNAME
,
481 (char *) iso3136ctry
, sizeof(iso3136ctry
));
482 if (--l
< 1) goto end
;
486 memcpy(q
, iso639lang
, k
);
489 memcpy(q
, iso3136ctry
, l
);
495 return((count
< xsltLocaleListSize
) ? TRUE
: FALSE
);
500 xsltEnumSupportedLocales(void) {
501 xmlRMutexLock(xsltLocaleMutex
);
502 if (xsltLocaleListSize
<= 0) {
505 EnumSystemLocalesA(xsltCountSupportedLocales
, LCID_SUPPORTED
);
507 len
= xsltLocaleListSize
* sizeof(xsltRFC1766Info
);
508 xsltLocaleList
= xmlMalloc(len
);
509 memset(xsltLocaleList
, 0, len
);
510 EnumSystemLocalesA(xsltIterateSupportedLocales
, LCID_SUPPORTED
);
512 xmlRMutexUnlock(xsltLocaleMutex
);
515 #endif /*def XSLT_LOCALE_WINAPI*/