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