0001 """ Locale support. 0002 0003 The module provides low-level access to the C lib's locale APIs 0004 and adds high level number formatting APIs as well as a locale 0005 aliasing engine to complement these. 0006 0007 The aliasing engine includes support for many commonly used locale 0008 names and maps them to values suitable for passing to the C lib's 0009 setlocale() function. It also includes default encodings for all 0010 supported locale names. 0011 0012 """ 0013 0014 import sys 0015 0016 # Try importing the _locale module. 0017 # 0018 # If this fails, fall back on a basic 'C' locale emulation. 0019 0020 # Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before 0021 # trying the import. So __all__ is also fiddled at the end of the file. 0022 __all__ = ["setlocale","Error","localeconv","strcoll","strxfrm", 0023 "format","str","atof","atoi","LC_CTYPE","LC_COLLATE", 0024 "LC_TIME","LC_MONETARY","LC_NUMERIC", "LC_ALL","CHAR_MAX"] 0025 0026 try: 0027 0028 from _locale import * 0029 0030 except ImportError: 0031 0032 # Locale emulation 0033 0034 CHAR_MAX = 127 0035 LC_ALL = 6 0036 LC_COLLATE = 3 0037 LC_CTYPE = 0 0038 LC_MESSAGES = 5 0039 LC_MONETARY = 4 0040 LC_NUMERIC = 1 0041 LC_TIME = 2 0042 Error = ValueError 0043 0044 def localeconv(): 0045 """ localeconv() -> dict. 0046 Returns numeric and monetary locale-specific parameters. 0047 """ 0048 # 'C' locale default values 0049 return {'grouping': [127], 0050 'currency_symbol': '', 0051 'n_sign_posn': 127, 0052 'p_cs_precedes': 127, 0053 'n_cs_precedes': 127, 0054 'mon_grouping': [], 0055 'n_sep_by_space': 127, 0056 'decimal_point': '.', 0057 'negative_sign': '', 0058 'positive_sign': '', 0059 'p_sep_by_space': 127, 0060 'int_curr_symbol': '', 0061 'p_sign_posn': 127, 0062 'thousands_sep': '', 0063 'mon_thousands_sep': '', 0064 'frac_digits': 127, 0065 'mon_decimal_point': '', 0066 'int_frac_digits': 127} 0067 0068 def setlocale(category, value=None): 0069 """ setlocale(integer,string=None) -> string. 0070 Activates/queries locale processing. 0071 """ 0072 if value not in (None, '', 'C'): 0073 raise Error, '_locale emulation only supports "C" locale' 0074 return 'C' 0075 0076 def strcoll(a,b): 0077 """ strcoll(string,string) -> int. 0078 Compares two strings according to the locale. 0079 """ 0080 return cmp(a,b) 0081 0082 def strxfrm(s): 0083 """ strxfrm(string) -> string. 0084 Returns a string that behaves for cmp locale-aware. 0085 """ 0086 return s 0087 0088 ### Number formatting APIs 0089 0090 # Author: Martin von Loewis 0091 0092 #perform the grouping from right to left 0093 def _group(s): 0094 conv=localeconv() 0095 grouping=conv['grouping'] 0096 if not grouping:return (s, 0) 0097 result="" 0098 seps = 0 0099 spaces = "" 0100 if s[-1] == ' ': 0101 sp = s.find(' ') 0102 spaces = s[sp:] 0103 s = s[:sp] 0104 while s and grouping: 0105 # if grouping is -1, we are done 0106 if grouping[0]==CHAR_MAX: 0107 break 0108 # 0: re-use last group ad infinitum 0109 elif grouping[0]!=0: 0110 #process last group 0111 group=grouping[0] 0112 grouping=grouping[1:] 0113 if result: 0114 result=s[-group:]+conv['thousands_sep']+result 0115 seps += 1 0116 else: 0117 result=s[-group:] 0118 s=s[:-group] 0119 if s and s[-1] not in "0123456789": 0120 # the leading string is only spaces and signs 0121 return s+result+spaces,seps 0122 if not result: 0123 return s+spaces,seps 0124 if s: 0125 result=s+conv['thousands_sep']+result 0126 seps += 1 0127 return result+spaces,seps 0128 0129 def format(f,val,grouping=0): 0130 """Formats a value in the same way that the % formatting would use, 0131 but takes the current locale into account. 0132 Grouping is applied if the third parameter is true.""" 0133 result = f % val 0134 fields = result.split(".") 0135 seps = 0 0136 if grouping: 0137 fields[0],seps=_group(fields[0]) 0138 if len(fields)==2: 0139 result = fields[0]+localeconv()['decimal_point']+fields[1] 0140 elif len(fields)==1: 0141 result = fields[0] 0142 else: 0143 raise Error, "Too many decimal points in result string" 0144 0145 while seps: 0146 # If the number was formatted for a specific width, then it 0147 # might have been filled with spaces to the left or right. If 0148 # so, kill as much spaces as there where separators. 0149 # Leading zeroes as fillers are not yet dealt with, as it is 0150 # not clear how they should interact with grouping. 0151 sp = result.find(" ") 0152 if sp==-1:break 0153 result = result[:sp]+result[sp+1:] 0154 seps -= 1 0155 0156 return result 0157 0158 def str(val): 0159 """Convert float to integer, taking the locale into account.""" 0160 return format("%.12g",val) 0161 0162 def atof(string,func=float): 0163 "Parses a string as a float according to the locale settings." 0164 #First, get rid of the grouping 0165 ts = localeconv()['thousands_sep'] 0166 if ts: 0167 string = string.replace(ts, '') 0168 #next, replace the decimal point with a dot 0169 dd = localeconv()['decimal_point'] 0170 if dd: 0171 string = string.replace(dd, '.') 0172 #finally, parse the string 0173 return func(string) 0174 0175 def atoi(str): 0176 "Converts a string to an integer according to the locale settings." 0177 return atof(str, int) 0178 0179 def _test(): 0180 setlocale(LC_ALL, "") 0181 #do grouping 0182 s1=format("%d", 123456789,1) 0183 print s1, "is", atoi(s1) 0184 #standard formatting 0185 s1=str(3.14) 0186 print s1, "is", atof(s1) 0187 0188 ### Locale name aliasing engine 0189 0190 # Author: Marc-Andre Lemburg, mal@lemburg.com 0191 # Various tweaks by Fredrik Lundh <fredrik@pythonware.com> 0192 0193 # store away the low-level version of setlocale (it's 0194 # overridden below) 0195 _setlocale = setlocale 0196 0197 def normalize(localename): 0198 0199 """ Returns a normalized locale code for the given locale 0200 name. 0201 0202 The returned locale code is formatted for use with 0203 setlocale(). 0204 0205 If normalization fails, the original name is returned 0206 unchanged. 0207 0208 If the given encoding is not known, the function defaults to 0209 the default encoding for the locale code just like setlocale() 0210 does. 0211 0212 """ 0213 # Normalize the locale name and extract the encoding 0214 fullname = localename.lower() 0215 if ':' in fullname: 0216 # ':' is sometimes used as encoding delimiter. 0217 fullname = fullname.replace(':', '.') 0218 if '.' in fullname: 0219 langname, encoding = fullname.split('.')[:2] 0220 fullname = langname + '.' + encoding 0221 else: 0222 langname = fullname 0223 encoding = '' 0224 0225 # First lookup: fullname (possibly with encoding) 0226 code = locale_alias.get(fullname, None) 0227 if code is not None: 0228 return code 0229 0230 # Second try: langname (without encoding) 0231 code = locale_alias.get(langname, None) 0232 if code is not None: 0233 if '.' in code: 0234 langname, defenc = code.split('.') 0235 else: 0236 langname = code 0237 defenc = '' 0238 if encoding: 0239 encoding = encoding_alias.get(encoding, encoding) 0240 else: 0241 encoding = defenc 0242 if encoding: 0243 return langname + '.' + encoding 0244 else: 0245 return langname 0246 0247 else: 0248 return localename 0249 0250 def _parse_localename(localename): 0251 0252 """ Parses the locale code for localename and returns the 0253 result as tuple (language code, encoding). 0254 0255 The localename is normalized and passed through the locale 0256 alias engine. A ValueError is raised in case the locale name 0257 cannot be parsed. 0258 0259 The language code corresponds to RFC 1766. code and encoding 0260 can be None in case the values cannot be determined or are 0261 unknown to this implementation. 0262 0263 """ 0264 code = normalize(localename) 0265 if '@' in localename: 0266 # Deal with locale modifiers 0267 code, modifier = code.split('@') 0268 if modifier == 'euro' and '.' not in code: 0269 # Assume Latin-9 for @euro locales. This is bogus, 0270 # since some systems may use other encodings for these 0271 # locales. Also, we ignore other modifiers. 0272 return code, 'iso-8859-15' 0273 0274 if '.' in code: 0275 return code.split('.')[:2] 0276 elif code == 'C': 0277 return None, None 0278 raise ValueError, 'unknown locale: %s' % localename 0279 0280 def _build_localename(localetuple): 0281 0282 """ Builds a locale code from the given tuple (language code, 0283 encoding). 0284 0285 No aliasing or normalizing takes place. 0286 0287 """ 0288 language, encoding = localetuple 0289 if language is None: 0290 language = 'C' 0291 if encoding is None: 0292 return language 0293 else: 0294 return language + '.' + encoding 0295 0296 def getdefaultlocale(envvars=('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG')): 0297 0298 """ Tries to determine the default locale settings and returns 0299 them as tuple (language code, encoding). 0300 0301 According to POSIX, a program which has not called 0302 setlocale(LC_ALL, "") runs using the portable 'C' locale. 0303 Calling setlocale(LC_ALL, "") lets it use the default locale as 0304 defined by the LANG variable. Since we don't want to interfere 0305 with the current locale setting we thus emulate the behavior 0306 in the way described above. 0307 0308 To maintain compatibility with other platforms, not only the 0309 LANG variable is tested, but a list of variables given as 0310 envvars parameter. The first found to be defined will be 0311 used. envvars defaults to the search path used in GNU gettext; 0312 it must always contain the variable name 'LANG'. 0313 0314 Except for the code 'C', the language code corresponds to RFC 0315 1766. code and encoding can be None in case the values cannot 0316 be determined. 0317 0318 """ 0319 0320 try: 0321 # check if it's supported by the _locale module 0322 import _locale 0323 code, encoding = _locale._getdefaultlocale() 0324 except (ImportError, AttributeError): 0325 pass 0326 else: 0327 # make sure the code/encoding values are valid 0328 if sys.platform == "win32" and code and code[:2] == "0x": 0329 # map windows language identifier to language name 0330 code = windows_locale.get(int(code, 0)) 0331 # ...add other platform-specific processing here, if 0332 # necessary... 0333 return code, encoding 0334 0335 # fall back on POSIX behaviour 0336 import os 0337 lookup = os.environ.get 0338 for variable in envvars: 0339 localename = lookup(variable,None) 0340 if localename: 0341 break 0342 else: 0343 localename = 'C' 0344 return _parse_localename(localename) 0345 0346 0347 def getlocale(category=LC_CTYPE): 0348 0349 """ Returns the current setting for the given locale category as 0350 tuple (language code, encoding). 0351 0352 category may be one of the LC_* value except LC_ALL. It 0353 defaults to LC_CTYPE. 0354 0355 Except for the code 'C', the language code corresponds to RFC 0356 1766. code and encoding can be None in case the values cannot 0357 be determined. 0358 0359 """ 0360 localename = _setlocale(category) 0361 if category == LC_ALL and ';' in localename: 0362 raise TypeError, 'category LC_ALL is not supported' 0363 return _parse_localename(localename) 0364 0365 def setlocale(category, locale=None): 0366 0367 """ Set the locale for the given category. The locale can be 0368 a string, a locale tuple (language code, encoding), or None. 0369 0370 Locale tuples are converted to strings the locale aliasing 0371 engine. Locale strings are passed directly to the C lib. 0372 0373 category may be given as one of the LC_* values. 0374 0375 """ 0376 if locale and type(locale) is not type(""): 0377 # convert to string 0378 locale = normalize(_build_localename(locale)) 0379 return _setlocale(category, locale) 0380 0381 def resetlocale(category=LC_ALL): 0382 0383 """ Sets the locale for category to the default setting. 0384 0385 The default setting is determined by calling 0386 getdefaultlocale(). category defaults to LC_ALL. 0387 0388 """ 0389 _setlocale(category, _build_localename(getdefaultlocale())) 0390 0391 if sys.platform in ('win32', 'darwin', 'mac'): 0392 # On Win32, this will return the ANSI code page 0393 # On the Mac, it should return the system encoding; 0394 # it might return "ascii" instead 0395 def getpreferredencoding(do_setlocale = True): 0396 """Return the charset that the user is likely using.""" 0397 import _locale 0398 return _locale._getdefaultlocale()[1] 0399 else: 0400 # On Unix, if CODESET is available, use that. 0401 try: 0402 CODESET 0403 except NameError: 0404 # Fall back to parsing environment variables :-( 0405 def getpreferredencoding(do_setlocale = True): 0406 """Return the charset that the user is likely using, 0407 by looking at environment variables.""" 0408 return getdefaultlocale()[1] 0409 else: 0410 def getpreferredencoding(do_setlocale = True): 0411 """Return the charset that the user is likely using, 0412 according to the system configuration.""" 0413 if do_setlocale: 0414 oldloc = setlocale(LC_CTYPE) 0415 setlocale(LC_CTYPE, "") 0416 result = nl_langinfo(CODESET) 0417 setlocale(LC_CTYPE, oldloc) 0418 return result 0419 else: 0420 return nl_langinfo(CODESET) 0421 0422 0423 ### Database 0424 # 0425 # The following data was extracted from the locale.alias file which 0426 # comes with X11 and then hand edited removing the explicit encoding 0427 # definitions and adding some more aliases. The file is usually 0428 # available as /usr/lib/X11/locale/locale.alias. 0429 # 0430 0431 # 0432 # The encoding_alias table maps lowercase encoding alias names to C 0433 # locale encoding names (case-sensitive). 0434 # 0435 encoding_alias = { 0436 '437': 'C', 0437 'c': 'C', 0438 'iso8859': 'ISO8859-1', 0439 '8859': 'ISO8859-1', 0440 '88591': 'ISO8859-1', 0441 'ascii': 'ISO8859-1', 0442 'en': 'ISO8859-1', 0443 'iso88591': 'ISO8859-1', 0444 'iso_8859-1': 'ISO8859-1', 0445 '885915': 'ISO8859-15', 0446 'iso885915': 'ISO8859-15', 0447 'iso_8859-15': 'ISO8859-15', 0448 'iso8859-2': 'ISO8859-2', 0449 'iso88592': 'ISO8859-2', 0450 'iso_8859-2': 'ISO8859-2', 0451 'iso88595': 'ISO8859-5', 0452 'iso88596': 'ISO8859-6', 0453 'iso88597': 'ISO8859-7', 0454 'iso88598': 'ISO8859-8', 0455 'iso88599': 'ISO8859-9', 0456 'iso-2022-jp': 'JIS7', 0457 'jis': 'JIS7', 0458 'jis7': 'JIS7', 0459 'sjis': 'SJIS', 0460 'tis620': 'TACTIS', 0461 'ajec': 'eucJP', 0462 'eucjp': 'eucJP', 0463 'ujis': 'eucJP', 0464 'utf-8': 'utf', 0465 'utf8': 'utf', 0466 'utf8@ucs4': 'utf', 0467 } 0468 0469 # 0470 # The locale_alias table maps lowercase alias names to C locale names 0471 # (case-sensitive). Encodings are always separated from the locale 0472 # name using a dot ('.'); they should only be given in case the 0473 # language name is needed to interpret the given encoding alias 0474 # correctly (CJK codes often have this need). 0475 # 0476 locale_alias = { 0477 'american': 'en_US.ISO8859-1', 0478 'ar': 'ar_AA.ISO8859-6', 0479 'ar_aa': 'ar_AA.ISO8859-6', 0480 'ar_sa': 'ar_SA.ISO8859-6', 0481 'arabic': 'ar_AA.ISO8859-6', 0482 'bg': 'bg_BG.ISO8859-5', 0483 'bg_bg': 'bg_BG.ISO8859-5', 0484 'bulgarian': 'bg_BG.ISO8859-5', 0485 'c-french': 'fr_CA.ISO8859-1', 0486 'c': 'C', 0487 'c_c': 'C', 0488 'cextend': 'en_US.ISO8859-1', 0489 'chinese-s': 'zh_CN.eucCN', 0490 'chinese-t': 'zh_TW.eucTW', 0491 'croatian': 'hr_HR.ISO8859-2', 0492 'cs': 'cs_CZ.ISO8859-2', 0493 'cs_cs': 'cs_CZ.ISO8859-2', 0494 'cs_cz': 'cs_CZ.ISO8859-2', 0495 'cz': 'cz_CZ.ISO8859-2', 0496 'cz_cz': 'cz_CZ.ISO8859-2', 0497 'czech': 'cs_CS.ISO8859-2', 0498 'da': 'da_DK.ISO8859-1', 0499 'da_dk': 'da_DK.ISO8859-1', 0500 'danish': 'da_DK.ISO8859-1', 0501 'de': 'de_DE.ISO8859-1', 0502 'de_at': 'de_AT.ISO8859-1', 0503 'de_ch': 'de_CH.ISO8859-1', 0504 'de_de': 'de_DE.ISO8859-1', 0505 'dutch': 'nl_BE.ISO8859-1', 0506 'ee': 'ee_EE.ISO8859-4', 0507 'el': 'el_GR.ISO8859-7', 0508 'el_gr': 'el_GR.ISO8859-7', 0509 'en': 'en_US.ISO8859-1', 0510 'en_au': 'en_AU.ISO8859-1', 0511 'en_ca': 'en_CA.ISO8859-1', 0512 'en_gb': 'en_GB.ISO8859-1', 0513 'en_ie': 'en_IE.ISO8859-1', 0514 'en_nz': 'en_NZ.ISO8859-1', 0515 'en_uk': 'en_GB.ISO8859-1', 0516 'en_us': 'en_US.ISO8859-1', 0517 'eng_gb': 'en_GB.ISO8859-1', 0518 'english': 'en_EN.ISO8859-1', 0519 'english_uk': 'en_GB.ISO8859-1', 0520 'english_united-states': 'en_US.ISO8859-1', 0521 'english_us': 'en_US.ISO8859-1', 0522 'es': 'es_ES.ISO8859-1', 0523 'es_ar': 'es_AR.ISO8859-1', 0524 'es_bo': 'es_BO.ISO8859-1', 0525 'es_cl': 'es_CL.ISO8859-1', 0526 'es_co': 'es_CO.ISO8859-1', 0527 'es_cr': 'es_CR.ISO8859-1', 0528 'es_ec': 'es_EC.ISO8859-1', 0529 'es_es': 'es_ES.ISO8859-1', 0530 'es_gt': 'es_GT.ISO8859-1', 0531 'es_mx': 'es_MX.ISO8859-1', 0532 'es_ni': 'es_NI.ISO8859-1', 0533 'es_pa': 'es_PA.ISO8859-1', 0534 'es_pe': 'es_PE.ISO8859-1', 0535 'es_py': 'es_PY.ISO8859-1', 0536 'es_sv': 'es_SV.ISO8859-1', 0537 'es_uy': 'es_UY.ISO8859-1', 0538 'es_ve': 'es_VE.ISO8859-1', 0539 'et': 'et_EE.ISO8859-4', 0540 'et_ee': 'et_EE.ISO8859-4', 0541 'fi': 'fi_FI.ISO8859-1', 0542 'fi_fi': 'fi_FI.ISO8859-1', 0543 'finnish': 'fi_FI.ISO8859-1', 0544 'fr': 'fr_FR.ISO8859-1', 0545 'fr_be': 'fr_BE.ISO8859-1', 0546 'fr_ca': 'fr_CA.ISO8859-1', 0547 'fr_ch': 'fr_CH.ISO8859-1', 0548 'fr_fr': 'fr_FR.ISO8859-1', 0549 'fre_fr': 'fr_FR.ISO8859-1', 0550 'french': 'fr_FR.ISO8859-1', 0551 'french_france': 'fr_FR.ISO8859-1', 0552 'ger_de': 'de_DE.ISO8859-1', 0553 'german': 'de_DE.ISO8859-1', 0554 'german_germany': 'de_DE.ISO8859-1', 0555 'greek': 'el_GR.ISO8859-7', 0556 'hebrew': 'iw_IL.ISO8859-8', 0557 'hr': 'hr_HR.ISO8859-2', 0558 'hr_hr': 'hr_HR.ISO8859-2', 0559 'hu': 'hu_HU.ISO8859-2', 0560 'hu_hu': 'hu_HU.ISO8859-2', 0561 'hungarian': 'hu_HU.ISO8859-2', 0562 'icelandic': 'is_IS.ISO8859-1', 0563 'id': 'id_ID.ISO8859-1', 0564 'id_id': 'id_ID.ISO8859-1', 0565 'is': 'is_IS.ISO8859-1', 0566 'is_is': 'is_IS.ISO8859-1', 0567 'iso-8859-1': 'en_US.ISO8859-1', 0568 'iso-8859-15': 'en_US.ISO8859-15', 0569 'iso8859-1': 'en_US.ISO8859-1', 0570 'iso8859-15': 'en_US.ISO8859-15', 0571 'iso_8859_1': 'en_US.ISO8859-1', 0572 'iso_8859_15': 'en_US.ISO8859-15', 0573 'it': 'it_IT.ISO8859-1', 0574 'it_ch': 'it_CH.ISO8859-1', 0575 'it_it': 'it_IT.ISO8859-1', 0576 'italian': 'it_IT.ISO8859-1', 0577 'iw': 'iw_IL.ISO8859-8', 0578 'iw_il': 'iw_IL.ISO8859-8', 0579 'ja': 'ja_JP.eucJP', 0580 'ja.jis': 'ja_JP.JIS7', 0581 'ja.sjis': 'ja_JP.SJIS', 0582 'ja_jp': 'ja_JP.eucJP', 0583 'ja_jp.ajec': 'ja_JP.eucJP', 0584 'ja_jp.euc': 'ja_JP.eucJP', 0585 'ja_jp.eucjp': 'ja_JP.eucJP', 0586 'ja_jp.iso-2022-jp': 'ja_JP.JIS7', 0587 'ja_jp.jis': 'ja_JP.JIS7', 0588 'ja_jp.jis7': 'ja_JP.JIS7', 0589 'ja_jp.mscode': 'ja_JP.SJIS', 0590 'ja_jp.sjis': 'ja_JP.SJIS', 0591 'ja_jp.ujis': 'ja_JP.eucJP', 0592 'japan': 'ja_JP.eucJP', 0593 'japanese': 'ja_JP.SJIS', 0594 'japanese-euc': 'ja_JP.eucJP', 0595 'japanese.euc': 'ja_JP.eucJP', 0596 'jp_jp': 'ja_JP.eucJP', 0597 'ko': 'ko_KR.eucKR', 0598 'ko_kr': 'ko_KR.eucKR', 0599 'ko_kr.euc': 'ko_KR.eucKR', 0600 'korean': 'ko_KR.eucKR', 0601 'lt': 'lt_LT.ISO8859-4', 0602 'lv': 'lv_LV.ISO8859-4', 0603 'mk': 'mk_MK.ISO8859-5', 0604 'mk_mk': 'mk_MK.ISO8859-5', 0605 'nl': 'nl_NL.ISO8859-1', 0606 'nl_be': 'nl_BE.ISO8859-1', 0607 'nl_nl': 'nl_NL.ISO8859-1', 0608 'no': 'no_NO.ISO8859-1', 0609 'no_no': 'no_NO.ISO8859-1', 0610 'norwegian': 'no_NO.ISO8859-1', 0611 'pl': 'pl_PL.ISO8859-2', 0612 'pl_pl': 'pl_PL.ISO8859-2', 0613 'polish': 'pl_PL.ISO8859-2', 0614 'portuguese': 'pt_PT.ISO8859-1', 0615 'portuguese_brazil': 'pt_BR.ISO8859-1', 0616 'posix': 'C', 0617 'posix-utf2': 'C', 0618 'pt': 'pt_PT.ISO8859-1', 0619 'pt_br': 'pt_BR.ISO8859-1', 0620 'pt_pt': 'pt_PT.ISO8859-1', 0621 'ro': 'ro_RO.ISO8859-2', 0622 'ro_ro': 'ro_RO.ISO8859-2', 0623 'ru': 'ru_RU.ISO8859-5', 0624 'ru_ru': 'ru_RU.ISO8859-5', 0625 'rumanian': 'ro_RO.ISO8859-2', 0626 'russian': 'ru_RU.ISO8859-5', 0627 'serbocroatian': 'sh_YU.ISO8859-2', 0628 'sh': 'sh_YU.ISO8859-2', 0629 'sh_hr': 'sh_HR.ISO8859-2', 0630 'sh_sp': 'sh_YU.ISO8859-2', 0631 'sh_yu': 'sh_YU.ISO8859-2', 0632 'sk': 'sk_SK.ISO8859-2', 0633 'sk_sk': 'sk_SK.ISO8859-2', 0634 'sl': 'sl_CS.ISO8859-2', 0635 'sl_cs': 'sl_CS.ISO8859-2', 0636 'sl_si': 'sl_SI.ISO8859-2', 0637 'slovak': 'sk_SK.ISO8859-2', 0638 'slovene': 'sl_CS.ISO8859-2', 0639 'sp': 'sp_YU.ISO8859-5', 0640 'sp_yu': 'sp_YU.ISO8859-5', 0641 'spanish': 'es_ES.ISO8859-1', 0642 'spanish_spain': 'es_ES.ISO8859-1', 0643 'sr_sp': 'sr_SP.ISO8859-2', 0644 'sv': 'sv_SE.ISO8859-1', 0645 'sv_se': 'sv_SE.ISO8859-1', 0646 'swedish': 'sv_SE.ISO8859-1', 0647 'th_th': 'th_TH.TACTIS', 0648 'tr': 'tr_TR.ISO8859-9', 0649 'tr_tr': 'tr_TR.ISO8859-9', 0650 'turkish': 'tr_TR.ISO8859-9', 0651 'univ': 'en_US.utf', 0652 'universal': 'en_US.utf', 0653 'zh': 'zh_CN.eucCN', 0654 'zh_cn': 'zh_CN.eucCN', 0655 'zh_cn.big5': 'zh_TW.eucTW', 0656 'zh_cn.euc': 'zh_CN.eucCN', 0657 'zh_tw': 'zh_TW.eucTW', 0658 'zh_tw.euc': 'zh_TW.eucTW', 0659 } 0660 0661 # 0662 # this maps windows language identifiers (as used on Windows 95 and 0663 # earlier) to locale strings. 0664 # 0665 # NOTE: this mapping is incomplete. If your language is missing, please 0666 # submit a bug report to Python bug manager, which you can find via: 0667 # http://www.python.org/dev/ 0668 # Make sure you include the missing language identifier and the suggested 0669 # locale code. 0670 # 0671 0672 windows_locale = { 0673 0x0404: "zh_TW", # Chinese (Taiwan) 0674 0x0804: "zh_CN", # Chinese (PRC) 0675 0x0406: "da_DK", # Danish 0676 0x0413: "nl_NL", # Dutch (Netherlands) 0677 0x0409: "en_US", # English (United States) 0678 0x0809: "en_UK", # English (United Kingdom) 0679 0x0c09: "en_AU", # English (Australian) 0680 0x1009: "en_CA", # English (Canadian) 0681 0x1409: "en_NZ", # English (New Zealand) 0682 0x1809: "en_IE", # English (Ireland) 0683 0x1c09: "en_ZA", # English (South Africa) 0684 0x040b: "fi_FI", # Finnish 0685 0x040c: "fr_FR", # French (Standard) 0686 0x080c: "fr_BE", # French (Belgian) 0687 0x0c0c: "fr_CA", # French (Canadian) 0688 0x100c: "fr_CH", # French (Switzerland) 0689 0x0407: "de_DE", # German (Standard) 0690 0x0408: "el_GR", # Greek 0691 0x040d: "iw_IL", # Hebrew 0692 0x040f: "is_IS", # Icelandic 0693 0x0410: "it_IT", # Italian (Standard) 0694 0x0411: "ja_JA", # Japanese 0695 0x0414: "no_NO", # Norwegian (Bokmal) 0696 0x0816: "pt_PT", # Portuguese (Standard) 0697 0x0c0a: "es_ES", # Spanish (Modern Sort) 0698 0x0441: "sw_KE", # Swahili (Kenya) 0699 0x041d: "sv_SE", # Swedish 0700 0x081d: "sv_FI", # Swedish (Finland) 0701 0x041f: "tr_TR", # Turkish 0702 } 0703 0704 def _print_locale(): 0705 0706 """ Test function. 0707 """ 0708 categories = {} 0709 def _init_categories(categories=categories): 0710 for k,v in globals().items(): 0711 if k[:3] == 'LC_': 0712 categories[k] = v 0713 _init_categories() 0714 del categories['LC_ALL'] 0715 0716 print 'Locale defaults as determined by getdefaultlocale():' 0717 print '-'*72 0718 lang, enc = getdefaultlocale() 0719 print 'Language: ', lang or '(undefined)' 0720 print 'Encoding: ', enc or '(undefined)' 0721 print 0722 0723 print 'Locale settings on startup:' 0724 print '-'*72 0725 for name,category in categories.items(): 0726 print name, '...' 0727 lang, enc = getlocale(category) 0728 print ' Language: ', lang or '(undefined)' 0729 print ' Encoding: ', enc or '(undefined)' 0730 print 0731 0732 print 0733 print 'Locale settings after calling resetlocale():' 0734 print '-'*72 0735 resetlocale() 0736 for name,category in categories.items(): 0737 print name, '...' 0738 lang, enc = getlocale(category) 0739 print ' Language: ', lang or '(undefined)' 0740 print ' Encoding: ', enc or '(undefined)' 0741 print 0742 0743 try: 0744 setlocale(LC_ALL, "") 0745 except: 0746 print 'NOTE:' 0747 print 'setlocale(LC_ALL, "") does not support the default locale' 0748 print 'given in the OS environment variables.' 0749 else: 0750 print 0751 print 'Locale settings after calling setlocale(LC_ALL, ""):' 0752 print '-'*72 0753 for name,category in categories.items(): 0754 print name, '...' 0755 lang, enc = getlocale(category) 0756 print ' Language: ', lang or '(undefined)' 0757 print ' Encoding: ', enc or '(undefined)' 0758 print 0759 0760 ### 0761 0762 try: 0763 LC_MESSAGES 0764 except NameError: 0765 pass 0766 else: 0767 __all__.append("LC_MESSAGES") 0768 0769 if __name__=='__main__': 0770 print 'Locale aliasing:' 0771 print 0772 _print_locale() 0773 print 0774 print 'Number formatting:' 0775 print 0776 _test() 0777
Generated by PyXR 0.9.4