blob: 1014dbd915bd930198b3f91bd76638a8282aad01 [file] [log] [blame]
Marc-André Lemburg5431bc32000-06-07 09:11:40 +00001""" Locale support.
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00002
Marc-André Lemburg5431bc32000-06-07 09:11:40 +00003 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
Fredrik Lundh6c86b992000-07-09 17:12:58 +000014import string, sys
Marc-André Lemburg5431bc32000-06-07 09:11:40 +000015
Fredrik Lundh6c86b992000-07-09 17:12:58 +000016# Try importing the _locale module.
17#
18# If this fails, fall back on a basic 'C' locale emulation.
19#
Guido van Rossumeef1d4e1997-11-19 19:01:43 +000020
Marc-André Lemburg23481142000-06-08 17:49:41 +000021try:
Fredrik Lundh6c86b992000-07-09 17:12:58 +000022
Marc-André Lemburg23481142000-06-08 17:49:41 +000023 from _locale import *
24
25except ImportError:
26
Fredrik Lundh6c86b992000-07-09 17:12:58 +000027 # Locale emulation
28
Marc-André Lemburg23481142000-06-08 17:49:41 +000029 CHAR_MAX = 127
30 LC_ALL = 6
31 LC_COLLATE = 3
32 LC_CTYPE = 0
33 LC_MESSAGES = 5
34 LC_MONETARY = 4
35 LC_NUMERIC = 1
36 LC_TIME = 2
37 Error = ValueError
38
39 def localeconv():
Fredrik Lundh6c86b992000-07-09 17:12:58 +000040 """ localeconv() -> dict.
Marc-André Lemburg23481142000-06-08 17:49:41 +000041 Returns numeric and monetary locale-specific parameters.
42 """
43 # 'C' locale default values
44 return {'grouping': [127],
45 'currency_symbol': '',
46 'n_sign_posn': 127,
Fredrik Lundh6c86b992000-07-09 17:12:58 +000047 'p_cs_precedes': 127,
48 'n_cs_precedes': 127,
49 'mon_grouping': [],
Marc-André Lemburg23481142000-06-08 17:49:41 +000050 'n_sep_by_space': 127,
51 'decimal_point': '.',
52 'negative_sign': '',
53 'positive_sign': '',
Fredrik Lundh6c86b992000-07-09 17:12:58 +000054 'p_sep_by_space': 127,
Marc-André Lemburg23481142000-06-08 17:49:41 +000055 'int_curr_symbol': '',
Fredrik Lundh6c86b992000-07-09 17:12:58 +000056 'p_sign_posn': 127,
Marc-André Lemburg23481142000-06-08 17:49:41 +000057 'thousands_sep': '',
Fredrik Lundh6c86b992000-07-09 17:12:58 +000058 'mon_thousands_sep': '',
59 'frac_digits': 127,
Marc-André Lemburg23481142000-06-08 17:49:41 +000060 'mon_decimal_point': '',
61 'int_frac_digits': 127}
Fredrik Lundh6c86b992000-07-09 17:12:58 +000062
Marc-André Lemburg23481142000-06-08 17:49:41 +000063 def setlocale(category, value=None):
Fredrik Lundh6c86b992000-07-09 17:12:58 +000064 """ setlocale(integer,string=None) -> string.
Marc-André Lemburg23481142000-06-08 17:49:41 +000065 Activates/queries locale processing.
66 """
67 if value is not None and \
68 value is not 'C':
Fredrik Lundh6c86b992000-07-09 17:12:58 +000069 raise Error, '_locale emulation only supports "C" locale'
Marc-André Lemburg23481142000-06-08 17:49:41 +000070 return 'C'
71
72 def strcoll(a,b):
Fredrik Lundh6c86b992000-07-09 17:12:58 +000073 """ strcoll(string,string) -> int.
Marc-André Lemburg23481142000-06-08 17:49:41 +000074 Compares two strings according to the locale.
75 """
76 return cmp(a,b)
77
78 def strxfrm(s):
Fredrik Lundh6c86b992000-07-09 17:12:58 +000079 """ strxfrm(string) -> string.
Marc-André Lemburg23481142000-06-08 17:49:41 +000080 Returns a string that behaves for cmp locale-aware.
81 """
82 return s
Marc-André Lemburg5431bc32000-06-07 09:11:40 +000083
84### Number formatting APIs
85
86# Author: Martin von Loewis
Guido van Rossumeef1d4e1997-11-19 19:01:43 +000087
88#perform the grouping from right to left
89def _group(s):
90 conv=localeconv()
91 grouping=conv['grouping']
92 if not grouping:return s
93 result=""
94 while s and grouping:
Fredrik Lundh6c86b992000-07-09 17:12:58 +000095 # if grouping is -1, we are done
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000096 if grouping[0]==CHAR_MAX:
97 break
98 # 0: re-use last group ad infinitum
99 elif grouping[0]!=0:
100 #process last group
101 group=grouping[0]
102 grouping=grouping[1:]
103 if result:
104 result=s[-group:]+conv['thousands_sep']+result
105 else:
106 result=s[-group:]
107 s=s[:-group]
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000108 if not result:
109 return s
110 if s:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000111 result=s+conv['thousands_sep']+result
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000112 return result
113
114def format(f,val,grouping=0):
115 """Formats a value in the same way that the % formatting would use,
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000116 but takes the current locale into account.
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000117 Grouping is applied if the third parameter is true."""
118 result = f % val
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000119 fields = string.split(result, ".")
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000120 if grouping:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000121 fields[0]=_group(fields[0])
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000122 if len(fields)==2:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 return fields[0]+localeconv()['decimal_point']+fields[1]
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000124 elif len(fields)==1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 return fields[0]
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000126 else:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000127 raise Error, "Too many decimal points in result string"
128
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000129def str(val):
130 """Convert float to integer, taking the locale into account."""
131 return format("%.12g",val)
132
133def atof(str,func=string.atof):
134 "Parses a string as a float according to the locale settings."
135 #First, get rid of the grouping
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000136 ts = localeconv()['thousands_sep']
137 if ts:
138 s=string.split(str,ts)
139 str=string.join(s, "")
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000140 #next, replace the decimal point with a dot
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000141 dd = localeconv()['decimal_point']
142 if dd:
143 s=string.split(str,dd)
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000144 str=string.join(s, '.')
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000145 #finally, parse the string
146 return func(str)
147
148def atoi(str):
149 "Converts a string to an integer according to the locale settings."
150 return atof(str,string.atoi)
151
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000152def _test():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000153 setlocale(LC_ALL, "")
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000154 #do grouping
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000155 s1=format("%d", 123456789,1)
156 print s1, "is", atoi(s1)
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000157 #standard formatting
158 s1=str(3.14)
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000159 print s1, "is", atof(s1)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000160
161### Locale name aliasing engine
162
163# Author: Marc-Andre Lemburg, mal@lemburg.com
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000164# Various tweaks by Fredrik Lundh <effbot@telia.com>
165
166# store away the low-level version of setlocale (it's
167# overridden below)
168_setlocale = setlocale
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000169
170def normalize(localename):
171
172 """ Returns a normalized locale code for the given locale
173 name.
174
175 The returned locale code is formatted for use with
176 setlocale().
177
178 If normalization fails, the original name is returned
179 unchanged.
180
181 If the given encoding is not known, the function defaults to
182 the default encoding for the locale code just like setlocale()
183 does.
184
185 """
186 # Normalize the locale name and extract the encoding
187 fullname = string.lower(localename)
188 if ':' in fullname:
189 # ':' is sometimes used as encoding delimiter.
190 fullname = string.replace(fullname, ':', '.')
191 if '.' in fullname:
192 langname, encoding = string.split(fullname, '.')[:2]
193 fullname = langname + '.' + encoding
194 else:
195 langname = fullname
196 encoding = ''
197
198 # First lookup: fullname (possibly with encoding)
199 code = locale_alias.get(fullname, None)
200 if code is not None:
201 return code
202
203 # Second try: langname (without encoding)
204 code = locale_alias.get(langname, None)
205 if code is not None:
206 if '.' in code:
207 langname, defenc = string.split(code, '.')
208 else:
209 langname = code
210 defenc = ''
211 if encoding:
212 encoding = encoding_alias.get(encoding, encoding)
213 else:
214 encoding = defenc
215 if encoding:
216 return langname + '.' + encoding
217 else:
218 return langname
219
220 else:
221 return localename
222
223def _parse_localename(localename):
224
225 """ Parses the locale code for localename and returns the
226 result as tuple (language code, encoding).
227
228 The localename is normalized and passed through the locale
229 alias engine. A ValueError is raised in case the locale name
230 cannot be parsed.
231
232 The language code corresponds to RFC 1766. code and encoding
233 can be None in case the values cannot be determined or are
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000234 unknown to this implementation.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000235
236 """
237 code = normalize(localename)
238 if '.' in code:
239 return string.split(code, '.')[:2]
240 elif code == 'C':
241 return None, None
242 else:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000243 raise ValueError, 'unknown locale: %s' % localename
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000244 return l
245
246def _build_localename(localetuple):
247
248 """ Builds a locale code from the given tuple (language code,
249 encoding).
250
251 No aliasing or normalizing takes place.
252
253 """
254 language, encoding = localetuple
255 if language is None:
256 language = 'C'
257 if encoding is None:
258 return language
259 else:
260 return language + '.' + encoding
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000261
262def getdefaultlocale(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000263
264 """ Tries to determine the default locale settings and returns
265 them as tuple (language code, encoding).
266
267 According to POSIX, a program which has not called
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000268 setlocale(LC_ALL, "") runs using the portable 'C' locale.
269 Calling setlocale(LC_ALL, "") lets it use the default locale as
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000270 defined by the LANG variable. Since we don't want to interfere
271 with the current locale setting we thus emulate the behaviour
272 in the way described above.
273
274 To maintain compatibility with other platforms, not only the
275 LANG variable is tested, but a list of variables given as
276 envvars parameter. The first found to be defined will be
277 used. envvars defaults to the search path used in GNU gettext;
278 it must always contain the variable name 'LANG'.
279
280 Except for the code 'C', the language code corresponds to RFC
281 1766. code and encoding can be None in case the values cannot
282 be determined.
283
284 """
Fredrik Lundh04661322000-07-09 23:16:10 +0000285
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000286 try:
287 # check if it's supported by the _locale module
288 import _locale
289 code, encoding = _locale._getdefaultlocale()
Fredrik Lundh04661322000-07-09 23:16:10 +0000290 except (ImportError, AttributeError):
291 pass
292 else:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000293 if sys.platform == "win32" and code and code[:2] == "0x":
294 # map windows language identifier to language name
295 code = windows_locale.get(int(code, 0))
296 return code, encoding
Fredrik Lundh04661322000-07-09 23:16:10 +0000297
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000298 # fall back on POSIX behaviour
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000299 import os
300 lookup = os.environ.get
301 for variable in envvars:
302 localename = lookup(variable,None)
303 if localename is not None:
304 break
305 else:
306 localename = 'C'
307 return _parse_localename(localename)
308
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000309# compatibility
310get_default = getdefaultlocale
311
312def getlocale(category=LC_CTYPE):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000313
314 """ Returns the current setting for the given locale category as
315 tuple (language code, encoding).
316
317 category may be one of the LC_* value except LC_ALL. It
318 defaults to LC_CTYPE.
319
320 Except for the code 'C', the language code corresponds to RFC
321 1766. code and encoding can be None in case the values cannot
322 be determined.
323
324 """
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000325 localename = _setlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000326 if category == LC_ALL and ';' in localename:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000327 raise TypeError, 'category LC_ALL is not supported'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000328 return _parse_localename(localename)
329
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000330def setlocale(category, locale=None):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000331
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000332 """ Set the locale for the given category. The locale can be
333 a string, a locale tuple (language code, encoding), or None.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000334
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000335 Locale tuples are converted to strings the locale aliasing
336 engine. Locale strings are passed directly to the C lib.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000337
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000338 category may be given as one of the LC_* values.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000339
340 """
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000341 if locale and type(locale) is not type(""):
342 # convert to string
343 locale = normalize(_build_localename(locale))
344 return _setlocale(category, locale)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000345
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000346def resetlocale(category=LC_ALL):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000347
348 """ Sets the locale for category to the default setting.
349
350 The default setting is determined by calling
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000351 getdefaultlocale(). category defaults to LC_ALL.
352
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000353 """
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000354 _setlocale(category, _build_localename(getdefaultlocale()))
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000355
356### Database
357#
358# The following data was extracted from the locale.alias file which
359# comes with X11 and then hand edited removing the explicit encoding
360# definitions and adding some more aliases. The file is usually
361# available as /usr/lib/X11/locale/locale.alias.
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000362#
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000363
364#
365# The encoding_alias table maps lowercase encoding alias names to C
366# locale encoding names (case-sensitive).
367#
368encoding_alias = {
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000369 '437': 'C',
370 'c': 'C',
371 'iso8859': 'ISO8859-1',
372 '8859': 'ISO8859-1',
373 '88591': 'ISO8859-1',
374 'ascii': 'ISO8859-1',
375 'en': 'ISO8859-1',
376 'iso88591': 'ISO8859-1',
377 'iso_8859-1': 'ISO8859-1',
378 '885915': 'ISO8859-15',
379 'iso885915': 'ISO8859-15',
380 'iso_8859-15': 'ISO8859-15',
381 'iso8859-2': 'ISO8859-2',
382 'iso88592': 'ISO8859-2',
383 'iso_8859-2': 'ISO8859-2',
384 'iso88595': 'ISO8859-5',
385 'iso88596': 'ISO8859-6',
386 'iso88597': 'ISO8859-7',
387 'iso88598': 'ISO8859-8',
388 'iso88599': 'ISO8859-9',
389 'iso-2022-jp': 'JIS7',
390 'jis': 'JIS7',
391 'jis7': 'JIS7',
392 'sjis': 'SJIS',
393 'tis620': 'TACTIS',
394 'ajec': 'eucJP',
395 'eucjp': 'eucJP',
396 'ujis': 'eucJP',
397 'utf-8': 'utf',
398 'utf8': 'utf',
399 'utf8@ucs4': 'utf',
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000400}
401
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000402#
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000403# The locale_alias table maps lowercase alias names to C locale names
404# (case-sensitive). Encodings are always separated from the locale
405# name using a dot ('.'); they should only be given in case the
406# language name is needed to interpret the given encoding alias
407# correctly (CJK codes often have this need).
408#
409locale_alias = {
410 'american': 'en_US.ISO8859-1',
411 'ar': 'ar_AA.ISO8859-6',
412 'ar_aa': 'ar_AA.ISO8859-6',
413 'ar_sa': 'ar_SA.ISO8859-6',
414 'arabic': 'ar_AA.ISO8859-6',
415 'bg': 'bg_BG.ISO8859-5',
416 'bg_bg': 'bg_BG.ISO8859-5',
417 'bulgarian': 'bg_BG.ISO8859-5',
418 'c-french': 'fr_CA.ISO8859-1',
419 'c': 'C',
420 'c_c': 'C',
421 'cextend': 'en_US.ISO8859-1',
422 'chinese-s': 'zh_CN.eucCN',
423 'chinese-t': 'zh_TW.eucTW',
424 'croatian': 'hr_HR.ISO8859-2',
425 'cs': 'cs_CZ.ISO8859-2',
426 'cs_cs': 'cs_CZ.ISO8859-2',
427 'cs_cz': 'cs_CZ.ISO8859-2',
428 'cz': 'cz_CZ.ISO8859-2',
429 'cz_cz': 'cz_CZ.ISO8859-2',
430 'czech': 'cs_CS.ISO8859-2',
431 'da': 'da_DK.ISO8859-1',
432 'da_dk': 'da_DK.ISO8859-1',
433 'danish': 'da_DK.ISO8859-1',
434 'de': 'de_DE.ISO8859-1',
435 'de_at': 'de_AT.ISO8859-1',
436 'de_ch': 'de_CH.ISO8859-1',
437 'de_de': 'de_DE.ISO8859-1',
438 'dutch': 'nl_BE.ISO8859-1',
439 'ee': 'ee_EE.ISO8859-4',
440 'el': 'el_GR.ISO8859-7',
441 'el_gr': 'el_GR.ISO8859-7',
442 'en': 'en_US.ISO8859-1',
443 'en_au': 'en_AU.ISO8859-1',
444 'en_ca': 'en_CA.ISO8859-1',
445 'en_gb': 'en_GB.ISO8859-1',
446 'en_ie': 'en_IE.ISO8859-1',
447 'en_nz': 'en_NZ.ISO8859-1',
448 'en_uk': 'en_GB.ISO8859-1',
449 'en_us': 'en_US.ISO8859-1',
450 'eng_gb': 'en_GB.ISO8859-1',
451 'english': 'en_EN.ISO8859-1',
452 'english_uk': 'en_GB.ISO8859-1',
453 'english_united-states': 'en_US.ISO8859-1',
454 'english_us': 'en_US.ISO8859-1',
455 'es': 'es_ES.ISO8859-1',
456 'es_ar': 'es_AR.ISO8859-1',
457 'es_bo': 'es_BO.ISO8859-1',
458 'es_cl': 'es_CL.ISO8859-1',
459 'es_co': 'es_CO.ISO8859-1',
460 'es_cr': 'es_CR.ISO8859-1',
461 'es_ec': 'es_EC.ISO8859-1',
462 'es_es': 'es_ES.ISO8859-1',
463 'es_gt': 'es_GT.ISO8859-1',
464 'es_mx': 'es_MX.ISO8859-1',
465 'es_ni': 'es_NI.ISO8859-1',
466 'es_pa': 'es_PA.ISO8859-1',
467 'es_pe': 'es_PE.ISO8859-1',
468 'es_py': 'es_PY.ISO8859-1',
469 'es_sv': 'es_SV.ISO8859-1',
470 'es_uy': 'es_UY.ISO8859-1',
471 'es_ve': 'es_VE.ISO8859-1',
472 'et': 'et_EE.ISO8859-4',
473 'et_ee': 'et_EE.ISO8859-4',
474 'fi': 'fi_FI.ISO8859-1',
475 'fi_fi': 'fi_FI.ISO8859-1',
476 'finnish': 'fi_FI.ISO8859-1',
477 'fr': 'fr_FR.ISO8859-1',
478 'fr_be': 'fr_BE.ISO8859-1',
479 'fr_ca': 'fr_CA.ISO8859-1',
480 'fr_ch': 'fr_CH.ISO8859-1',
481 'fr_fr': 'fr_FR.ISO8859-1',
482 'fre_fr': 'fr_FR.ISO8859-1',
483 'french': 'fr_FR.ISO8859-1',
484 'french_france': 'fr_FR.ISO8859-1',
485 'ger_de': 'de_DE.ISO8859-1',
486 'german': 'de_DE.ISO8859-1',
487 'german_germany': 'de_DE.ISO8859-1',
488 'greek': 'el_GR.ISO8859-7',
489 'hebrew': 'iw_IL.ISO8859-8',
490 'hr': 'hr_HR.ISO8859-2',
491 'hr_hr': 'hr_HR.ISO8859-2',
492 'hu': 'hu_HU.ISO8859-2',
493 'hu_hu': 'hu_HU.ISO8859-2',
494 'hungarian': 'hu_HU.ISO8859-2',
495 'icelandic': 'is_IS.ISO8859-1',
496 'id': 'id_ID.ISO8859-1',
497 'id_id': 'id_ID.ISO8859-1',
498 'is': 'is_IS.ISO8859-1',
499 'is_is': 'is_IS.ISO8859-1',
500 'iso-8859-1': 'en_US.ISO8859-1',
501 'iso-8859-15': 'en_US.ISO8859-15',
502 'iso8859-1': 'en_US.ISO8859-1',
503 'iso8859-15': 'en_US.ISO8859-15',
504 'iso_8859_1': 'en_US.ISO8859-1',
505 'iso_8859_15': 'en_US.ISO8859-15',
506 'it': 'it_IT.ISO8859-1',
507 'it_ch': 'it_CH.ISO8859-1',
508 'it_it': 'it_IT.ISO8859-1',
509 'italian': 'it_IT.ISO8859-1',
510 'iw': 'iw_IL.ISO8859-8',
511 'iw_il': 'iw_IL.ISO8859-8',
512 'ja': 'ja_JP.eucJP',
513 'ja.jis': 'ja_JP.JIS7',
514 'ja.sjis': 'ja_JP.SJIS',
515 'ja_jp': 'ja_JP.eucJP',
516 'ja_jp.ajec': 'ja_JP.eucJP',
517 'ja_jp.euc': 'ja_JP.eucJP',
518 'ja_jp.eucjp': 'ja_JP.eucJP',
519 'ja_jp.iso-2022-jp': 'ja_JP.JIS7',
520 'ja_jp.jis': 'ja_JP.JIS7',
521 'ja_jp.jis7': 'ja_JP.JIS7',
522 'ja_jp.mscode': 'ja_JP.SJIS',
523 'ja_jp.sjis': 'ja_JP.SJIS',
524 'ja_jp.ujis': 'ja_JP.eucJP',
525 'japan': 'ja_JP.eucJP',
526 'japanese': 'ja_JP.SJIS',
527 'japanese-euc': 'ja_JP.eucJP',
528 'japanese.euc': 'ja_JP.eucJP',
529 'jp_jp': 'ja_JP.eucJP',
530 'ko': 'ko_KR.eucKR',
531 'ko_kr': 'ko_KR.eucKR',
532 'ko_kr.euc': 'ko_KR.eucKR',
533 'korean': 'ko_KR.eucKR',
534 'lt': 'lt_LT.ISO8859-4',
535 'lv': 'lv_LV.ISO8859-4',
536 'mk': 'mk_MK.ISO8859-5',
537 'mk_mk': 'mk_MK.ISO8859-5',
538 'nl': 'nl_NL.ISO8859-1',
539 'nl_be': 'nl_BE.ISO8859-1',
540 'nl_nl': 'nl_NL.ISO8859-1',
541 'no': 'no_NO.ISO8859-1',
542 'no_no': 'no_NO.ISO8859-1',
543 'norwegian': 'no_NO.ISO8859-1',
544 'pl': 'pl_PL.ISO8859-2',
545 'pl_pl': 'pl_PL.ISO8859-2',
546 'polish': 'pl_PL.ISO8859-2',
547 'portuguese': 'pt_PT.ISO8859-1',
548 'portuguese_brazil': 'pt_BR.ISO8859-1',
549 'posix': 'C',
550 'posix-utf2': 'C',
551 'pt': 'pt_PT.ISO8859-1',
552 'pt_br': 'pt_BR.ISO8859-1',
553 'pt_pt': 'pt_PT.ISO8859-1',
554 'ro': 'ro_RO.ISO8859-2',
555 'ro_ro': 'ro_RO.ISO8859-2',
556 'ru': 'ru_RU.ISO8859-5',
557 'ru_ru': 'ru_RU.ISO8859-5',
558 'rumanian': 'ro_RO.ISO8859-2',
559 'russian': 'ru_RU.ISO8859-5',
560 'serbocroatian': 'sh_YU.ISO8859-2',
561 'sh': 'sh_YU.ISO8859-2',
562 'sh_hr': 'sh_HR.ISO8859-2',
563 'sh_sp': 'sh_YU.ISO8859-2',
564 'sh_yu': 'sh_YU.ISO8859-2',
565 'sk': 'sk_SK.ISO8859-2',
566 'sk_sk': 'sk_SK.ISO8859-2',
567 'sl': 'sl_CS.ISO8859-2',
568 'sl_cs': 'sl_CS.ISO8859-2',
569 'sl_si': 'sl_SI.ISO8859-2',
570 'slovak': 'sk_SK.ISO8859-2',
571 'slovene': 'sl_CS.ISO8859-2',
572 'sp': 'sp_YU.ISO8859-5',
573 'sp_yu': 'sp_YU.ISO8859-5',
574 'spanish': 'es_ES.ISO8859-1',
575 'spanish_spain': 'es_ES.ISO8859-1',
576 'sr_sp': 'sr_SP.ISO8859-2',
577 'sv': 'sv_SE.ISO8859-1',
578 'sv_se': 'sv_SE.ISO8859-1',
579 'swedish': 'sv_SE.ISO8859-1',
580 'th_th': 'th_TH.TACTIS',
581 'tr': 'tr_TR.ISO8859-9',
582 'tr_tr': 'tr_TR.ISO8859-9',
583 'turkish': 'tr_TR.ISO8859-9',
584 'univ': 'en_US.utf',
585 'universal': 'en_US.utf',
586 'zh': 'zh_CN.eucCN',
587 'zh_cn': 'zh_CN.eucCN',
588 'zh_cn.big5': 'zh_TW.eucTW',
589 'zh_cn.euc': 'zh_CN.eucCN',
590 'zh_tw': 'zh_TW.eucTW',
591 'zh_tw.euc': 'zh_TW.eucTW',
592}
593
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000594#
595# this maps windows language identifiers (as used on Windows 95 and
596# earlier) to locale strings.
597#
598# NOTE: this mapping is incomplete. If your language is missing, send
599# a note with the missing language identifier and the suggested locale
600# code to Fredrik Lundh <effbot@telia.com>. Thanks /F
601
602windows_locale = {
603 0x0404: "zh_TW", # Chinese (Taiwan)
604 0x0804: "zh_CN", # Chinese (PRC)
605 0x0406: "da_DK", # Danish
606 0x0413: "nl_NL", # Dutch (Netherlands)
607 0x0409: "en_US", # English (United States)
608 0x0809: "en_UK", # English (United Kingdom)
609 0x0c09: "en_AU", # English (Australian)
610 0x1009: "en_CA", # English (Canadian)
611 0x1409: "en_NZ", # English (New Zealand)
612 0x1809: "en_IE", # English (Ireland)
613 0x1c09: "en_ZA", # English (South Africa)
614 0x040b: "fi_FI", # Finnish
615 0x040c: "fr_FR", # French (Standard)
616 0x080c: "fr_BE", # French (Belgian)
617 0x0c0c: "fr_CA", # French (Canadian)
618 0x100c: "fr_CH", # French (Switzerland)
619 0x0407: "de_DE", # German (Standard)
620 0x0408: "el_GR", # Greek
621 0x040d: "iw_IL", # Hebrew
622 0x040f: "is_IS", # Icelandic
623 0x0410: "it_IT", # Italian (Standard)
624 0x0411: "ja_JA", # Japanese
625 0x0414: "no_NO", # Norwegian (Bokmal)
626 0x0816: "pt_PT", # Portuguese (Standard)
627 0x0c0a: "es_ES", # Spanish (Modern Sort)
628 0x0441: "sw_KE", # Swahili (Kenya)
629 0x041d: "sv_SE", # Swedish
630 0x081d: "sv_FI", # Swedish (Finland)
631 0x041f: "tr_TR", # Turkish
632}
633
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000634def _print_locale():
635
636 """ Test function.
637 """
638 categories = {}
639 def _init_categories(categories=categories):
640 for k,v in globals().items():
641 if k[:3] == 'LC_':
642 categories[k] = v
643 _init_categories()
644 del categories['LC_ALL']
645
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000646 print 'Locale defaults as determined by getdefaultlocale():'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000647 print '-'*72
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000648 lang, enc = getdefaultlocale()
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000649 print 'Language: ', lang or '(undefined)'
650 print 'Encoding: ', enc or '(undefined)'
651 print
652
653 print 'Locale settings on startup:'
654 print '-'*72
655 for name,category in categories.items():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000656 print name, '...'
657 lang, enc = getlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000658 print ' Language: ', lang or '(undefined)'
659 print ' Encoding: ', enc or '(undefined)'
660 print
661
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000662 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000663 print 'Locale settings after calling resetlocale():'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000664 print '-'*72
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000665 resetlocale()
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000666 for name,category in categories.items():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000667 print name, '...'
668 lang, enc = getlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000669 print ' Language: ', lang or '(undefined)'
670 print ' Encoding: ', enc or '(undefined)'
671 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000672
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000673 try:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000674 setlocale(LC_ALL, "")
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000675 except:
676 print 'NOTE:'
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000677 print 'setlocale(LC_ALL, "") does not support the default locale'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000678 print 'given in the OS environment variables.'
679 else:
680 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000681 print 'Locale settings after calling setlocale(LC_ALL, ""):'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000682 print '-'*72
683 for name,category in categories.items():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000684 print name, '...'
685 lang, enc = getlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000686 print ' Language: ', lang or '(undefined)'
687 print ' Encoding: ', enc or '(undefined)'
688 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000689
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000690###
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000691
692if __name__=='__main__':
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000693 print 'Locale aliasing:'
694 print
695 _print_locale()
696 print
697 print 'Number formatting:'
698 print
699 _test()