Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 1 | """ Locale support. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 2 | |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 3 | The module provides low-level access to the C lib's locale APIs |
| 4 | and adds high level number formatting APIs as well as a locale |
| 5 | aliasing engine to complement these. |
| 6 | |
| 7 | The aliasing engine includes support for many commonly used locale |
| 8 | names and maps them to values suitable for passing to the C lib's |
| 9 | setlocale() function. It also includes default encodings for all |
| 10 | supported locale names. |
| 11 | |
| 12 | """ |
| 13 | |
| 14 | import string |
| 15 | |
Marc-André Lemburg | 2348114 | 2000-06-08 17:49:41 +0000 | [diff] [blame] | 16 | ### Load C lib locale APIs or use an emulation |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 17 | |
Marc-André Lemburg | 2348114 | 2000-06-08 17:49:41 +0000 | [diff] [blame] | 18 | try: |
| 19 | from _locale import * |
| 20 | |
| 21 | except ImportError: |
| 22 | |
| 23 | CHAR_MAX = 127 |
| 24 | LC_ALL = 6 |
| 25 | LC_COLLATE = 3 |
| 26 | LC_CTYPE = 0 |
| 27 | LC_MESSAGES = 5 |
| 28 | LC_MONETARY = 4 |
| 29 | LC_NUMERIC = 1 |
| 30 | LC_TIME = 2 |
| 31 | Error = ValueError |
| 32 | |
| 33 | def localeconv(): |
| 34 | """ localeconv() -> dict. |
| 35 | Returns numeric and monetary locale-specific parameters. |
| 36 | """ |
| 37 | # 'C' locale default values |
| 38 | return {'grouping': [127], |
| 39 | 'currency_symbol': '', |
| 40 | 'n_sign_posn': 127, |
| 41 | 'p_cs_precedes': 127, |
| 42 | 'n_cs_precedes': 127, |
| 43 | 'mon_grouping': [], |
| 44 | 'n_sep_by_space': 127, |
| 45 | 'decimal_point': '.', |
| 46 | 'negative_sign': '', |
| 47 | 'positive_sign': '', |
| 48 | 'p_sep_by_space': 127, |
| 49 | 'int_curr_symbol': '', |
| 50 | 'p_sign_posn': 127, |
| 51 | 'thousands_sep': '', |
| 52 | 'mon_thousands_sep': '', |
| 53 | 'frac_digits': 127, |
| 54 | 'mon_decimal_point': '', |
| 55 | 'int_frac_digits': 127} |
| 56 | |
| 57 | def setlocale(category, value=None): |
| 58 | """ setlocale(integer,string=None) -> string. |
| 59 | Activates/queries locale processing. |
| 60 | """ |
| 61 | if value is not None and \ |
| 62 | value is not 'C': |
| 63 | raise Error,'_locale emulation only supports "C" locale' |
| 64 | return 'C' |
| 65 | |
| 66 | def strcoll(a,b): |
| 67 | """ strcoll(string,string) -> int. |
| 68 | Compares two strings according to the locale. |
| 69 | """ |
| 70 | return cmp(a,b) |
| 71 | |
| 72 | def strxfrm(s): |
| 73 | """ strxfrm(string) -> string. |
| 74 | Returns a string that behaves for cmp locale-aware. |
| 75 | """ |
| 76 | return s |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 77 | |
| 78 | ### Number formatting APIs |
| 79 | |
| 80 | # Author: Martin von Loewis |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 81 | |
| 82 | #perform the grouping from right to left |
| 83 | def _group(s): |
| 84 | conv=localeconv() |
| 85 | grouping=conv['grouping'] |
| 86 | if not grouping:return s |
| 87 | result="" |
| 88 | while s and grouping: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 89 | # if grouping is -1, we are done |
| 90 | if grouping[0]==CHAR_MAX: |
| 91 | break |
| 92 | # 0: re-use last group ad infinitum |
| 93 | elif grouping[0]!=0: |
| 94 | #process last group |
| 95 | group=grouping[0] |
| 96 | grouping=grouping[1:] |
| 97 | if result: |
| 98 | result=s[-group:]+conv['thousands_sep']+result |
| 99 | else: |
| 100 | result=s[-group:] |
| 101 | s=s[:-group] |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 102 | if not result: |
| 103 | return s |
| 104 | if s: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | result=s+conv['thousands_sep']+result |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 106 | return result |
| 107 | |
| 108 | def format(f,val,grouping=0): |
| 109 | """Formats a value in the same way that the % formatting would use, |
| 110 | but takes the current locale into account. |
| 111 | Grouping is applied if the third parameter is true.""" |
| 112 | result = f % val |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 113 | fields = string.split(result, ".") |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 114 | if grouping: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 115 | fields[0]=_group(fields[0]) |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 116 | if len(fields)==2: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | return fields[0]+localeconv()['decimal_point']+fields[1] |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 118 | elif len(fields)==1: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 119 | return fields[0] |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 120 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 121 | raise Error,"Too many decimal points in result string" |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 122 | |
| 123 | def str(val): |
| 124 | """Convert float to integer, taking the locale into account.""" |
| 125 | return format("%.12g",val) |
| 126 | |
| 127 | def atof(str,func=string.atof): |
| 128 | "Parses a string as a float according to the locale settings." |
| 129 | #First, get rid of the grouping |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 130 | ts = localeconv()['thousands_sep'] |
| 131 | if ts: |
| 132 | s=string.split(str,ts) |
| 133 | str=string.join(s, "") |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 134 | #next, replace the decimal point with a dot |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 135 | dd = localeconv()['decimal_point'] |
| 136 | if dd: |
| 137 | s=string.split(str,dd) |
| 138 | str=string.join(s,'.') |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 139 | #finally, parse the string |
| 140 | return func(str) |
| 141 | |
| 142 | def atoi(str): |
| 143 | "Converts a string to an integer according to the locale settings." |
| 144 | return atof(str,string.atoi) |
| 145 | |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 146 | def _test(): |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 147 | setlocale(LC_ALL,"") |
| 148 | #do grouping |
| 149 | s1=format("%d",123456789,1) |
| 150 | print s1,"is",atoi(s1) |
| 151 | #standard formatting |
| 152 | s1=str(3.14) |
| 153 | print s1,"is",atof(s1) |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 154 | |
| 155 | ### Locale name aliasing engine |
| 156 | |
| 157 | # Author: Marc-Andre Lemburg, mal@lemburg.com |
| 158 | |
| 159 | def normalize(localename): |
| 160 | |
| 161 | """ Returns a normalized locale code for the given locale |
| 162 | name. |
| 163 | |
| 164 | The returned locale code is formatted for use with |
| 165 | setlocale(). |
| 166 | |
| 167 | If normalization fails, the original name is returned |
| 168 | unchanged. |
| 169 | |
| 170 | If the given encoding is not known, the function defaults to |
| 171 | the default encoding for the locale code just like setlocale() |
| 172 | does. |
| 173 | |
| 174 | """ |
| 175 | # Normalize the locale name and extract the encoding |
| 176 | fullname = string.lower(localename) |
| 177 | if ':' in fullname: |
| 178 | # ':' is sometimes used as encoding delimiter. |
| 179 | fullname = string.replace(fullname, ':', '.') |
| 180 | if '.' in fullname: |
| 181 | langname, encoding = string.split(fullname, '.')[:2] |
| 182 | fullname = langname + '.' + encoding |
| 183 | else: |
| 184 | langname = fullname |
| 185 | encoding = '' |
| 186 | |
| 187 | # First lookup: fullname (possibly with encoding) |
| 188 | code = locale_alias.get(fullname, None) |
| 189 | if code is not None: |
| 190 | return code |
| 191 | |
| 192 | # Second try: langname (without encoding) |
| 193 | code = locale_alias.get(langname, None) |
| 194 | if code is not None: |
| 195 | if '.' in code: |
| 196 | langname, defenc = string.split(code, '.') |
| 197 | else: |
| 198 | langname = code |
| 199 | defenc = '' |
| 200 | if encoding: |
| 201 | encoding = encoding_alias.get(encoding, encoding) |
| 202 | else: |
| 203 | encoding = defenc |
| 204 | if encoding: |
| 205 | return langname + '.' + encoding |
| 206 | else: |
| 207 | return langname |
| 208 | |
| 209 | else: |
| 210 | return localename |
| 211 | |
| 212 | def _parse_localename(localename): |
| 213 | |
| 214 | """ Parses the locale code for localename and returns the |
| 215 | result as tuple (language code, encoding). |
| 216 | |
| 217 | The localename is normalized and passed through the locale |
| 218 | alias engine. A ValueError is raised in case the locale name |
| 219 | cannot be parsed. |
| 220 | |
| 221 | The language code corresponds to RFC 1766. code and encoding |
| 222 | can be None in case the values cannot be determined or are |
| 223 | unkown to this implementation. |
| 224 | |
| 225 | """ |
| 226 | code = normalize(localename) |
| 227 | if '.' in code: |
| 228 | return string.split(code, '.')[:2] |
| 229 | elif code == 'C': |
| 230 | return None, None |
| 231 | else: |
| 232 | raise ValueError,'unkown locale: %s' % localename |
| 233 | return l |
| 234 | |
| 235 | def _build_localename(localetuple): |
| 236 | |
| 237 | """ Builds a locale code from the given tuple (language code, |
| 238 | encoding). |
| 239 | |
| 240 | No aliasing or normalizing takes place. |
| 241 | |
| 242 | """ |
| 243 | language, encoding = localetuple |
| 244 | if language is None: |
| 245 | language = 'C' |
| 246 | if encoding is None: |
| 247 | return language |
| 248 | else: |
| 249 | return language + '.' + encoding |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 250 | |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 251 | def get_default(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')): |
| 252 | |
| 253 | """ Tries to determine the default locale settings and returns |
| 254 | them as tuple (language code, encoding). |
| 255 | |
| 256 | According to POSIX, a program which has not called |
| 257 | setlocale(LC_ALL,"") runs using the portable 'C' locale. |
| 258 | Calling setlocale(LC_ALL,"") lets it use the default locale as |
| 259 | defined by the LANG variable. Since we don't want to interfere |
| 260 | with the current locale setting we thus emulate the behaviour |
| 261 | in the way described above. |
| 262 | |
| 263 | To maintain compatibility with other platforms, not only the |
| 264 | LANG variable is tested, but a list of variables given as |
| 265 | envvars parameter. The first found to be defined will be |
| 266 | used. envvars defaults to the search path used in GNU gettext; |
| 267 | it must always contain the variable name 'LANG'. |
| 268 | |
| 269 | Except for the code 'C', the language code corresponds to RFC |
| 270 | 1766. code and encoding can be None in case the values cannot |
| 271 | be determined. |
| 272 | |
| 273 | """ |
| 274 | import os |
| 275 | lookup = os.environ.get |
| 276 | for variable in envvars: |
| 277 | localename = lookup(variable,None) |
| 278 | if localename is not None: |
| 279 | break |
| 280 | else: |
| 281 | localename = 'C' |
| 282 | return _parse_localename(localename) |
| 283 | |
| 284 | def get_locale(category=LC_CTYPE): |
| 285 | |
| 286 | """ Returns the current setting for the given locale category as |
| 287 | tuple (language code, encoding). |
| 288 | |
| 289 | category may be one of the LC_* value except LC_ALL. It |
| 290 | defaults to LC_CTYPE. |
| 291 | |
| 292 | Except for the code 'C', the language code corresponds to RFC |
| 293 | 1766. code and encoding can be None in case the values cannot |
| 294 | be determined. |
| 295 | |
| 296 | """ |
| 297 | localename = setlocale(category) |
| 298 | if category == LC_ALL and ';' in localename: |
| 299 | raise TypeError,'category LC_ALL is not supported' |
| 300 | return _parse_localename(localename) |
| 301 | |
| 302 | def set_locale(localetuple, category=LC_ALL): |
| 303 | |
| 304 | """ Set the locale according to the localetuple (language code, |
| 305 | encoding) as returned by get_locale() and get_default(). |
| 306 | |
| 307 | The given codes are passed through the locale aliasing engine |
| 308 | before being given to setlocale() for processing. |
| 309 | |
| 310 | category may be given as one of the LC_* values. It defaults |
| 311 | to LC_ALL. |
| 312 | |
| 313 | """ |
| 314 | setlocale(category, normalize(_build_localename(localetuple))) |
| 315 | |
| 316 | def set_to_default(category=LC_ALL): |
| 317 | |
| 318 | """ Sets the locale for category to the default setting. |
| 319 | |
| 320 | The default setting is determined by calling |
| 321 | get_default(). category defaults to LC_ALL. |
| 322 | |
| 323 | """ |
| 324 | setlocale(category, _build_localename(get_default())) |
| 325 | |
| 326 | ### Database |
| 327 | # |
| 328 | # The following data was extracted from the locale.alias file which |
| 329 | # comes with X11 and then hand edited removing the explicit encoding |
| 330 | # definitions and adding some more aliases. The file is usually |
| 331 | # available as /usr/lib/X11/locale/locale.alias. |
| 332 | # |
| 333 | |
| 334 | # |
| 335 | # The encoding_alias table maps lowercase encoding alias names to C |
| 336 | # locale encoding names (case-sensitive). |
| 337 | # |
| 338 | encoding_alias = { |
| 339 | '437': 'C', |
| 340 | 'c': 'C', |
| 341 | 'iso8859': 'ISO8859-1', |
| 342 | '8859': 'ISO8859-1', |
| 343 | '88591': 'ISO8859-1', |
| 344 | 'ascii': 'ISO8859-1', |
| 345 | 'en': 'ISO8859-1', |
| 346 | 'iso88591': 'ISO8859-1', |
| 347 | 'iso_8859-1': 'ISO8859-1', |
| 348 | '885915': 'ISO8859-15', |
| 349 | 'iso885915': 'ISO8859-15', |
| 350 | 'iso_8859-15': 'ISO8859-15', |
| 351 | 'iso8859-2': 'ISO8859-2', |
| 352 | 'iso88592': 'ISO8859-2', |
| 353 | 'iso_8859-2': 'ISO8859-2', |
| 354 | 'iso88595': 'ISO8859-5', |
| 355 | 'iso88596': 'ISO8859-6', |
| 356 | 'iso88597': 'ISO8859-7', |
| 357 | 'iso88598': 'ISO8859-8', |
| 358 | 'iso88599': 'ISO8859-9', |
| 359 | 'iso-2022-jp': 'JIS7', |
| 360 | 'jis': 'JIS7', |
| 361 | 'jis7': 'JIS7', |
| 362 | 'sjis': 'SJIS', |
| 363 | 'tis620': 'TACTIS', |
| 364 | 'ajec': 'eucJP', |
| 365 | 'eucjp': 'eucJP', |
| 366 | 'ujis': 'eucJP', |
| 367 | 'utf-8': 'utf', |
| 368 | 'utf8': 'utf', |
| 369 | 'utf8@ucs4': 'utf', |
| 370 | } |
| 371 | |
| 372 | # |
| 373 | # The locale_alias table maps lowercase alias names to C locale names |
| 374 | # (case-sensitive). Encodings are always separated from the locale |
| 375 | # name using a dot ('.'); they should only be given in case the |
| 376 | # language name is needed to interpret the given encoding alias |
| 377 | # correctly (CJK codes often have this need). |
| 378 | # |
| 379 | locale_alias = { |
| 380 | 'american': 'en_US.ISO8859-1', |
| 381 | 'ar': 'ar_AA.ISO8859-6', |
| 382 | 'ar_aa': 'ar_AA.ISO8859-6', |
| 383 | 'ar_sa': 'ar_SA.ISO8859-6', |
| 384 | 'arabic': 'ar_AA.ISO8859-6', |
| 385 | 'bg': 'bg_BG.ISO8859-5', |
| 386 | 'bg_bg': 'bg_BG.ISO8859-5', |
| 387 | 'bulgarian': 'bg_BG.ISO8859-5', |
| 388 | 'c-french': 'fr_CA.ISO8859-1', |
| 389 | 'c': 'C', |
| 390 | 'c_c': 'C', |
| 391 | 'cextend': 'en_US.ISO8859-1', |
| 392 | 'chinese-s': 'zh_CN.eucCN', |
| 393 | 'chinese-t': 'zh_TW.eucTW', |
| 394 | 'croatian': 'hr_HR.ISO8859-2', |
| 395 | 'cs': 'cs_CZ.ISO8859-2', |
| 396 | 'cs_cs': 'cs_CZ.ISO8859-2', |
| 397 | 'cs_cz': 'cs_CZ.ISO8859-2', |
| 398 | 'cz': 'cz_CZ.ISO8859-2', |
| 399 | 'cz_cz': 'cz_CZ.ISO8859-2', |
| 400 | 'czech': 'cs_CS.ISO8859-2', |
| 401 | 'da': 'da_DK.ISO8859-1', |
| 402 | 'da_dk': 'da_DK.ISO8859-1', |
| 403 | 'danish': 'da_DK.ISO8859-1', |
| 404 | 'de': 'de_DE.ISO8859-1', |
| 405 | 'de_at': 'de_AT.ISO8859-1', |
| 406 | 'de_ch': 'de_CH.ISO8859-1', |
| 407 | 'de_de': 'de_DE.ISO8859-1', |
| 408 | 'dutch': 'nl_BE.ISO8859-1', |
| 409 | 'ee': 'ee_EE.ISO8859-4', |
| 410 | 'el': 'el_GR.ISO8859-7', |
| 411 | 'el_gr': 'el_GR.ISO8859-7', |
| 412 | 'en': 'en_US.ISO8859-1', |
| 413 | 'en_au': 'en_AU.ISO8859-1', |
| 414 | 'en_ca': 'en_CA.ISO8859-1', |
| 415 | 'en_gb': 'en_GB.ISO8859-1', |
| 416 | 'en_ie': 'en_IE.ISO8859-1', |
| 417 | 'en_nz': 'en_NZ.ISO8859-1', |
| 418 | 'en_uk': 'en_GB.ISO8859-1', |
| 419 | 'en_us': 'en_US.ISO8859-1', |
| 420 | 'eng_gb': 'en_GB.ISO8859-1', |
| 421 | 'english': 'en_EN.ISO8859-1', |
| 422 | 'english_uk': 'en_GB.ISO8859-1', |
| 423 | 'english_united-states': 'en_US.ISO8859-1', |
| 424 | 'english_us': 'en_US.ISO8859-1', |
| 425 | 'es': 'es_ES.ISO8859-1', |
| 426 | 'es_ar': 'es_AR.ISO8859-1', |
| 427 | 'es_bo': 'es_BO.ISO8859-1', |
| 428 | 'es_cl': 'es_CL.ISO8859-1', |
| 429 | 'es_co': 'es_CO.ISO8859-1', |
| 430 | 'es_cr': 'es_CR.ISO8859-1', |
| 431 | 'es_ec': 'es_EC.ISO8859-1', |
| 432 | 'es_es': 'es_ES.ISO8859-1', |
| 433 | 'es_gt': 'es_GT.ISO8859-1', |
| 434 | 'es_mx': 'es_MX.ISO8859-1', |
| 435 | 'es_ni': 'es_NI.ISO8859-1', |
| 436 | 'es_pa': 'es_PA.ISO8859-1', |
| 437 | 'es_pe': 'es_PE.ISO8859-1', |
| 438 | 'es_py': 'es_PY.ISO8859-1', |
| 439 | 'es_sv': 'es_SV.ISO8859-1', |
| 440 | 'es_uy': 'es_UY.ISO8859-1', |
| 441 | 'es_ve': 'es_VE.ISO8859-1', |
| 442 | 'et': 'et_EE.ISO8859-4', |
| 443 | 'et_ee': 'et_EE.ISO8859-4', |
| 444 | 'fi': 'fi_FI.ISO8859-1', |
| 445 | 'fi_fi': 'fi_FI.ISO8859-1', |
| 446 | 'finnish': 'fi_FI.ISO8859-1', |
| 447 | 'fr': 'fr_FR.ISO8859-1', |
| 448 | 'fr_be': 'fr_BE.ISO8859-1', |
| 449 | 'fr_ca': 'fr_CA.ISO8859-1', |
| 450 | 'fr_ch': 'fr_CH.ISO8859-1', |
| 451 | 'fr_fr': 'fr_FR.ISO8859-1', |
| 452 | 'fre_fr': 'fr_FR.ISO8859-1', |
| 453 | 'french': 'fr_FR.ISO8859-1', |
| 454 | 'french_france': 'fr_FR.ISO8859-1', |
| 455 | 'ger_de': 'de_DE.ISO8859-1', |
| 456 | 'german': 'de_DE.ISO8859-1', |
| 457 | 'german_germany': 'de_DE.ISO8859-1', |
| 458 | 'greek': 'el_GR.ISO8859-7', |
| 459 | 'hebrew': 'iw_IL.ISO8859-8', |
| 460 | 'hr': 'hr_HR.ISO8859-2', |
| 461 | 'hr_hr': 'hr_HR.ISO8859-2', |
| 462 | 'hu': 'hu_HU.ISO8859-2', |
| 463 | 'hu_hu': 'hu_HU.ISO8859-2', |
| 464 | 'hungarian': 'hu_HU.ISO8859-2', |
| 465 | 'icelandic': 'is_IS.ISO8859-1', |
| 466 | 'id': 'id_ID.ISO8859-1', |
| 467 | 'id_id': 'id_ID.ISO8859-1', |
| 468 | 'is': 'is_IS.ISO8859-1', |
| 469 | 'is_is': 'is_IS.ISO8859-1', |
| 470 | 'iso-8859-1': 'en_US.ISO8859-1', |
| 471 | 'iso-8859-15': 'en_US.ISO8859-15', |
| 472 | 'iso8859-1': 'en_US.ISO8859-1', |
| 473 | 'iso8859-15': 'en_US.ISO8859-15', |
| 474 | 'iso_8859_1': 'en_US.ISO8859-1', |
| 475 | 'iso_8859_15': 'en_US.ISO8859-15', |
| 476 | 'it': 'it_IT.ISO8859-1', |
| 477 | 'it_ch': 'it_CH.ISO8859-1', |
| 478 | 'it_it': 'it_IT.ISO8859-1', |
| 479 | 'italian': 'it_IT.ISO8859-1', |
| 480 | 'iw': 'iw_IL.ISO8859-8', |
| 481 | 'iw_il': 'iw_IL.ISO8859-8', |
| 482 | 'ja': 'ja_JP.eucJP', |
| 483 | 'ja.jis': 'ja_JP.JIS7', |
| 484 | 'ja.sjis': 'ja_JP.SJIS', |
| 485 | 'ja_jp': 'ja_JP.eucJP', |
| 486 | 'ja_jp.ajec': 'ja_JP.eucJP', |
| 487 | 'ja_jp.euc': 'ja_JP.eucJP', |
| 488 | 'ja_jp.eucjp': 'ja_JP.eucJP', |
| 489 | 'ja_jp.iso-2022-jp': 'ja_JP.JIS7', |
| 490 | 'ja_jp.jis': 'ja_JP.JIS7', |
| 491 | 'ja_jp.jis7': 'ja_JP.JIS7', |
| 492 | 'ja_jp.mscode': 'ja_JP.SJIS', |
| 493 | 'ja_jp.sjis': 'ja_JP.SJIS', |
| 494 | 'ja_jp.ujis': 'ja_JP.eucJP', |
| 495 | 'japan': 'ja_JP.eucJP', |
| 496 | 'japanese': 'ja_JP.SJIS', |
| 497 | 'japanese-euc': 'ja_JP.eucJP', |
| 498 | 'japanese.euc': 'ja_JP.eucJP', |
| 499 | 'jp_jp': 'ja_JP.eucJP', |
| 500 | 'ko': 'ko_KR.eucKR', |
| 501 | 'ko_kr': 'ko_KR.eucKR', |
| 502 | 'ko_kr.euc': 'ko_KR.eucKR', |
| 503 | 'korean': 'ko_KR.eucKR', |
| 504 | 'lt': 'lt_LT.ISO8859-4', |
| 505 | 'lv': 'lv_LV.ISO8859-4', |
| 506 | 'mk': 'mk_MK.ISO8859-5', |
| 507 | 'mk_mk': 'mk_MK.ISO8859-5', |
| 508 | 'nl': 'nl_NL.ISO8859-1', |
| 509 | 'nl_be': 'nl_BE.ISO8859-1', |
| 510 | 'nl_nl': 'nl_NL.ISO8859-1', |
| 511 | 'no': 'no_NO.ISO8859-1', |
| 512 | 'no_no': 'no_NO.ISO8859-1', |
| 513 | 'norwegian': 'no_NO.ISO8859-1', |
| 514 | 'pl': 'pl_PL.ISO8859-2', |
| 515 | 'pl_pl': 'pl_PL.ISO8859-2', |
| 516 | 'polish': 'pl_PL.ISO8859-2', |
| 517 | 'portuguese': 'pt_PT.ISO8859-1', |
| 518 | 'portuguese_brazil': 'pt_BR.ISO8859-1', |
| 519 | 'posix': 'C', |
| 520 | 'posix-utf2': 'C', |
| 521 | 'pt': 'pt_PT.ISO8859-1', |
| 522 | 'pt_br': 'pt_BR.ISO8859-1', |
| 523 | 'pt_pt': 'pt_PT.ISO8859-1', |
| 524 | 'ro': 'ro_RO.ISO8859-2', |
| 525 | 'ro_ro': 'ro_RO.ISO8859-2', |
| 526 | 'ru': 'ru_RU.ISO8859-5', |
| 527 | 'ru_ru': 'ru_RU.ISO8859-5', |
| 528 | 'rumanian': 'ro_RO.ISO8859-2', |
| 529 | 'russian': 'ru_RU.ISO8859-5', |
| 530 | 'serbocroatian': 'sh_YU.ISO8859-2', |
| 531 | 'sh': 'sh_YU.ISO8859-2', |
| 532 | 'sh_hr': 'sh_HR.ISO8859-2', |
| 533 | 'sh_sp': 'sh_YU.ISO8859-2', |
| 534 | 'sh_yu': 'sh_YU.ISO8859-2', |
| 535 | 'sk': 'sk_SK.ISO8859-2', |
| 536 | 'sk_sk': 'sk_SK.ISO8859-2', |
| 537 | 'sl': 'sl_CS.ISO8859-2', |
| 538 | 'sl_cs': 'sl_CS.ISO8859-2', |
| 539 | 'sl_si': 'sl_SI.ISO8859-2', |
| 540 | 'slovak': 'sk_SK.ISO8859-2', |
| 541 | 'slovene': 'sl_CS.ISO8859-2', |
| 542 | 'sp': 'sp_YU.ISO8859-5', |
| 543 | 'sp_yu': 'sp_YU.ISO8859-5', |
| 544 | 'spanish': 'es_ES.ISO8859-1', |
| 545 | 'spanish_spain': 'es_ES.ISO8859-1', |
| 546 | 'sr_sp': 'sr_SP.ISO8859-2', |
| 547 | 'sv': 'sv_SE.ISO8859-1', |
| 548 | 'sv_se': 'sv_SE.ISO8859-1', |
| 549 | 'swedish': 'sv_SE.ISO8859-1', |
| 550 | 'th_th': 'th_TH.TACTIS', |
| 551 | 'tr': 'tr_TR.ISO8859-9', |
| 552 | 'tr_tr': 'tr_TR.ISO8859-9', |
| 553 | 'turkish': 'tr_TR.ISO8859-9', |
| 554 | 'univ': 'en_US.utf', |
| 555 | 'universal': 'en_US.utf', |
| 556 | 'zh': 'zh_CN.eucCN', |
| 557 | 'zh_cn': 'zh_CN.eucCN', |
| 558 | 'zh_cn.big5': 'zh_TW.eucTW', |
| 559 | 'zh_cn.euc': 'zh_CN.eucCN', |
| 560 | 'zh_tw': 'zh_TW.eucTW', |
| 561 | 'zh_tw.euc': 'zh_TW.eucTW', |
| 562 | } |
| 563 | |
| 564 | def _print_locale(): |
| 565 | |
| 566 | """ Test function. |
| 567 | """ |
| 568 | categories = {} |
| 569 | def _init_categories(categories=categories): |
| 570 | for k,v in globals().items(): |
| 571 | if k[:3] == 'LC_': |
| 572 | categories[k] = v |
| 573 | _init_categories() |
| 574 | del categories['LC_ALL'] |
| 575 | |
| 576 | print 'Locale defaults as determined by get_default():' |
| 577 | print '-'*72 |
| 578 | lang, enc = get_default() |
| 579 | print 'Language: ', lang or '(undefined)' |
| 580 | print 'Encoding: ', enc or '(undefined)' |
| 581 | print |
| 582 | |
| 583 | print 'Locale settings on startup:' |
| 584 | print '-'*72 |
| 585 | for name,category in categories.items(): |
| 586 | print name,'...' |
| 587 | lang, enc = get_locale(category) |
| 588 | print ' Language: ', lang or '(undefined)' |
| 589 | print ' Encoding: ', enc or '(undefined)' |
| 590 | print |
| 591 | |
| 592 | set_to_default() |
| 593 | print |
| 594 | print 'Locale settings after calling set_to_default():' |
| 595 | print '-'*72 |
| 596 | for name,category in categories.items(): |
| 597 | print name,'...' |
| 598 | lang, enc = get_locale(category) |
| 599 | print ' Language: ', lang or '(undefined)' |
| 600 | print ' Encoding: ', enc or '(undefined)' |
| 601 | print |
| 602 | |
| 603 | try: |
| 604 | setlocale(LC_ALL,"") |
| 605 | except: |
| 606 | print 'NOTE:' |
| 607 | print 'setlocale(LC_ALL,"") does not support the default locale' |
| 608 | print 'given in the OS environment variables.' |
| 609 | else: |
| 610 | print |
| 611 | print 'Locale settings after calling setlocale(LC_ALL,""):' |
| 612 | print '-'*72 |
| 613 | for name,category in categories.items(): |
| 614 | print name,'...' |
| 615 | lang, enc = get_locale(category) |
| 616 | print ' Language: ', lang or '(undefined)' |
| 617 | print ' Encoding: ', enc or '(undefined)' |
| 618 | print |
| 619 | |
| 620 | ### |
Guido van Rossum | eef1d4e | 1997-11-19 19:01:43 +0000 | [diff] [blame] | 621 | |
| 622 | if __name__=='__main__': |
Marc-André Lemburg | 5431bc3 | 2000-06-07 09:11:40 +0000 | [diff] [blame] | 623 | print 'Locale aliasing:' |
| 624 | print |
| 625 | _print_locale() |
| 626 | print |
| 627 | print 'Number formatting:' |
| 628 | print |
| 629 | _test() |