blob: dd7df1e743bc30fe0f5189591db0adb81f143184 [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
Eric S. Raymondbe9b5072001-02-09 10:48:30 +000014import 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.
Guido van Rossumeef1d4e1997-11-19 19:01:43 +000019
Tim Peters1baf8292001-01-24 10:13:46 +000020# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
21# trying the import. So __all__ is also fiddled at the end of the file.
Skip Montanaro17ab1232001-01-24 06:27:27 +000022__all__ = ["setlocale","Error","localeconv","strcoll","strxfrm",
23 "format","str","atof","atoi","LC_CTYPE","LC_COLLATE",
Tim Peters1baf8292001-01-24 10:13:46 +000024 "LC_TIME","LC_MONETARY","LC_NUMERIC", "LC_ALL","CHAR_MAX"]
Skip Montanaro17ab1232001-01-24 06:27:27 +000025
Marc-André Lemburg23481142000-06-08 17:49:41 +000026try:
Fredrik Lundh6c86b992000-07-09 17:12:58 +000027
Marc-André Lemburg23481142000-06-08 17:49:41 +000028 from _locale import *
29
30except ImportError:
31
Fredrik Lundh6c86b992000-07-09 17:12:58 +000032 # Locale emulation
33
Marc-André Lemburg23481142000-06-08 17:49:41 +000034 CHAR_MAX = 127
35 LC_ALL = 6
36 LC_COLLATE = 3
37 LC_CTYPE = 0
38 LC_MESSAGES = 5
39 LC_MONETARY = 4
40 LC_NUMERIC = 1
41 LC_TIME = 2
42 Error = ValueError
43
44 def localeconv():
Fredrik Lundh6c86b992000-07-09 17:12:58 +000045 """ localeconv() -> dict.
Marc-André Lemburg23481142000-06-08 17:49:41 +000046 Returns numeric and monetary locale-specific parameters.
47 """
48 # 'C' locale default values
49 return {'grouping': [127],
50 'currency_symbol': '',
51 'n_sign_posn': 127,
Fredrik Lundh6c86b992000-07-09 17:12:58 +000052 'p_cs_precedes': 127,
53 'n_cs_precedes': 127,
54 'mon_grouping': [],
Marc-André Lemburg23481142000-06-08 17:49:41 +000055 'n_sep_by_space': 127,
56 'decimal_point': '.',
57 'negative_sign': '',
58 'positive_sign': '',
Fredrik Lundh6c86b992000-07-09 17:12:58 +000059 'p_sep_by_space': 127,
Marc-André Lemburg23481142000-06-08 17:49:41 +000060 'int_curr_symbol': '',
Fredrik Lundh6c86b992000-07-09 17:12:58 +000061 'p_sign_posn': 127,
Marc-André Lemburg23481142000-06-08 17:49:41 +000062 'thousands_sep': '',
Fredrik Lundh6c86b992000-07-09 17:12:58 +000063 'mon_thousands_sep': '',
64 'frac_digits': 127,
Marc-André Lemburg23481142000-06-08 17:49:41 +000065 'mon_decimal_point': '',
66 'int_frac_digits': 127}
Fredrik Lundh6c86b992000-07-09 17:12:58 +000067
Marc-André Lemburg23481142000-06-08 17:49:41 +000068 def setlocale(category, value=None):
Fredrik Lundh6c86b992000-07-09 17:12:58 +000069 """ setlocale(integer,string=None) -> string.
Marc-André Lemburg23481142000-06-08 17:49:41 +000070 Activates/queries locale processing.
71 """
Barry Warsaw7519e7a2001-03-23 17:00:07 +000072 if value is not None and value != 'C':
Fredrik Lundh6c86b992000-07-09 17:12:58 +000073 raise Error, '_locale emulation only supports "C" locale'
Marc-André Lemburg23481142000-06-08 17:49:41 +000074 return 'C'
75
76 def strcoll(a,b):
Fredrik Lundh6c86b992000-07-09 17:12:58 +000077 """ strcoll(string,string) -> int.
Marc-André Lemburg23481142000-06-08 17:49:41 +000078 Compares two strings according to the locale.
79 """
80 return cmp(a,b)
81
82 def strxfrm(s):
Fredrik Lundh6c86b992000-07-09 17:12:58 +000083 """ strxfrm(string) -> string.
Marc-André Lemburg23481142000-06-08 17:49:41 +000084 Returns a string that behaves for cmp locale-aware.
85 """
86 return s
Marc-André Lemburg5431bc32000-06-07 09:11:40 +000087
88### Number formatting APIs
89
90# Author: Martin von Loewis
Guido van Rossumeef1d4e1997-11-19 19:01:43 +000091
92#perform the grouping from right to left
93def _group(s):
94 conv=localeconv()
95 grouping=conv['grouping']
Guido van Rossum67addfe2001-04-16 16:04:10 +000096 if not grouping:return (s, 0)
Guido van Rossumeef1d4e1997-11-19 19:01:43 +000097 result=""
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000098 seps = 0
99 spaces = ""
100 if s[-1] == ' ':
101 sp = s.find(' ')
102 spaces = s[sp:]
103 s = s[:sp]
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000104 while s and grouping:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000105 # if grouping is -1, we are done
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000106 if grouping[0]==CHAR_MAX:
107 break
108 # 0: re-use last group ad infinitum
109 elif grouping[0]!=0:
110 #process last group
111 group=grouping[0]
112 grouping=grouping[1:]
113 if result:
114 result=s[-group:]+conv['thousands_sep']+result
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000115 seps += 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000116 else:
117 result=s[-group:]
118 s=s[:-group]
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000119 if s and s[-1] not in "0123456789":
120 # the leading string is only spaces and signs
121 return s+result+spaces,seps
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000122 if not result:
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000123 return s+spaces,seps
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000124 if s:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 result=s+conv['thousands_sep']+result
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000126 seps += 1
127 return result+spaces,seps
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000128
129def format(f,val,grouping=0):
130 """Formats a value in the same way that the % formatting would use,
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000131 but takes the current locale into account.
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000132 Grouping is applied if the third parameter is true."""
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000133 result = f % val
Martin v. Löwisdb786872001-01-21 18:52:33 +0000134 fields = result.split(".")
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000135 seps = 0
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000136 if grouping:
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000137 fields[0],seps=_group(fields[0])
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000138 if len(fields)==2:
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000139 result = fields[0]+localeconv()['decimal_point']+fields[1]
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000140 elif len(fields)==1:
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000141 result = fields[0]
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000142 else:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000143 raise Error, "Too many decimal points in result string"
144
Martin v. Löwis88ad12a2001-04-13 08:09:50 +0000145 while seps:
146 # If the number was formatted for a specific width, then it
147 # might have been filled with spaces to the left or right. If
148 # so, kill as much spaces as there where separators.
149 # Leading zeroes as fillers are not yet dealt with, as it is
150 # not clear how they should interact with grouping.
151 sp = result.find(" ")
152 if sp==-1:break
153 result = result[:sp]+result[sp+1:]
154 seps -= 1
155
156 return result
Martin v. Löwisdb786872001-01-21 18:52:33 +0000157
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000158def str(val):
159 """Convert float to integer, taking the locale into account."""
160 return format("%.12g",val)
161
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000162def atof(str,func=float):
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000163 "Parses a string as a float according to the locale settings."
164 #First, get rid of the grouping
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000165 ts = localeconv()['thousands_sep']
166 if ts:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000167 s=str.split(ts)
168 str="".join(s)
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000169 #next, replace the decimal point with a dot
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000170 dd = localeconv()['decimal_point']
171 if dd:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000172 s=str.split(dd)
173 str='.'.join(s)
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000174 #finally, parse the string
175 return func(str)
176
177def atoi(str):
178 "Converts a string to an integer according to the locale settings."
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000179 return atof(str, int)
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000180
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000181def _test():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000182 setlocale(LC_ALL, "")
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000183 #do grouping
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000184 s1=format("%d", 123456789,1)
185 print s1, "is", atoi(s1)
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000186 #standard formatting
187 s1=str(3.14)
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000188 print s1, "is", atof(s1)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000189
190### Locale name aliasing engine
191
192# Author: Marc-Andre Lemburg, mal@lemburg.com
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000193# Various tweaks by Fredrik Lundh <effbot@telia.com>
194
195# store away the low-level version of setlocale (it's
196# overridden below)
197_setlocale = setlocale
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000198
199def normalize(localename):
200
201 """ Returns a normalized locale code for the given locale
202 name.
203
204 The returned locale code is formatted for use with
205 setlocale().
206
207 If normalization fails, the original name is returned
208 unchanged.
209
210 If the given encoding is not known, the function defaults to
211 the default encoding for the locale code just like setlocale()
212 does.
213
214 """
215 # Normalize the locale name and extract the encoding
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000216 fullname = localename.lower()
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000217 if ':' in fullname:
218 # ':' is sometimes used as encoding delimiter.
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000219 fullname = fullname.replace(':', '.')
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000220 if '.' in fullname:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000221 langname, encoding = fullname.split('.')[:2]
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000222 fullname = langname + '.' + encoding
223 else:
224 langname = fullname
225 encoding = ''
226
227 # First lookup: fullname (possibly with encoding)
228 code = locale_alias.get(fullname, None)
229 if code is not None:
230 return code
231
232 # Second try: langname (without encoding)
233 code = locale_alias.get(langname, None)
234 if code is not None:
235 if '.' in code:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000236 langname, defenc = code.split('.')
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000237 else:
238 langname = code
239 defenc = ''
240 if encoding:
241 encoding = encoding_alias.get(encoding, encoding)
242 else:
243 encoding = defenc
244 if encoding:
245 return langname + '.' + encoding
246 else:
247 return langname
248
249 else:
250 return localename
251
252def _parse_localename(localename):
253
254 """ Parses the locale code for localename and returns the
255 result as tuple (language code, encoding).
256
257 The localename is normalized and passed through the locale
258 alias engine. A ValueError is raised in case the locale name
259 cannot be parsed.
260
261 The language code corresponds to RFC 1766. code and encoding
262 can be None in case the values cannot be determined or are
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000263 unknown to this implementation.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000264
265 """
266 code = normalize(localename)
267 if '.' in code:
Eric S. Raymondbe9b5072001-02-09 10:48:30 +0000268 return code.split('.')[:2]
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000269 elif code == 'C':
270 return None, None
271 else:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000272 raise ValueError, 'unknown locale: %s' % localename
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000273 return l
274
275def _build_localename(localetuple):
276
277 """ Builds a locale code from the given tuple (language code,
278 encoding).
279
280 No aliasing or normalizing takes place.
281
282 """
283 language, encoding = localetuple
284 if language is None:
285 language = 'C'
286 if encoding is None:
287 return language
288 else:
289 return language + '.' + encoding
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000290
291def getdefaultlocale(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000292
293 """ Tries to determine the default locale settings and returns
294 them as tuple (language code, encoding).
295
296 According to POSIX, a program which has not called
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000297 setlocale(LC_ALL, "") runs using the portable 'C' locale.
298 Calling setlocale(LC_ALL, "") lets it use the default locale as
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000299 defined by the LANG variable. Since we don't want to interfere
Thomas Wouters7e474022000-07-16 12:04:32 +0000300 with the current locale setting we thus emulate the behavior
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000301 in the way described above.
302
303 To maintain compatibility with other platforms, not only the
304 LANG variable is tested, but a list of variables given as
305 envvars parameter. The first found to be defined will be
306 used. envvars defaults to the search path used in GNU gettext;
307 it must always contain the variable name 'LANG'.
308
309 Except for the code 'C', the language code corresponds to RFC
310 1766. code and encoding can be None in case the values cannot
311 be determined.
312
313 """
Fredrik Lundh04661322000-07-09 23:16:10 +0000314
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000315 try:
316 # check if it's supported by the _locale module
317 import _locale
318 code, encoding = _locale._getdefaultlocale()
Fredrik Lundh04661322000-07-09 23:16:10 +0000319 except (ImportError, AttributeError):
320 pass
321 else:
Fredrik Lundh663809e2000-07-10 19:32:19 +0000322 # make sure the code/encoding values are valid
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000323 if sys.platform == "win32" and code and code[:2] == "0x":
324 # map windows language identifier to language name
325 code = windows_locale.get(int(code, 0))
Fredrik Lundh663809e2000-07-10 19:32:19 +0000326 # ...add other platform-specific processing here, if
327 # necessary...
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000328 return code, encoding
Fredrik Lundh04661322000-07-09 23:16:10 +0000329
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000330 # fall back on POSIX behaviour
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000331 import os
332 lookup = os.environ.get
333 for variable in envvars:
334 localename = lookup(variable,None)
335 if localename is not None:
336 break
337 else:
338 localename = 'C'
339 return _parse_localename(localename)
340
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000341
342def getlocale(category=LC_CTYPE):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000343
344 """ Returns the current setting for the given locale category as
345 tuple (language code, encoding).
346
347 category may be one of the LC_* value except LC_ALL. It
348 defaults to LC_CTYPE.
349
350 Except for the code 'C', the language code corresponds to RFC
351 1766. code and encoding can be None in case the values cannot
352 be determined.
353
354 """
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000355 localename = _setlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000356 if category == LC_ALL and ';' in localename:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000357 raise TypeError, 'category LC_ALL is not supported'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000358 return _parse_localename(localename)
359
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000360def setlocale(category, locale=None):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000361
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000362 """ Set the locale for the given category. The locale can be
363 a string, a locale tuple (language code, encoding), or None.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000364
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000365 Locale tuples are converted to strings the locale aliasing
366 engine. Locale strings are passed directly to the C lib.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000367
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000368 category may be given as one of the LC_* values.
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000369
370 """
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000371 if locale and type(locale) is not type(""):
372 # convert to string
373 locale = normalize(_build_localename(locale))
374 return _setlocale(category, locale)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000375
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000376def resetlocale(category=LC_ALL):
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000377
378 """ Sets the locale for category to the default setting.
379
380 The default setting is determined by calling
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000381 getdefaultlocale(). category defaults to LC_ALL.
382
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000383 """
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000384 _setlocale(category, _build_localename(getdefaultlocale()))
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000385
386### Database
387#
388# The following data was extracted from the locale.alias file which
389# comes with X11 and then hand edited removing the explicit encoding
390# definitions and adding some more aliases. The file is usually
391# available as /usr/lib/X11/locale/locale.alias.
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000392#
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000393
394#
395# The encoding_alias table maps lowercase encoding alias names to C
396# locale encoding names (case-sensitive).
397#
398encoding_alias = {
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000399 '437': 'C',
400 'c': 'C',
401 'iso8859': 'ISO8859-1',
402 '8859': 'ISO8859-1',
403 '88591': 'ISO8859-1',
404 'ascii': 'ISO8859-1',
405 'en': 'ISO8859-1',
406 'iso88591': 'ISO8859-1',
407 'iso_8859-1': 'ISO8859-1',
408 '885915': 'ISO8859-15',
409 'iso885915': 'ISO8859-15',
410 'iso_8859-15': 'ISO8859-15',
411 'iso8859-2': 'ISO8859-2',
412 'iso88592': 'ISO8859-2',
413 'iso_8859-2': 'ISO8859-2',
414 'iso88595': 'ISO8859-5',
415 'iso88596': 'ISO8859-6',
416 'iso88597': 'ISO8859-7',
417 'iso88598': 'ISO8859-8',
418 'iso88599': 'ISO8859-9',
419 'iso-2022-jp': 'JIS7',
420 'jis': 'JIS7',
421 'jis7': 'JIS7',
422 'sjis': 'SJIS',
423 'tis620': 'TACTIS',
424 'ajec': 'eucJP',
425 'eucjp': 'eucJP',
426 'ujis': 'eucJP',
427 'utf-8': 'utf',
428 'utf8': 'utf',
429 'utf8@ucs4': 'utf',
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000430}
431
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000432#
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000433# The locale_alias table maps lowercase alias names to C locale names
434# (case-sensitive). Encodings are always separated from the locale
435# name using a dot ('.'); they should only be given in case the
436# language name is needed to interpret the given encoding alias
437# correctly (CJK codes often have this need).
438#
439locale_alias = {
440 'american': 'en_US.ISO8859-1',
441 'ar': 'ar_AA.ISO8859-6',
442 'ar_aa': 'ar_AA.ISO8859-6',
443 'ar_sa': 'ar_SA.ISO8859-6',
444 'arabic': 'ar_AA.ISO8859-6',
445 'bg': 'bg_BG.ISO8859-5',
446 'bg_bg': 'bg_BG.ISO8859-5',
447 'bulgarian': 'bg_BG.ISO8859-5',
448 'c-french': 'fr_CA.ISO8859-1',
449 'c': 'C',
450 'c_c': 'C',
451 'cextend': 'en_US.ISO8859-1',
452 'chinese-s': 'zh_CN.eucCN',
453 'chinese-t': 'zh_TW.eucTW',
454 'croatian': 'hr_HR.ISO8859-2',
455 'cs': 'cs_CZ.ISO8859-2',
456 'cs_cs': 'cs_CZ.ISO8859-2',
457 'cs_cz': 'cs_CZ.ISO8859-2',
458 'cz': 'cz_CZ.ISO8859-2',
459 'cz_cz': 'cz_CZ.ISO8859-2',
460 'czech': 'cs_CS.ISO8859-2',
461 'da': 'da_DK.ISO8859-1',
462 'da_dk': 'da_DK.ISO8859-1',
463 'danish': 'da_DK.ISO8859-1',
464 'de': 'de_DE.ISO8859-1',
465 'de_at': 'de_AT.ISO8859-1',
466 'de_ch': 'de_CH.ISO8859-1',
467 'de_de': 'de_DE.ISO8859-1',
468 'dutch': 'nl_BE.ISO8859-1',
469 'ee': 'ee_EE.ISO8859-4',
470 'el': 'el_GR.ISO8859-7',
471 'el_gr': 'el_GR.ISO8859-7',
472 'en': 'en_US.ISO8859-1',
473 'en_au': 'en_AU.ISO8859-1',
474 'en_ca': 'en_CA.ISO8859-1',
475 'en_gb': 'en_GB.ISO8859-1',
476 'en_ie': 'en_IE.ISO8859-1',
477 'en_nz': 'en_NZ.ISO8859-1',
478 'en_uk': 'en_GB.ISO8859-1',
479 'en_us': 'en_US.ISO8859-1',
480 'eng_gb': 'en_GB.ISO8859-1',
481 'english': 'en_EN.ISO8859-1',
482 'english_uk': 'en_GB.ISO8859-1',
483 'english_united-states': 'en_US.ISO8859-1',
484 'english_us': 'en_US.ISO8859-1',
485 'es': 'es_ES.ISO8859-1',
486 'es_ar': 'es_AR.ISO8859-1',
487 'es_bo': 'es_BO.ISO8859-1',
488 'es_cl': 'es_CL.ISO8859-1',
489 'es_co': 'es_CO.ISO8859-1',
490 'es_cr': 'es_CR.ISO8859-1',
491 'es_ec': 'es_EC.ISO8859-1',
492 'es_es': 'es_ES.ISO8859-1',
493 'es_gt': 'es_GT.ISO8859-1',
494 'es_mx': 'es_MX.ISO8859-1',
495 'es_ni': 'es_NI.ISO8859-1',
496 'es_pa': 'es_PA.ISO8859-1',
497 'es_pe': 'es_PE.ISO8859-1',
498 'es_py': 'es_PY.ISO8859-1',
499 'es_sv': 'es_SV.ISO8859-1',
500 'es_uy': 'es_UY.ISO8859-1',
501 'es_ve': 'es_VE.ISO8859-1',
502 'et': 'et_EE.ISO8859-4',
503 'et_ee': 'et_EE.ISO8859-4',
504 'fi': 'fi_FI.ISO8859-1',
505 'fi_fi': 'fi_FI.ISO8859-1',
506 'finnish': 'fi_FI.ISO8859-1',
507 'fr': 'fr_FR.ISO8859-1',
508 'fr_be': 'fr_BE.ISO8859-1',
509 'fr_ca': 'fr_CA.ISO8859-1',
510 'fr_ch': 'fr_CH.ISO8859-1',
511 'fr_fr': 'fr_FR.ISO8859-1',
512 'fre_fr': 'fr_FR.ISO8859-1',
513 'french': 'fr_FR.ISO8859-1',
514 'french_france': 'fr_FR.ISO8859-1',
515 'ger_de': 'de_DE.ISO8859-1',
516 'german': 'de_DE.ISO8859-1',
517 'german_germany': 'de_DE.ISO8859-1',
518 'greek': 'el_GR.ISO8859-7',
519 'hebrew': 'iw_IL.ISO8859-8',
520 'hr': 'hr_HR.ISO8859-2',
521 'hr_hr': 'hr_HR.ISO8859-2',
522 'hu': 'hu_HU.ISO8859-2',
523 'hu_hu': 'hu_HU.ISO8859-2',
524 'hungarian': 'hu_HU.ISO8859-2',
525 'icelandic': 'is_IS.ISO8859-1',
526 'id': 'id_ID.ISO8859-1',
527 'id_id': 'id_ID.ISO8859-1',
528 'is': 'is_IS.ISO8859-1',
529 'is_is': 'is_IS.ISO8859-1',
530 'iso-8859-1': 'en_US.ISO8859-1',
531 'iso-8859-15': 'en_US.ISO8859-15',
532 'iso8859-1': 'en_US.ISO8859-1',
533 'iso8859-15': 'en_US.ISO8859-15',
534 'iso_8859_1': 'en_US.ISO8859-1',
535 'iso_8859_15': 'en_US.ISO8859-15',
536 'it': 'it_IT.ISO8859-1',
537 'it_ch': 'it_CH.ISO8859-1',
538 'it_it': 'it_IT.ISO8859-1',
539 'italian': 'it_IT.ISO8859-1',
540 'iw': 'iw_IL.ISO8859-8',
541 'iw_il': 'iw_IL.ISO8859-8',
542 'ja': 'ja_JP.eucJP',
543 'ja.jis': 'ja_JP.JIS7',
544 'ja.sjis': 'ja_JP.SJIS',
545 'ja_jp': 'ja_JP.eucJP',
546 'ja_jp.ajec': 'ja_JP.eucJP',
547 'ja_jp.euc': 'ja_JP.eucJP',
548 'ja_jp.eucjp': 'ja_JP.eucJP',
549 'ja_jp.iso-2022-jp': 'ja_JP.JIS7',
550 'ja_jp.jis': 'ja_JP.JIS7',
551 'ja_jp.jis7': 'ja_JP.JIS7',
552 'ja_jp.mscode': 'ja_JP.SJIS',
553 'ja_jp.sjis': 'ja_JP.SJIS',
554 'ja_jp.ujis': 'ja_JP.eucJP',
555 'japan': 'ja_JP.eucJP',
556 'japanese': 'ja_JP.SJIS',
557 'japanese-euc': 'ja_JP.eucJP',
558 'japanese.euc': 'ja_JP.eucJP',
559 'jp_jp': 'ja_JP.eucJP',
560 'ko': 'ko_KR.eucKR',
561 'ko_kr': 'ko_KR.eucKR',
562 'ko_kr.euc': 'ko_KR.eucKR',
563 'korean': 'ko_KR.eucKR',
564 'lt': 'lt_LT.ISO8859-4',
565 'lv': 'lv_LV.ISO8859-4',
566 'mk': 'mk_MK.ISO8859-5',
567 'mk_mk': 'mk_MK.ISO8859-5',
568 'nl': 'nl_NL.ISO8859-1',
569 'nl_be': 'nl_BE.ISO8859-1',
570 'nl_nl': 'nl_NL.ISO8859-1',
571 'no': 'no_NO.ISO8859-1',
572 'no_no': 'no_NO.ISO8859-1',
573 'norwegian': 'no_NO.ISO8859-1',
574 'pl': 'pl_PL.ISO8859-2',
575 'pl_pl': 'pl_PL.ISO8859-2',
576 'polish': 'pl_PL.ISO8859-2',
577 'portuguese': 'pt_PT.ISO8859-1',
578 'portuguese_brazil': 'pt_BR.ISO8859-1',
579 'posix': 'C',
580 'posix-utf2': 'C',
581 'pt': 'pt_PT.ISO8859-1',
582 'pt_br': 'pt_BR.ISO8859-1',
583 'pt_pt': 'pt_PT.ISO8859-1',
584 'ro': 'ro_RO.ISO8859-2',
585 'ro_ro': 'ro_RO.ISO8859-2',
586 'ru': 'ru_RU.ISO8859-5',
587 'ru_ru': 'ru_RU.ISO8859-5',
588 'rumanian': 'ro_RO.ISO8859-2',
589 'russian': 'ru_RU.ISO8859-5',
590 'serbocroatian': 'sh_YU.ISO8859-2',
591 'sh': 'sh_YU.ISO8859-2',
592 'sh_hr': 'sh_HR.ISO8859-2',
593 'sh_sp': 'sh_YU.ISO8859-2',
594 'sh_yu': 'sh_YU.ISO8859-2',
595 'sk': 'sk_SK.ISO8859-2',
596 'sk_sk': 'sk_SK.ISO8859-2',
597 'sl': 'sl_CS.ISO8859-2',
598 'sl_cs': 'sl_CS.ISO8859-2',
599 'sl_si': 'sl_SI.ISO8859-2',
600 'slovak': 'sk_SK.ISO8859-2',
601 'slovene': 'sl_CS.ISO8859-2',
602 'sp': 'sp_YU.ISO8859-5',
603 'sp_yu': 'sp_YU.ISO8859-5',
604 'spanish': 'es_ES.ISO8859-1',
605 'spanish_spain': 'es_ES.ISO8859-1',
606 'sr_sp': 'sr_SP.ISO8859-2',
607 'sv': 'sv_SE.ISO8859-1',
608 'sv_se': 'sv_SE.ISO8859-1',
609 'swedish': 'sv_SE.ISO8859-1',
610 'th_th': 'th_TH.TACTIS',
611 'tr': 'tr_TR.ISO8859-9',
612 'tr_tr': 'tr_TR.ISO8859-9',
613 'turkish': 'tr_TR.ISO8859-9',
614 'univ': 'en_US.utf',
615 'universal': 'en_US.utf',
616 'zh': 'zh_CN.eucCN',
617 'zh_cn': 'zh_CN.eucCN',
618 'zh_cn.big5': 'zh_TW.eucTW',
619 'zh_cn.euc': 'zh_CN.eucCN',
620 'zh_tw': 'zh_TW.eucTW',
621 'zh_tw.euc': 'zh_TW.eucTW',
622}
623
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000624#
625# this maps windows language identifiers (as used on Windows 95 and
626# earlier) to locale strings.
627#
628# NOTE: this mapping is incomplete. If your language is missing, send
629# a note with the missing language identifier and the suggested locale
630# code to Fredrik Lundh <effbot@telia.com>. Thanks /F
631
632windows_locale = {
633 0x0404: "zh_TW", # Chinese (Taiwan)
634 0x0804: "zh_CN", # Chinese (PRC)
635 0x0406: "da_DK", # Danish
636 0x0413: "nl_NL", # Dutch (Netherlands)
637 0x0409: "en_US", # English (United States)
638 0x0809: "en_UK", # English (United Kingdom)
639 0x0c09: "en_AU", # English (Australian)
640 0x1009: "en_CA", # English (Canadian)
641 0x1409: "en_NZ", # English (New Zealand)
642 0x1809: "en_IE", # English (Ireland)
643 0x1c09: "en_ZA", # English (South Africa)
644 0x040b: "fi_FI", # Finnish
645 0x040c: "fr_FR", # French (Standard)
646 0x080c: "fr_BE", # French (Belgian)
647 0x0c0c: "fr_CA", # French (Canadian)
648 0x100c: "fr_CH", # French (Switzerland)
649 0x0407: "de_DE", # German (Standard)
650 0x0408: "el_GR", # Greek
651 0x040d: "iw_IL", # Hebrew
652 0x040f: "is_IS", # Icelandic
653 0x0410: "it_IT", # Italian (Standard)
654 0x0411: "ja_JA", # Japanese
655 0x0414: "no_NO", # Norwegian (Bokmal)
656 0x0816: "pt_PT", # Portuguese (Standard)
657 0x0c0a: "es_ES", # Spanish (Modern Sort)
658 0x0441: "sw_KE", # Swahili (Kenya)
659 0x041d: "sv_SE", # Swedish
660 0x081d: "sv_FI", # Swedish (Finland)
661 0x041f: "tr_TR", # Turkish
662}
663
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000664def _print_locale():
665
666 """ Test function.
667 """
668 categories = {}
669 def _init_categories(categories=categories):
670 for k,v in globals().items():
671 if k[:3] == 'LC_':
672 categories[k] = v
673 _init_categories()
674 del categories['LC_ALL']
675
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000676 print 'Locale defaults as determined by getdefaultlocale():'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000677 print '-'*72
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000678 lang, enc = getdefaultlocale()
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000679 print 'Language: ', lang or '(undefined)'
680 print 'Encoding: ', enc or '(undefined)'
681 print
682
683 print 'Locale settings on startup:'
684 print '-'*72
685 for name,category in categories.items():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000686 print name, '...'
687 lang, enc = getlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000688 print ' Language: ', lang or '(undefined)'
689 print ' Encoding: ', enc or '(undefined)'
690 print
691
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000692 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000693 print 'Locale settings after calling resetlocale():'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000694 print '-'*72
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000695 resetlocale()
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000696 for name,category in categories.items():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000697 print name, '...'
698 lang, enc = getlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000699 print ' Language: ', lang or '(undefined)'
700 print ' Encoding: ', enc or '(undefined)'
701 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000702
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000703 try:
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000704 setlocale(LC_ALL, "")
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000705 except:
706 print 'NOTE:'
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000707 print 'setlocale(LC_ALL, "") does not support the default locale'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000708 print 'given in the OS environment variables.'
709 else:
710 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000711 print 'Locale settings after calling setlocale(LC_ALL, ""):'
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000712 print '-'*72
713 for name,category in categories.items():
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000714 print name, '...'
715 lang, enc = getlocale(category)
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000716 print ' Language: ', lang or '(undefined)'
717 print ' Encoding: ', enc or '(undefined)'
718 print
Fredrik Lundh6c86b992000-07-09 17:12:58 +0000719
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000720###
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000721
Tim Peters1baf8292001-01-24 10:13:46 +0000722try:
723 LC_MESSAGES
724except:
725 pass
726else:
727 __all__.append("LC_MESSAGES")
728
Guido van Rossumeef1d4e1997-11-19 19:01:43 +0000729if __name__=='__main__':
Marc-André Lemburg5431bc32000-06-07 09:11:40 +0000730 print 'Locale aliasing:'
731 print
732 _print_locale()
733 print
734 print 'Number formatting:'
735 print
736 _test()