Mercurial > babel > old > babel-test
annotate babel/core.py @ 185:170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
author | cmlenz |
---|---|
date | Thu, 28 Jun 2007 16:34:40 +0000 |
parents | 0db5d8723c76 |
children | bc22f5aef216 |
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 | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
42 >>> locale = Locale('en', 'US') |
1 | 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'.' | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
59 |
31 | 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 | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
71 def __init__(self, language, territory=None, script=None, variant=None): |
1 | 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 | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
82 :param script: the script code |
1 | 83 :param variant: the variant code |
31 | 84 :raise `UnknownLocaleError`: if no locale data is available for the |
85 requested locale | |
1 | 86 """ |
87 self.language = language | |
88 self.territory = territory | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
89 self.script = script |
1 | 90 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
|
91 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
|
92 |
31 | 93 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
|
94 if not localedata.exists(identifier): |
31 | 95 raise UnknownLocaleError(identifier) |
1 | 96 |
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
|
97 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
|
98 """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
|
99 |
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 >>> 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
|
101 ... 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
|
102 >>> 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
|
103 >>> 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
|
104 <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
|
105 |
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 :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
|
107 :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
|
108 (``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
|
109 :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
|
110 """ |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
111 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
|
112 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
|
113 |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
114 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
|
115 """Find the best match between available and requested locale strings. |
156 | 116 |
72
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'], ['de_DE', 'de_AT']) |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
118 <Locale "de_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', 'en_US'], ['en', 'de']) |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
120 <Locale "de"> |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
121 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) |
156 | 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 | |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
130 :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
|
131 :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
|
132 :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
|
133 was found |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
134 :rtype: `Locale` |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
135 """ |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
136 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
|
137 if identifier: |
156 | 138 return Locale.parse(identifier, sep=sep) |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
139 negotiate = classmethod(negotiate) |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
140 |
1 | 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 | |
31 | 160 :raise `UnknownLocaleError`: if no locale data is available for the |
161 requested locale | |
1 | 162 """ |
163 if type(identifier) is cls: | |
164 return identifier | |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
165 return cls(*parse_locale(identifier, sep=sep)) |
1 | 166 parse = classmethod(parse) |
167 | |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
168 def __eq__(self, other): |
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
169 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
|
170 |
1 | 171 def __repr__(self): |
172 return '<Locale "%s">' % str(self) | |
173 | |
174 def __str__(self): | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
175 return '_'.join(filter(None, [self.language, self.script, |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
176 self.territory, self.variant])) |
1 | 177 |
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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 _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
|
183 |
185
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
184 def get_display_name(self, locale=None): |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
185 """Return the display name of the locale using the given locale. |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
186 |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
187 The display name will include the language, territory, script, and |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
188 variant, if those are specified. |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
189 |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
190 >>> Locale('zh', 'CN', script='Hans').get_display_name('en') |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
191 u'Chinese (Simplified Han, China)' |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
192 |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
193 :param locale: the locale to use |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
194 :return: the display name |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
195 """ |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
196 if locale is None: |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
197 locale = self |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
198 locale = Locale.parse(locale) |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
199 retval = locale.languages.get(self.language) |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
200 if self.territory or self.script or self.variant: |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
201 details = [] |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
202 if self.script: |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
203 details.append(locale.scripts.get(self.script)) |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
204 if self.territory: |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
205 details.append(locale.territories.get(self.territory)) |
1 | 206 if self.variant: |
185
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
207 details.append(locale.variants.get(self.variant)) |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
208 details = filter(None, details) |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
209 if details: |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
210 retval += ' (%s)' % u', '.join(details) |
1 | 211 return retval |
185
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
212 |
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
213 display_name = property(get_display_name, doc="""\ |
1 | 214 The localized display name of the locale. |
215 | |
216 >>> Locale('en').display_name | |
217 u'English' | |
218 >>> Locale('en', 'US').display_name | |
219 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
|
220 >>> 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
|
221 u'svenska' |
1 | 222 |
223 :type: `unicode` | |
224 """) | |
225 | |
53 | 226 def english_name(self): |
185
170cffc66554
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
227 return self.get_display_name(Locale('en')) |
53 | 228 english_name = property(english_name, doc="""\ |
229 The english display name of the locale. | |
230 | |
231 >>> Locale('de').english_name | |
232 u'German' | |
233 >>> Locale('de', 'DE').english_name | |
234 u'German (Germany)' | |
235 | |
236 :type: `unicode` | |
237 """) | |
238 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
239 #{ 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
|
240 |
1 | 241 def languages(self): |
242 return self._data['languages'] | |
243 languages = property(languages, doc="""\ | |
244 Mapping of language codes to translated language names. | |
245 | |
246 >>> Locale('de', 'DE').languages['ja'] | |
247 u'Japanisch' | |
248 | |
249 :type: `dict` | |
250 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ | |
251 """) | |
252 | |
253 def scripts(self): | |
254 return self._data['scripts'] | |
255 scripts = property(scripts, doc="""\ | |
256 Mapping of script codes to translated script names. | |
257 | |
258 >>> Locale('en', 'US').scripts['Hira'] | |
259 u'Hiragana' | |
260 | |
261 :type: `dict` | |
262 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ | |
263 """) | |
264 | |
265 def territories(self): | |
266 return self._data['territories'] | |
267 territories = property(territories, doc="""\ | |
268 Mapping of script codes to translated script names. | |
269 | |
270 >>> Locale('es', 'CO').territories['DE'] | |
271 u'Alemania' | |
272 | |
273 :type: `dict` | |
274 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ | |
275 """) | |
276 | |
277 def variants(self): | |
278 return self._data['variants'] | |
279 variants = property(variants, doc="""\ | |
280 Mapping of script codes to translated script names. | |
281 | |
282 >>> Locale('de', 'DE').variants['1901'] | |
283 u'alte deutsche Rechtschreibung' | |
284 | |
285 :type: `dict` | |
286 """) | |
287 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
288 #{ 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
|
289 |
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
|
290 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
|
291 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
|
292 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
|
293 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
|
294 |
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
|
295 >>> 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
|
296 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
|
297 >>> 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
|
298 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
|
299 |
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
|
300 :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
|
301 """) |
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
|
302 |
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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 |
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
|
308 >>> 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
|
309 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
|
310 >>> 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
|
311 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
|
312 |
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
|
313 :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
|
314 """) |
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
|
315 |
1 | 316 def number_symbols(self): |
317 return self._data['number_symbols'] | |
318 number_symbols = property(number_symbols, doc="""\ | |
319 Symbols used in number formatting. | |
320 | |
321 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
322 u',' | |
323 | |
324 :type: `dict` | |
325 """) | |
326 | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
327 def decimal_formats(self): |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
328 return self._data['decimal_formats'] |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
329 decimal_formats = property(decimal_formats, doc="""\ |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
330 Locale patterns for decimal number formatting. |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
331 |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
332 >>> Locale('en', 'US').decimal_formats[None] |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
333 <NumberPattern u'#,##0.###'> |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
334 |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
335 :type: `dict` |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
336 """) |
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
337 |
125 | 338 def currency_formats(self): |
339 return self._data['currency_formats'] | |
340 currency_formats = property(currency_formats, doc=r"""\ | |
341 Locale patterns for currency number formatting. | |
342 | |
343 >>> print Locale('en', 'US').currency_formats[None] | |
344 <NumberPattern u'\xa4#,##0.00'> | |
345 | |
346 :type: `dict` | |
347 """) | |
348 | |
22 | 349 def percent_formats(self): |
350 return self._data['percent_formats'] | |
351 percent_formats = property(percent_formats, doc="""\ | |
352 Locale patterns for percent number formatting. | |
353 | |
354 >>> Locale('en', 'US').percent_formats[None] | |
355 <NumberPattern u'#,##0%'> | |
356 | |
357 :type: `dict` | |
358 """) | |
359 | |
125 | 360 def scientific_formats(self): |
361 return self._data['scientific_formats'] | |
362 scientific_formats = property(scientific_formats, doc="""\ | |
363 Locale patterns for scientific number formatting. | |
364 | |
365 >>> Locale('en', 'US').scientific_formats[None] | |
366 <NumberPattern u'#E0'> | |
367 | |
368 :type: `dict` | |
369 """) | |
370 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
371 #{ 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
|
372 |
1 | 373 def periods(self): |
374 return self._data['periods'] | |
375 periods = property(periods, doc="""\ | |
376 Locale display names for day periods (AM/PM). | |
377 | |
378 >>> Locale('en', 'US').periods['am'] | |
379 u'AM' | |
380 | |
381 :type: `dict` | |
382 """) | |
383 | |
384 def days(self): | |
385 return self._data['days'] | |
386 days = property(days, doc="""\ | |
387 Locale display names for weekdays. | |
388 | |
15 | 389 >>> Locale('de', 'DE').days['format']['wide'][3] |
1 | 390 u'Donnerstag' |
391 | |
392 :type: `dict` | |
393 """) | |
394 | |
395 def months(self): | |
396 return self._data['months'] | |
397 months = property(months, doc="""\ | |
398 Locale display names for months. | |
399 | |
400 >>> Locale('de', 'DE').months['format']['wide'][10] | |
401 u'Oktober' | |
402 | |
403 :type: `dict` | |
404 """) | |
405 | |
406 def quarters(self): | |
407 return self._data['quarters'] | |
408 quarters = property(quarters, doc="""\ | |
409 Locale display names for quarters. | |
410 | |
411 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
412 u'1. Quartal' | |
413 | |
414 :type: `dict` | |
415 """) | |
416 | |
417 def eras(self): | |
418 return self._data['eras'] | |
419 eras = property(eras, doc="""\ | |
420 Locale display names for eras. | |
421 | |
422 >>> Locale('en', 'US').eras['wide'][1] | |
423 u'Anno Domini' | |
424 >>> Locale('en', 'US').eras['abbreviated'][0] | |
425 u'BC' | |
426 | |
427 :type: `dict` | |
428 """) | |
429 | |
28
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
430 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
|
431 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
|
432 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
|
433 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
|
434 |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
435 >>> 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
|
436 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
|
437 >>> 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
|
438 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
|
439 |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
440 :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
|
441 """) |
11278622ede9
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
26
diff
changeset
|
442 |
34 | 443 def zone_aliases(self): |
444 return self._data['zone_aliases'] | |
445 zone_aliases = property(zone_aliases, doc="""\ | |
446 Mapping of time zone aliases to their respective canonical identifer. | |
447 | |
448 >>> Locale('en').zone_aliases['UTC'] | |
449 'Etc/GMT' | |
450 | |
451 :type: `dict` | |
452 :note: this doesn't really belong here, as it does not change between | |
453 locales | |
454 """) | |
455 | |
8
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
456 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
|
457 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
|
458 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
|
459 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
|
460 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
461 >>> Locale('de', 'DE').first_week_day |
15 | 462 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
|
463 >>> Locale('en', 'US').first_week_day |
15 | 464 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
|
465 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
466 :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
|
467 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
468 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
469 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
|
470 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
|
471 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
|
472 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
|
473 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
474 >>> Locale('de', 'DE').weekend_start |
15 | 475 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
|
476 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
477 :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
|
478 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
479 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
480 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
|
481 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
|
482 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
|
483 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
|
484 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
485 >>> Locale('de', 'DE').weekend_end |
15 | 486 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
|
487 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
488 :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
|
489 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
490 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
491 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
|
492 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
|
493 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
|
494 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
|
495 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
|
496 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
497 >>> 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
|
498 4 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
499 |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
500 :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
|
501 """) |
9132c9218745
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
502 |
1 | 503 def date_formats(self): |
504 return self._data['date_formats'] | |
505 date_formats = property(date_formats, doc="""\ | |
506 Locale patterns for date formatting. | |
507 | |
508 >>> Locale('en', 'US').date_formats['short'] | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
509 <DateTimePattern u'M/d/yy'> |
1 | 510 >>> Locale('fr', 'FR').date_formats['long'] |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
511 <DateTimePattern u'd MMMM yyyy'> |
1 | 512 |
513 :type: `dict` | |
514 """) | |
515 | |
516 def time_formats(self): | |
517 return self._data['time_formats'] | |
518 time_formats = property(time_formats, doc="""\ | |
519 Locale patterns for time formatting. | |
520 | |
521 >>> Locale('en', 'US').time_formats['short'] | |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
522 <DateTimePattern u'h:mm a'> |
1 | 523 >>> Locale('fr', 'FR').time_formats['long'] |
12
a2c54ef107c2
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
524 <DateTimePattern u'HH:mm:ss z'> |
9 | 525 |
526 :type: `dict` | |
527 """) | |
528 | |
33 | 529 def datetime_formats(self): |
530 return self._data['datetime_formats'] | |
531 datetime_formats = property(datetime_formats, doc="""\ | |
532 Locale patterns for datetime formatting. | |
533 | |
534 >>> Locale('en').datetime_formats[None] | |
535 u'{1} {0}' | |
536 >>> Locale('th').datetime_formats[None] | |
537 u'{1}, {0}' | |
538 | |
539 :type: `dict` | |
540 """) | |
541 | |
1 | 542 |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
543 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
|
544 """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
|
545 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
|
546 |
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
|
547 >>> 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
|
548 ... 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
|
549 >>> 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
|
550 >>> 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
|
551 '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
|
552 |
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
|
553 :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
|
554 :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
|
555 ``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
|
556 |
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
|
557 :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
|
558 """ |
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
|
559 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
|
560 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
|
561 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
|
562 if locale: |
148
37f4875bad88
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
125
diff
changeset
|
563 if name == 'LANGUAGE' and ':' in locale: |
37f4875bad88
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
125
diff
changeset
|
564 # the LANGUAGE variable may contain a colon-separated list of |
37f4875bad88
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
125
diff
changeset
|
565 # language codes; we just pick the language on the list |
37f4875bad88
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
125
diff
changeset
|
566 locale = locale.split(':')[0] |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
567 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
|
568 |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
569 def negotiate_locale(preferred, available, sep='_'): |
1 | 570 """Find the best match between available and requested locale strings. |
571 | |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
572 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
1 | 573 'de_DE' |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
574 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) |
1 | 575 'de' |
576 | |
156 | 577 Case is ignored by the algorithm, the result uses the case of the preferred |
578 locale identifier: | |
579 | |
580 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) | |
581 'de_DE' | |
582 | |
1 | 583 :param preferred: the list of locale strings preferred by the user |
584 :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
|
585 :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
|
586 strings |
1 | 587 :return: the locale identifier for the best match, or `None` if no match |
588 was found | |
589 :rtype: `str` | |
590 """ | |
156 | 591 available = [a.lower() for a in available if a] |
1 | 592 for locale in preferred: |
156 | 593 if locale.lower() in available: |
1 | 594 return locale |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
595 parts = locale.split(sep) |
156 | 596 if len(parts) > 1 and parts[0].lower() in available: |
1 | 597 return parts[0] |
598 return None | |
599 | |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
600 def parse_locale(identifier, sep='_'): |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
601 """Parse a locale identifier into a tuple of the form:: |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
602 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
603 ``(language, territory, script, variant)`` |
1 | 604 |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
605 >>> parse_locale('zh_CN') |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
606 ('zh', 'CN', None, None) |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
607 >>> parse_locale('zh_Hans_CN') |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
608 ('zh', 'CN', 'Hans', None) |
1 | 609 |
610 The default component separator is "_", but a different separator can be | |
611 specified using the `sep` parameter: | |
612 | |
72
4dcdb1d367ec
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
613 >>> parse_locale('zh-CN', sep='-') |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
614 ('zh', 'CN', None, None) |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
615 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
616 If the identifier cannot be parsed into a locale, a `ValueError` exception |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
617 is raised: |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
618 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
619 >>> parse_locale('not_a_LOCALE_String') |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
620 Traceback (most recent call last): |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
621 ... |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
622 ValueError: 'not_a_LOCALE_String' is not a valid locale identifier |
1 | 623 |
624 :param identifier: the locale identifier string | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
625 :param sep: character that separates the different components of the locale |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
626 identifier |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
627 :return: the ``(language, territory, script, variant)`` tuple |
1 | 628 :rtype: `tuple` |
629 :raise `ValueError`: if the string does not appear to be a valid locale | |
630 identifier | |
631 | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
632 :see: `IETF RFC 4646 <http://www.ietf.org/rfc/rfc4646.txt>`_ |
1 | 633 """ |
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
|
634 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
|
635 # 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
|
636 identifier = identifier.split('.', 1)[0] |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
637 |
1 | 638 parts = identifier.split(sep) |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
639 lang = parts.pop(0).lower() |
1 | 640 if not lang.isalpha(): |
641 raise ValueError('expected only letters, got %r' % lang) | |
182
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
642 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
643 script = territory = variant = None |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
644 if parts: |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
645 if len(parts[0]) == 4 and parts[0].isalpha(): |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
646 script = parts.pop(0).title() |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
647 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
648 if parts: |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
649 if len(parts[0]) == 2 and parts[0].isalpha(): |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
650 territory = parts.pop(0).upper() |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
651 elif len(parts[0]) == 3 and parts[0].isdigit(): |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
652 territory = parts.pop(0) |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
653 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
654 if parts: |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
655 if len(parts[0]) == 4 and parts[0][0].isdigit() or \ |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
656 len(parts[0]) >= 5 and parts[0][0].isalpha(): |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
657 variant = parts.pop() |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
658 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
659 if parts: |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
660 raise ValueError('%r is not a valid locale identifier' % identifier) |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
661 |
0db5d8723c76
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
662 return lang, territory, script, variant |