Mercurial > babel > old > mirror
annotate babel/core.py @ 184:170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
author | cmlenz |
---|---|
date | Thu, 28 Jun 2007 11:43:26 +0000 |
parents | d9dd2a4d1faf |
children | d3bde66ac8a9 |
rev | line source |
---|---|
3 | 1 # -*- coding: utf-8 -*- |
2 # | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
3 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
14 """Core locale representation and locale data access.""" |
3 | 15 |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
16 import os |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
17 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
18 from babel import localedata |
3 | 19 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
20 __all__ = ['UnknownLocaleError', 'Locale', 'default_locale', 'negotiate_locale', |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
21 'parse_locale'] |
3 | 22 __docformat__ = 'restructuredtext en' |
23 | |
24 | |
33 | 25 class UnknownLocaleError(Exception): |
26 """Exception thrown when a locale is requested for which no locale data | |
27 is available. | |
28 """ | |
29 | |
30 def __init__(self, identifier): | |
31 """Create the exception. | |
32 | |
33 :param identifier: the identifier string of the unsupported locale | |
34 """ | |
35 Exception.__init__(self, 'unknown locale %r' % identifier) | |
36 self.identifier = identifier | |
37 | |
38 | |
3 | 39 class Locale(object): |
40 """Representation of a specific locale. | |
41 | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
42 >>> locale = Locale('en', 'US') |
3 | 43 >>> repr(locale) |
44 '<Locale "en_US">' | |
45 >>> locale.display_name | |
46 u'English (United States)' | |
47 | |
48 A `Locale` object can also be instantiated from a raw locale string: | |
49 | |
50 >>> locale = Locale.parse('en-US', sep='-') | |
51 >>> repr(locale) | |
52 '<Locale "en_US">' | |
53 | |
54 `Locale` objects provide access to a collection of locale data, such as | |
55 territory and language names, number and date format patterns, and more: | |
56 | |
57 >>> locale.number_symbols['decimal'] | |
58 u'.' | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
59 |
33 | 60 If a locale is requested for which no locale data is available, an |
61 `UnknownLocaleError` is raised: | |
62 | |
63 >>> Locale.parse('en_DE') | |
64 Traceback (most recent call last): | |
65 ... | |
66 UnknownLocaleError: unknown locale 'en_DE' | |
3 | 67 |
68 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
69 """ | |
70 | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
71 def __init__(self, language, territory=None, script=None, variant=None): |
3 | 72 """Initialize the locale object from the given identifier components. |
73 | |
74 >>> locale = Locale('en', 'US') | |
75 >>> locale.language | |
76 'en' | |
77 >>> locale.territory | |
78 'US' | |
79 | |
80 :param language: the language code | |
81 :param territory: the territory (country or region) code | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
82 :param script: the script code |
3 | 83 :param variant: the variant code |
33 | 84 :raise `UnknownLocaleError`: if no locale data is available for the |
85 requested locale | |
3 | 86 """ |
87 self.language = language | |
88 self.territory = territory | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
89 self.script = script |
3 | 90 self.variant = variant |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
91 self.__data = None |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
92 |
33 | 93 identifier = str(self) |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
94 if not localedata.exists(identifier): |
33 | 95 raise UnknownLocaleError(identifier) |
3 | 96 |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
97 def default(cls, category=None): |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
98 """Return the system default locale for the specified category. |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
99 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
100 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
101 ... os.environ[name] = '' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
102 >>> os.environ['LANG'] = 'fr_FR.UTF-8' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
103 >>> Locale.default('LC_MESSAGES') |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
104 <Locale "fr_FR"> |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
105 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
106 :param category: one of the ``LC_XXX`` environment variable names |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
107 :return: the value of the variable, or any of the fallbacks |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
108 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
109 :rtype: `Locale` |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
110 """ |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
111 return cls(default_locale(category)) |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
112 default = classmethod(default) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
113 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
114 def negotiate(cls, preferred, available, sep='_'): |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
115 """Find the best match between available and requested locale strings. |
158 | 116 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
117 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
118 <Locale "de_DE"> |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
119 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
120 <Locale "de"> |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
121 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) |
158 | 122 |
123 You can specify the character used in the locale identifiers to separate | |
124 the differnet components. This separator is applied to both lists. Also, | |
125 case is ignored in the comparison: | |
126 | |
127 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') | |
128 <Locale "de_DE"> | |
129 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
130 :param preferred: the list of locale identifers preferred by the user |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
131 :param available: the list of locale identifiers available |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
132 :return: the `Locale` object for the best match, or `None` if no match |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
133 was found |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
134 :rtype: `Locale` |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
135 """ |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
136 identifier = negotiate_locale(preferred, available, sep=sep) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
137 if identifier: |
158 | 138 return Locale.parse(identifier, sep=sep) |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
139 negotiate = classmethod(negotiate) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
140 |
3 | 141 def parse(cls, identifier, sep='_'): |
142 """Create a `Locale` instance for the given locale identifier. | |
143 | |
144 >>> l = Locale.parse('de-DE', sep='-') | |
145 >>> l.display_name | |
146 u'Deutsch (Deutschland)' | |
147 | |
148 If the `identifier` parameter is not a string, but actually a `Locale` | |
149 object, that object is returned: | |
150 | |
151 >>> Locale.parse(l) | |
152 <Locale "de_DE"> | |
153 | |
154 :param identifier: the locale identifier string | |
155 :param sep: optional component separator | |
156 :return: a corresponding `Locale` instance | |
157 :rtype: `Locale` | |
158 :raise `ValueError`: if the string does not appear to be a valid locale | |
159 identifier | |
33 | 160 :raise `UnknownLocaleError`: if no locale data is available for the |
161 requested locale | |
3 | 162 """ |
163 if type(identifier) is cls: | |
164 return identifier | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
165 return cls(*parse_locale(identifier, sep=sep)) |
3 | 166 parse = classmethod(parse) |
167 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
168 def __eq__(self, other): |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
169 return str(self) == str(other) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
170 |
3 | 171 def __repr__(self): |
172 return '<Locale "%s">' % str(self) | |
173 | |
174 def __str__(self): | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
175 return '_'.join(filter(None, [self.language, self.script, |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
176 self.territory, self.variant])) |
3 | 177 |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
178 def _data(self): |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
179 if self.__data is None: |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
180 self.__data = localedata.load(str(self)) |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
181 return self.__data |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
182 _data = property(_data) |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
183 |
3 | 184 def display_name(self): |
185 retval = self.languages.get(self.language) | |
186 if self.territory: | |
187 variant = '' | |
188 if self.variant: | |
189 variant = ', %s' % self.variants.get(self.variant) | |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
190 retval += ' (%s%s)' % (self.territories.get(self.territory), |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
191 variant) |
3 | 192 return retval |
193 display_name = property(display_name, doc="""\ | |
194 The localized display name of the locale. | |
195 | |
196 >>> Locale('en').display_name | |
197 u'English' | |
198 >>> Locale('en', 'US').display_name | |
199 u'English (United States)' | |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
200 >>> Locale('sv').display_name |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
201 u'svenska' |
3 | 202 |
203 :type: `unicode` | |
204 """) | |
205 | |
55 | 206 def english_name(self): |
207 en = Locale('en') | |
208 retval = en.languages.get(self.language) | |
209 if self.territory: | |
210 variant = '' | |
211 if self.variant: | |
212 variant = ', %s' % en.variants.get(self.variant) | |
213 retval += ' (%s%s)' % (en.territories.get(self.territory), | |
214 variant) | |
215 return retval | |
216 english_name = property(english_name, doc="""\ | |
217 The english display name of the locale. | |
218 | |
219 >>> Locale('de').english_name | |
220 u'German' | |
221 >>> Locale('de', 'DE').english_name | |
222 u'German (Germany)' | |
223 | |
224 :type: `unicode` | |
225 """) | |
226 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
227 #{ General Locale Display Names |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
228 |
3 | 229 def languages(self): |
230 return self._data['languages'] | |
231 languages = property(languages, doc="""\ | |
232 Mapping of language codes to translated language names. | |
233 | |
234 >>> Locale('de', 'DE').languages['ja'] | |
235 u'Japanisch' | |
236 | |
237 :type: `dict` | |
238 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ | |
239 """) | |
240 | |
241 def scripts(self): | |
242 return self._data['scripts'] | |
243 scripts = property(scripts, doc="""\ | |
244 Mapping of script codes to translated script names. | |
245 | |
246 >>> Locale('en', 'US').scripts['Hira'] | |
247 u'Hiragana' | |
248 | |
249 :type: `dict` | |
250 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ | |
251 """) | |
252 | |
253 def territories(self): | |
254 return self._data['territories'] | |
255 territories = property(territories, doc="""\ | |
256 Mapping of script codes to translated script names. | |
257 | |
258 >>> Locale('es', 'CO').territories['DE'] | |
259 u'Alemania' | |
260 | |
261 :type: `dict` | |
262 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ | |
263 """) | |
264 | |
265 def variants(self): | |
266 return self._data['variants'] | |
267 variants = property(variants, doc="""\ | |
268 Mapping of script codes to translated script names. | |
269 | |
270 >>> Locale('de', 'DE').variants['1901'] | |
271 u'alte deutsche Rechtschreibung' | |
272 | |
273 :type: `dict` | |
274 """) | |
275 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
276 #{ Number Formatting |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
277 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
278 def currencies(self): |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
279 return self._data['currency_names'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
280 currencies = property(currencies, doc="""\ |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
281 Mapping of currency codes to translated currency names. |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
282 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
283 >>> Locale('en').currencies['COP'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
284 u'Colombian Peso' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
285 >>> Locale('de', 'DE').currencies['COP'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
286 u'Kolumbianischer Peso' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
287 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
288 :type: `dict` |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
289 """) |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
290 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
291 def currency_symbols(self): |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
292 return self._data['currency_symbols'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
293 currency_symbols = property(currency_symbols, doc="""\ |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
294 Mapping of currency codes to symbols. |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
295 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
296 >>> Locale('en').currency_symbols['USD'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
297 u'US$' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
298 >>> Locale('en', 'US').currency_symbols['USD'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
299 u'$' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
300 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
301 :type: `dict` |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
302 """) |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
303 |
3 | 304 def number_symbols(self): |
305 return self._data['number_symbols'] | |
306 number_symbols = property(number_symbols, doc="""\ | |
307 Symbols used in number formatting. | |
308 | |
309 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
310 u',' | |
311 | |
312 :type: `dict` | |
313 """) | |
314 | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
315 def decimal_formats(self): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
316 return self._data['decimal_formats'] |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
317 decimal_formats = property(decimal_formats, doc="""\ |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
318 Locale patterns for decimal number formatting. |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
319 |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
320 >>> Locale('en', 'US').decimal_formats[None] |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
321 <NumberPattern u'#,##0.###'> |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
322 |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
323 :type: `dict` |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
324 """) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
325 |
127 | 326 def currency_formats(self): |
327 return self._data['currency_formats'] | |
328 currency_formats = property(currency_formats, doc=r"""\ | |
329 Locale patterns for currency number formatting. | |
330 | |
331 >>> print Locale('en', 'US').currency_formats[None] | |
332 <NumberPattern u'\xa4#,##0.00'> | |
333 | |
334 :type: `dict` | |
335 """) | |
336 | |
24 | 337 def percent_formats(self): |
338 return self._data['percent_formats'] | |
339 percent_formats = property(percent_formats, doc="""\ | |
340 Locale patterns for percent number formatting. | |
341 | |
342 >>> Locale('en', 'US').percent_formats[None] | |
343 <NumberPattern u'#,##0%'> | |
344 | |
345 :type: `dict` | |
346 """) | |
347 | |
127 | 348 def scientific_formats(self): |
349 return self._data['scientific_formats'] | |
350 scientific_formats = property(scientific_formats, doc="""\ | |
351 Locale patterns for scientific number formatting. | |
352 | |
353 >>> Locale('en', 'US').scientific_formats[None] | |
354 <NumberPattern u'#E0'> | |
355 | |
356 :type: `dict` | |
357 """) | |
358 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
359 #{ Calendar Information and Date Formatting |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
360 |
3 | 361 def periods(self): |
362 return self._data['periods'] | |
363 periods = property(periods, doc="""\ | |
364 Locale display names for day periods (AM/PM). | |
365 | |
366 >>> Locale('en', 'US').periods['am'] | |
367 u'AM' | |
368 | |
369 :type: `dict` | |
370 """) | |
371 | |
372 def days(self): | |
373 return self._data['days'] | |
374 days = property(days, doc="""\ | |
375 Locale display names for weekdays. | |
376 | |
17 | 377 >>> Locale('de', 'DE').days['format']['wide'][3] |
3 | 378 u'Donnerstag' |
379 | |
380 :type: `dict` | |
381 """) | |
382 | |
383 def months(self): | |
384 return self._data['months'] | |
385 months = property(months, doc="""\ | |
386 Locale display names for months. | |
387 | |
388 >>> Locale('de', 'DE').months['format']['wide'][10] | |
389 u'Oktober' | |
390 | |
391 :type: `dict` | |
392 """) | |
393 | |
394 def quarters(self): | |
395 return self._data['quarters'] | |
396 quarters = property(quarters, doc="""\ | |
397 Locale display names for quarters. | |
398 | |
399 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
400 u'1. Quartal' | |
401 | |
402 :type: `dict` | |
403 """) | |
404 | |
405 def eras(self): | |
406 return self._data['eras'] | |
407 eras = property(eras, doc="""\ | |
408 Locale display names for eras. | |
409 | |
410 >>> Locale('en', 'US').eras['wide'][1] | |
411 u'Anno Domini' | |
412 >>> Locale('en', 'US').eras['abbreviated'][0] | |
413 u'BC' | |
414 | |
415 :type: `dict` | |
416 """) | |
417 | |
30
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
418 def time_zones(self): |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
419 return self._data['time_zones'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
420 time_zones = property(time_zones, doc="""\ |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
421 Locale display names for time zones. |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
422 |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
423 >>> Locale('en', 'US').time_zones['America/Los_Angeles']['long']['standard'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
424 u'Pacific Standard Time' |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
425 >>> Locale('en', 'US').time_zones['Europe/Dublin']['city'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
426 u'Dublin' |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
427 |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
428 :type: `dict` |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
429 """) |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
430 |
36 | 431 def zone_aliases(self): |
432 return self._data['zone_aliases'] | |
433 zone_aliases = property(zone_aliases, doc="""\ | |
434 Mapping of time zone aliases to their respective canonical identifer. | |
435 | |
436 >>> Locale('en').zone_aliases['UTC'] | |
437 'Etc/GMT' | |
438 | |
439 :type: `dict` | |
440 :note: this doesn't really belong here, as it does not change between | |
441 locales | |
442 """) | |
443 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
444 def first_week_day(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
445 return self._data['week_data']['first_day'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
446 first_week_day = property(first_week_day, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
447 The first day of a week. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
448 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
449 >>> Locale('de', 'DE').first_week_day |
17 | 450 0 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
451 >>> Locale('en', 'US').first_week_day |
17 | 452 6 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
453 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
454 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
455 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
456 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
457 def weekend_start(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
458 return self._data['week_data']['weekend_start'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
459 weekend_start = property(weekend_start, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
460 The day the weekend starts. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
461 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
462 >>> Locale('de', 'DE').weekend_start |
17 | 463 5 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
464 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
465 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
466 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
467 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
468 def weekend_end(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
469 return self._data['week_data']['weekend_end'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
470 weekend_end = property(weekend_end, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
471 The day the weekend ends. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
472 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
473 >>> Locale('de', 'DE').weekend_end |
17 | 474 6 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
475 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
476 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
477 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
478 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
479 def min_week_days(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
480 return self._data['week_data']['min_days'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
481 min_week_days = property(min_week_days, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
482 The minimum number of days in a week so that the week is counted as the |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
483 first week of a year or month. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
484 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
485 >>> Locale('de', 'DE').min_week_days |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
486 4 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
487 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
488 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
489 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
490 |
3 | 491 def date_formats(self): |
492 return self._data['date_formats'] | |
493 date_formats = property(date_formats, doc="""\ | |
494 Locale patterns for date formatting. | |
495 | |
496 >>> Locale('en', 'US').date_formats['short'] | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
497 <DateTimePattern u'M/d/yy'> |
3 | 498 >>> Locale('fr', 'FR').date_formats['long'] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
499 <DateTimePattern u'd MMMM yyyy'> |
3 | 500 |
501 :type: `dict` | |
502 """) | |
503 | |
504 def time_formats(self): | |
505 return self._data['time_formats'] | |
506 time_formats = property(time_formats, doc="""\ | |
507 Locale patterns for time formatting. | |
508 | |
509 >>> Locale('en', 'US').time_formats['short'] | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
510 <DateTimePattern u'h:mm a'> |
3 | 511 >>> Locale('fr', 'FR').time_formats['long'] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
512 <DateTimePattern u'HH:mm:ss z'> |
11 | 513 |
514 :type: `dict` | |
515 """) | |
516 | |
35 | 517 def datetime_formats(self): |
518 return self._data['datetime_formats'] | |
519 datetime_formats = property(datetime_formats, doc="""\ | |
520 Locale patterns for datetime formatting. | |
521 | |
522 >>> Locale('en').datetime_formats[None] | |
523 u'{1} {0}' | |
524 >>> Locale('th').datetime_formats[None] | |
525 u'{1}, {0}' | |
526 | |
527 :type: `dict` | |
528 """) | |
529 | |
3 | 530 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
531 def default_locale(category=None): |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
532 """Returns the system default locale for a given category, based on |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
533 environment variables. |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
534 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
535 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
536 ... os.environ[name] = '' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
537 >>> os.environ['LANG'] = 'fr_FR.UTF-8' |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
538 >>> default_locale('LC_MESSAGES') |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
539 'fr_FR' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
540 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
541 :param category: one of the ``LC_XXX`` environment variable names |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
542 :return: the value of the variable, or any of the fallbacks (``LANGUAGE``, |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
543 ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
544 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
545 :rtype: `str` |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
546 """ |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
547 varnames = (category, 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG') |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
548 for name in filter(None, varnames): |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
549 locale = os.getenv(name) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
550 if locale: |
150
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
551 if name == 'LANGUAGE' and ':' in locale: |
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
552 # the LANGUAGE variable may contain a colon-separated list of |
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
553 # language codes; we just pick the language on the list |
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
554 locale = locale.split(':')[0] |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
555 return '_'.join(filter(None, parse_locale(locale))) |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
556 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
557 def negotiate_locale(preferred, available, sep='_'): |
3 | 558 """Find the best match between available and requested locale strings. |
559 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
560 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
3 | 561 'de_DE' |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
562 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) |
3 | 563 'de' |
564 | |
158 | 565 Case is ignored by the algorithm, the result uses the case of the preferred |
566 locale identifier: | |
567 | |
568 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) | |
569 'de_DE' | |
570 | |
3 | 571 :param preferred: the list of locale strings preferred by the user |
572 :param available: the list of locale strings available | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
573 :param sep: character that separates the different parts of the locale |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
574 strings |
3 | 575 :return: the locale identifier for the best match, or `None` if no match |
576 was found | |
577 :rtype: `str` | |
578 """ | |
158 | 579 available = [a.lower() for a in available if a] |
3 | 580 for locale in preferred: |
158 | 581 if locale.lower() in available: |
3 | 582 return locale |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
583 parts = locale.split(sep) |
158 | 584 if len(parts) > 1 and parts[0].lower() in available: |
3 | 585 return parts[0] |
586 return None | |
587 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
588 def parse_locale(identifier, sep='_'): |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
589 """Parse a locale identifier into a tuple of the form:: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
590 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
591 ``(language, territory, script, variant)`` |
3 | 592 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
593 >>> parse_locale('zh_CN') |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
594 ('zh', 'CN', None, None) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
595 >>> parse_locale('zh_Hans_CN') |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
596 ('zh', 'CN', 'Hans', None) |
3 | 597 |
598 The default component separator is "_", but a different separator can be | |
599 specified using the `sep` parameter: | |
600 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
601 >>> parse_locale('zh-CN', sep='-') |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
602 ('zh', 'CN', None, None) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
603 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
604 If the identifier cannot be parsed into a locale, a `ValueError` exception |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
605 is raised: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
606 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
607 >>> parse_locale('not_a_LOCALE_String') |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
608 Traceback (most recent call last): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
609 ... |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
610 ValueError: 'not_a_LOCALE_String' is not a valid locale identifier |
3 | 611 |
612 :param identifier: the locale identifier string | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
613 :param sep: character that separates the different components of the locale |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
614 identifier |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
615 :return: the ``(language, territory, script, variant)`` tuple |
3 | 616 :rtype: `tuple` |
617 :raise `ValueError`: if the string does not appear to be a valid locale | |
618 identifier | |
619 | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
620 :see: `IETF RFC 4646 <http://www.ietf.org/rfc/rfc4646.txt>`_ |
3 | 621 """ |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
622 if '.' in identifier: |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
623 # this is probably the charset/encoding, which we don't care about |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
624 identifier = identifier.split('.', 1)[0] |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
625 |
3 | 626 parts = identifier.split(sep) |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
627 lang = parts.pop(0).lower() |
3 | 628 if not lang.isalpha(): |
629 raise ValueError('expected only letters, got %r' % lang) | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
630 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
631 script = territory = variant = None |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
632 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
633 if len(parts[0]) == 4 and parts[0].isalpha(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
634 script = parts.pop(0).title() |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
635 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
636 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
637 if len(parts[0]) == 2 and parts[0].isalpha(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
638 territory = parts.pop(0).upper() |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
639 elif len(parts[0]) == 3 and parts[0].isdigit(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
640 territory = parts.pop(0) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
641 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
642 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
643 if len(parts[0]) == 4 and parts[0][0].isdigit() or \ |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
644 len(parts[0]) >= 5 and parts[0][0].isalpha(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
645 variant = parts.pop() |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
646 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
647 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
648 raise ValueError('%r is not a valid locale identifier' % identifier) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
649 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
650 return lang, territory, script, variant |