1 """Locale utility routines."""
15 __all__
= ["localelist"]
17 def localelist(*locales
):
18 """Normalize and return a list of locales, with appended defaults.
20 e.g. on a system with en_US as the default locale,
22 localelist('en_GB.utf8', 'de_DE') =>
23 ['en_GB', 'en', 'de_DE', 'de', 'en_US', 'C']
27 locales
= list(locales
)
29 if locale
is not None:
30 # Set the locale if it hasn't already been set, but don't fail
31 # if we can't set it for some reason.
32 if locale
.getlocale(locale
.LC_COLLATE
)[0] is None:
34 locale
.setlocale(locale
.LC_COLLATE
, '')
38 # Throw in the user's specified collation locale, the current locale,
39 # the default locale, and POSIX, for free.
40 locales
.append(locale
.getlocale(locale
.LC_COLLATE
)[0])
41 locales
.append(locale
.getlocale()[0])
42 locales
.append(locale
.getdefaultlocale()[0])
45 # Don't put the same locale in the return list more than twice.
52 if locale
is not None:
53 code
= locale
.normalize(code
)
54 # Strip off encoding if present.
55 code
= code
.split(".")[0]
56 if code
.lower() not in added
:
58 added
.add(code
.lower())
59 # Strip off territory if present.
60 code
= code
.split("_")[0]
61 if code
.lower() not in added
:
63 added
.add(code
.lower())
67 def encoding(preferred
=None):
68 """Try to find an optimal encoding.
71 preferred - use this encoding if possible
73 Otherwise, the locale encoding or the Python system encoding are
76 # can't use any codecs, use the system one (ascii).
78 return sys
.getdefaultencoding()
80 # if preferred is a valid codec, use it.
81 if preferred
is not None:
83 return codecs
.lookup(preferred
).name
84 except (LookupError, AttributeError):
87 # preferred is bad and can't get it from locale.
89 return sys
.getdefaultencoding()
91 # try to get it from the locale, if not there, set it and try again.
92 fromlocale
= locale
.getlocale(locale
.LC_COLLATE
)[1]
93 if fromlocale
is not None:
96 locale
.setlocale(locale
.LC_COLLATE
, '')[1]
100 fromlocale
= locale
.getlocale(locale
.LC_COLLATE
)
101 if fromlocale
is not None:
104 # okay, LC_COLLATE isn't set, maybe the generic locale is.
105 fromlocale
= locale
.getlocale()[1]
106 if fromlocale
is not None:
109 # but we won't reset the generic locale if it isn't, that'd be
112 # if the locale can't even give us a simple encoding, go back
113 # to the system one, and give up.
114 return locale
.getpreferredencoding() or sys
.getdefaultencoding()
116 def getpair(locale_
, encoding_
):
118 if encoding_
is None:
119 locale_
, encoding_
= locale_
.rsplit(".", 1)
121 locale_
= locale_
.rsplit(".")[0]
122 return locale_
, encoding(encoding_
)