Mercurial > babel > old > mirror
annotate babel/core.py @ 377:841858d5b567
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
author | cmlenz |
---|---|
date | Mon, 07 Jul 2008 14:49:16 +0000 |
parents | c1654337d53b |
children | e8a81ad0f8d3 |
rev | line source |
---|---|
3 | 1 # -*- coding: utf-8 -*- |
2 # | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
3 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
14 """Core locale representation and locale data access.""" |
3 | 15 |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
16 import os |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
17 import pickle |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
18 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
19 from babel import localedata |
3 | 20 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
21 __all__ = ['UnknownLocaleError', 'Locale', 'default_locale', 'negotiate_locale', |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
22 'parse_locale'] |
3 | 23 __docformat__ = 'restructuredtext en' |
24 | |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
25 _global_data = None |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
26 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
27 def get_global(key): |
261 | 28 """Return the dictionary for the given key in the global data. |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
29 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
30 The global data is stored in the ``babel/global.dat`` file and contains |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
31 information independent of individual locales. |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
32 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
33 >>> get_global('zone_aliases')['UTC'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
34 'Etc/GMT' |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
35 >>> get_global('zone_territories')['Europe/Berlin'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
36 'DE' |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
37 |
262 | 38 :param key: the data key |
273 | 39 :return: the dictionary found in the global data under the given key |
261 | 40 :rtype: `dict` |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
41 :since: version 0.9 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
42 """ |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
43 global _global_data |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
44 if _global_data is None: |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
45 dirname = os.path.join(os.path.dirname(__file__)) |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
46 filename = os.path.join(dirname, 'global.dat') |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
47 fileobj = open(filename, 'rb') |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
48 try: |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
49 _global_data = pickle.load(fileobj) |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
50 finally: |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
51 fileobj.close() |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
52 return _global_data.get(key, {}) |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
53 |
261 | 54 LOCALE_ALIASES = { |
55 'ar': 'ar_SY', 'bg': 'bg_BG', 'bs': 'bs_BA', 'ca': 'ca_ES', 'cs': 'cs_CZ', | |
56 'da': 'da_DK', 'de': 'de_DE', 'el': 'el_GR', 'en': 'en_US', 'es': 'es_ES', | |
57 'et': 'et_EE', 'fa': 'fa_IR', 'fi': 'fi_FI', 'fr': 'fr_FR', 'gl': 'gl_ES', | |
58 'he': 'he_IL', 'hu': 'hu_HU', 'id': 'id_ID', 'is': 'is_IS', 'it': 'it_IT', | |
59 'ja': 'ja_JP', 'km': 'km_KH', 'ko': 'ko_KR', 'lt': 'lt_LT', 'lv': 'lv_LV', | |
60 'mk': 'mk_MK', 'nl': 'nl_NL', 'nn': 'nn_NO', 'no': 'nb_NO', 'pl': 'pl_PL', | |
61 'pt': 'pt_PT', 'ro': 'ro_RO', 'ru': 'ru_RU', 'sk': 'sk_SK', 'sl': 'sl_SI', | |
62 'sv': 'sv_SE', 'th': 'th_TH', 'tr': 'tr_TR', 'uk': 'uk_UA' | |
63 } | |
64 | |
3 | 65 |
33 | 66 class UnknownLocaleError(Exception): |
67 """Exception thrown when a locale is requested for which no locale data | |
68 is available. | |
69 """ | |
70 | |
71 def __init__(self, identifier): | |
72 """Create the exception. | |
73 | |
74 :param identifier: the identifier string of the unsupported locale | |
75 """ | |
76 Exception.__init__(self, 'unknown locale %r' % identifier) | |
77 self.identifier = identifier | |
78 | |
79 | |
3 | 80 class Locale(object): |
81 """Representation of a specific locale. | |
82 | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
83 >>> locale = Locale('en', 'US') |
3 | 84 >>> repr(locale) |
85 '<Locale "en_US">' | |
86 >>> locale.display_name | |
87 u'English (United States)' | |
88 | |
89 A `Locale` object can also be instantiated from a raw locale string: | |
90 | |
91 >>> locale = Locale.parse('en-US', sep='-') | |
92 >>> repr(locale) | |
93 '<Locale "en_US">' | |
94 | |
95 `Locale` objects provide access to a collection of locale data, such as | |
96 territory and language names, number and date format patterns, and more: | |
97 | |
98 >>> locale.number_symbols['decimal'] | |
99 u'.' | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
100 |
33 | 101 If a locale is requested for which no locale data is available, an |
102 `UnknownLocaleError` is raised: | |
103 | |
104 >>> Locale.parse('en_DE') | |
105 Traceback (most recent call last): | |
106 ... | |
107 UnknownLocaleError: unknown locale 'en_DE' | |
3 | 108 |
109 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ | |
110 """ | |
111 | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
112 def __init__(self, language, territory=None, script=None, variant=None): |
3 | 113 """Initialize the locale object from the given identifier components. |
114 | |
115 >>> locale = Locale('en', 'US') | |
116 >>> locale.language | |
117 'en' | |
118 >>> locale.territory | |
119 'US' | |
120 | |
121 :param language: the language code | |
122 :param territory: the territory (country or region) code | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
123 :param script: the script code |
3 | 124 :param variant: the variant code |
33 | 125 :raise `UnknownLocaleError`: if no locale data is available for the |
126 requested locale | |
3 | 127 """ |
128 self.language = language | |
129 self.territory = territory | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
130 self.script = script |
3 | 131 self.variant = variant |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
132 self.__data = None |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
133 |
33 | 134 identifier = str(self) |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
135 if not localedata.exists(identifier): |
33 | 136 raise UnknownLocaleError(identifier) |
3 | 137 |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
138 def default(cls, category=None): |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
139 """Return the system default locale for the specified category. |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
140 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
141 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
142 ... os.environ[name] = '' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
143 >>> os.environ['LANG'] = 'fr_FR.UTF-8' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
144 >>> Locale.default('LC_MESSAGES') |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
145 <Locale "fr_FR"> |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
146 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
147 :param category: one of the ``LC_XXX`` environment variable names |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
148 :return: the value of the variable, or any of the fallbacks |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
149 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
150 :rtype: `Locale` |
261 | 151 :see: `default_locale` |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
152 """ |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
153 return cls(default_locale(category)) |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
154 default = classmethod(default) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
155 |
261 | 156 def negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES): |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
157 """Find the best match between available and requested locale strings. |
158 | 158 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
159 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
160 <Locale "de_DE"> |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
161 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
162 <Locale "de"> |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
163 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) |
158 | 164 |
165 You can specify the character used in the locale identifiers to separate | |
166 the differnet components. This separator is applied to both lists. Also, | |
167 case is ignored in the comparison: | |
168 | |
169 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') | |
170 <Locale "de_DE"> | |
171 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
172 :param preferred: the list of locale identifers preferred by the user |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
173 :param available: the list of locale identifiers available |
261 | 174 :param aliases: a dictionary of aliases for locale identifiers |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
175 :return: the `Locale` object for the best match, or `None` if no match |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
176 was found |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
177 :rtype: `Locale` |
261 | 178 :see: `negotiate_locale` |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
179 """ |
261 | 180 identifier = negotiate_locale(preferred, available, sep=sep, |
181 aliases=aliases) | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
182 if identifier: |
158 | 183 return Locale.parse(identifier, sep=sep) |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
184 negotiate = classmethod(negotiate) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
185 |
3 | 186 def parse(cls, identifier, sep='_'): |
187 """Create a `Locale` instance for the given locale identifier. | |
188 | |
189 >>> l = Locale.parse('de-DE', sep='-') | |
190 >>> l.display_name | |
191 u'Deutsch (Deutschland)' | |
192 | |
193 If the `identifier` parameter is not a string, but actually a `Locale` | |
194 object, that object is returned: | |
195 | |
196 >>> Locale.parse(l) | |
197 <Locale "de_DE"> | |
198 | |
199 :param identifier: the locale identifier string | |
200 :param sep: optional component separator | |
201 :return: a corresponding `Locale` instance | |
202 :rtype: `Locale` | |
203 :raise `ValueError`: if the string does not appear to be a valid locale | |
204 identifier | |
33 | 205 :raise `UnknownLocaleError`: if no locale data is available for the |
206 requested locale | |
261 | 207 :see: `parse_locale` |
3 | 208 """ |
283
c1654337d53b
In `Locale.parse()`, only parse the argument if it's a string, otherwise just return the argument. This assumes that a non-string argument is either a `Locale` instance, or a proxy object pretending to be one. That may not be a safe assumption, but at least it allows you to use proxy objects such as Paste's `StackedObjectProxy` for locales.
cmlenz
parents:
273
diff
changeset
|
209 if isinstance(identifier, basestring): |
c1654337d53b
In `Locale.parse()`, only parse the argument if it's a string, otherwise just return the argument. This assumes that a non-string argument is either a `Locale` instance, or a proxy object pretending to be one. That may not be a safe assumption, but at least it allows you to use proxy objects such as Paste's `StackedObjectProxy` for locales.
cmlenz
parents:
273
diff
changeset
|
210 return cls(*parse_locale(identifier, sep=sep)) |
c1654337d53b
In `Locale.parse()`, only parse the argument if it's a string, otherwise just return the argument. This assumes that a non-string argument is either a `Locale` instance, or a proxy object pretending to be one. That may not be a safe assumption, but at least it allows you to use proxy objects such as Paste's `StackedObjectProxy` for locales.
cmlenz
parents:
273
diff
changeset
|
211 return identifier |
3 | 212 parse = classmethod(parse) |
213 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
214 def __eq__(self, other): |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
215 return str(self) == str(other) |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
216 |
3 | 217 def __repr__(self): |
218 return '<Locale "%s">' % str(self) | |
219 | |
220 def __str__(self): | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
221 return '_'.join(filter(None, [self.language, self.script, |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
222 self.territory, self.variant])) |
3 | 223 |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
224 def _data(self): |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
225 if self.__data is None: |
377
841858d5b567
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
283
diff
changeset
|
226 self.__data = localedata.LocaleDataDict(localedata.load(str(self))) |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
227 return self.__data |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
228 _data = property(_data) |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
229 |
187
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
230 def get_display_name(self, locale=None): |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
231 """Return the display name of the locale using the given locale. |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
232 |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
233 The display name will include the language, territory, script, and |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
234 variant, if those are specified. |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
235 |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
236 >>> Locale('zh', 'CN', script='Hans').get_display_name('en') |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
237 u'Chinese (Simplified Han, China)' |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
238 |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
239 :param locale: the locale to use |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
240 :return: the display name |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
241 """ |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
242 if locale is None: |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
243 locale = self |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
244 locale = Locale.parse(locale) |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
245 retval = locale.languages.get(self.language) |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
246 if self.territory or self.script or self.variant: |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
247 details = [] |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
248 if self.script: |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
249 details.append(locale.scripts.get(self.script)) |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
250 if self.territory: |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
251 details.append(locale.territories.get(self.territory)) |
3 | 252 if self.variant: |
187
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
253 details.append(locale.variants.get(self.variant)) |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
254 details = filter(None, details) |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
255 if details: |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
256 retval += ' (%s)' % u', '.join(details) |
3 | 257 return retval |
187
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
258 |
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
259 display_name = property(get_display_name, doc="""\ |
3 | 260 The localized display name of the locale. |
261 | |
262 >>> Locale('en').display_name | |
263 u'English' | |
264 >>> Locale('en', 'US').display_name | |
265 u'English (United States)' | |
43
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
266 >>> Locale('sv').display_name |
0ad88be7da38
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
41
diff
changeset
|
267 u'svenska' |
3 | 268 |
269 :type: `unicode` | |
270 """) | |
271 | |
55 | 272 def english_name(self): |
187
d3bde66ac8a9
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
184
diff
changeset
|
273 return self.get_display_name(Locale('en')) |
55 | 274 english_name = property(english_name, doc="""\ |
275 The english display name of the locale. | |
276 | |
277 >>> Locale('de').english_name | |
278 u'German' | |
279 >>> Locale('de', 'DE').english_name | |
280 u'German (Germany)' | |
281 | |
282 :type: `unicode` | |
283 """) | |
284 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
285 #{ General Locale Display Names |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
286 |
3 | 287 def languages(self): |
288 return self._data['languages'] | |
289 languages = property(languages, doc="""\ | |
290 Mapping of language codes to translated language names. | |
291 | |
292 >>> Locale('de', 'DE').languages['ja'] | |
293 u'Japanisch' | |
294 | |
295 :type: `dict` | |
296 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_ | |
297 """) | |
298 | |
299 def scripts(self): | |
300 return self._data['scripts'] | |
301 scripts = property(scripts, doc="""\ | |
302 Mapping of script codes to translated script names. | |
303 | |
304 >>> Locale('en', 'US').scripts['Hira'] | |
305 u'Hiragana' | |
306 | |
307 :type: `dict` | |
308 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_ | |
309 """) | |
310 | |
311 def territories(self): | |
312 return self._data['territories'] | |
313 territories = property(territories, doc="""\ | |
314 Mapping of script codes to translated script names. | |
315 | |
316 >>> Locale('es', 'CO').territories['DE'] | |
317 u'Alemania' | |
318 | |
319 :type: `dict` | |
320 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_ | |
321 """) | |
322 | |
323 def variants(self): | |
324 return self._data['variants'] | |
325 variants = property(variants, doc="""\ | |
326 Mapping of script codes to translated script names. | |
327 | |
328 >>> Locale('de', 'DE').variants['1901'] | |
377
841858d5b567
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
283
diff
changeset
|
329 u'Alte deutsche Rechtschreibung' |
3 | 330 |
331 :type: `dict` | |
332 """) | |
333 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
334 #{ Number Formatting |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
335 |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
336 def currencies(self): |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
337 return self._data['currency_names'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
338 currencies = property(currencies, doc="""\ |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
339 Mapping of currency codes to translated currency names. |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
340 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
341 >>> Locale('en').currencies['COP'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
342 u'Colombian Peso' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
343 >>> Locale('de', 'DE').currencies['COP'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
344 u'Kolumbianischer Peso' |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
345 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
346 :type: `dict` |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
347 """) |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
348 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
349 def currency_symbols(self): |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
350 return self._data['currency_symbols'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
351 currency_symbols = property(currency_symbols, doc="""\ |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
352 Mapping of currency codes to symbols. |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
353 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
354 >>> Locale('en', 'US').currency_symbols['USD'] |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
355 u'$' |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
356 >>> Locale('es', 'CO').currency_symbols['USD'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
357 u'US$' |
28
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
358 |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
359 :type: `dict` |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
360 """) |
695884591af6
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
24
diff
changeset
|
361 |
3 | 362 def number_symbols(self): |
363 return self._data['number_symbols'] | |
364 number_symbols = property(number_symbols, doc="""\ | |
365 Symbols used in number formatting. | |
366 | |
367 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
368 u',' | |
369 | |
370 :type: `dict` | |
371 """) | |
372 | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
373 def decimal_formats(self): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
374 return self._data['decimal_formats'] |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
375 decimal_formats = property(decimal_formats, doc="""\ |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
376 Locale patterns for decimal number formatting. |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
377 |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
378 >>> Locale('en', 'US').decimal_formats[None] |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
379 <NumberPattern u'#,##0.###'> |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
380 |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
381 :type: `dict` |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
382 """) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
383 |
127 | 384 def currency_formats(self): |
385 return self._data['currency_formats'] | |
386 currency_formats = property(currency_formats, doc=r"""\ | |
387 Locale patterns for currency number formatting. | |
388 | |
389 >>> print Locale('en', 'US').currency_formats[None] | |
390 <NumberPattern u'\xa4#,##0.00'> | |
391 | |
392 :type: `dict` | |
393 """) | |
394 | |
24 | 395 def percent_formats(self): |
396 return self._data['percent_formats'] | |
397 percent_formats = property(percent_formats, doc="""\ | |
398 Locale patterns for percent number formatting. | |
399 | |
400 >>> Locale('en', 'US').percent_formats[None] | |
401 <NumberPattern u'#,##0%'> | |
402 | |
403 :type: `dict` | |
404 """) | |
405 | |
127 | 406 def scientific_formats(self): |
407 return self._data['scientific_formats'] | |
408 scientific_formats = property(scientific_formats, doc="""\ | |
409 Locale patterns for scientific number formatting. | |
410 | |
411 >>> Locale('en', 'US').scientific_formats[None] | |
412 <NumberPattern u'#E0'> | |
413 | |
414 :type: `dict` | |
415 """) | |
416 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
417 #{ Calendar Information and Date Formatting |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
418 |
3 | 419 def periods(self): |
420 return self._data['periods'] | |
421 periods = property(periods, doc="""\ | |
422 Locale display names for day periods (AM/PM). | |
423 | |
424 >>> Locale('en', 'US').periods['am'] | |
425 u'AM' | |
426 | |
427 :type: `dict` | |
428 """) | |
429 | |
430 def days(self): | |
431 return self._data['days'] | |
432 days = property(days, doc="""\ | |
433 Locale display names for weekdays. | |
434 | |
17 | 435 >>> Locale('de', 'DE').days['format']['wide'][3] |
3 | 436 u'Donnerstag' |
437 | |
438 :type: `dict` | |
439 """) | |
440 | |
441 def months(self): | |
442 return self._data['months'] | |
443 months = property(months, doc="""\ | |
444 Locale display names for months. | |
445 | |
446 >>> Locale('de', 'DE').months['format']['wide'][10] | |
447 u'Oktober' | |
448 | |
449 :type: `dict` | |
450 """) | |
451 | |
452 def quarters(self): | |
453 return self._data['quarters'] | |
454 quarters = property(quarters, doc="""\ | |
455 Locale display names for quarters. | |
456 | |
457 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
458 u'1. Quartal' | |
459 | |
460 :type: `dict` | |
461 """) | |
462 | |
463 def eras(self): | |
464 return self._data['eras'] | |
465 eras = property(eras, doc="""\ | |
466 Locale display names for eras. | |
467 | |
468 >>> Locale('en', 'US').eras['wide'][1] | |
469 u'Anno Domini' | |
470 >>> Locale('en', 'US').eras['abbreviated'][0] | |
471 u'BC' | |
472 | |
473 :type: `dict` | |
474 """) | |
475 | |
30
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
476 def time_zones(self): |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
477 return self._data['time_zones'] |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
478 time_zones = property(time_zones, doc="""\ |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
479 Locale display names for time zones. |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
480 |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
481 >>> Locale('en', 'US').time_zones['Europe/London']['long']['daylight'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
482 u'British Summer Time' |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
483 >>> Locale('en', 'US').time_zones['America/St_Johns']['city'] |
377
841858d5b567
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
283
diff
changeset
|
484 u"St. John's" |
30
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
485 |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
486 :type: `dict` |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
487 """) |
9a00ac84004c
Import basic timezone info from CLDR (see #3). Still missing a couple other pieces in the puzzle.
cmlenz
parents:
28
diff
changeset
|
488 |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
489 def meta_zones(self): |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
490 return self._data['meta_zones'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
491 meta_zones = property(meta_zones, doc="""\ |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
492 Locale display names for meta time zones. |
36 | 493 |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
494 Meta time zones are basically groups of different Olson time zones that |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
495 have the same GMT offset and daylight savings time. |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
496 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
497 >>> Locale('en', 'US').meta_zones['Europe_Central']['long']['daylight'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
498 u'Central European Summer Time' |
36 | 499 |
500 :type: `dict` | |
235
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
501 :since: version 0.9 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
502 """) |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
503 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
504 def zone_formats(self): |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
505 return self._data['zone_formats'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
506 zone_formats = property(zone_formats, doc=r"""\ |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
507 Patterns related to the formatting of time zones. |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
508 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
509 >>> Locale('en', 'US').zone_formats['fallback'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
510 u'%(1)s (%(0)s)' |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
511 >>> Locale('pt', 'BR').zone_formats['region'] |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
512 u'Hor\xe1rio %s' |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
513 |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
514 :type: `dict` |
d0cd235ede46
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
187
diff
changeset
|
515 :since: version 0.9 |
36 | 516 """) |
517 | |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
518 def first_week_day(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
519 return self._data['week_data']['first_day'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
520 first_week_day = property(first_week_day, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
521 The first day of a week. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
522 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
523 >>> Locale('de', 'DE').first_week_day |
17 | 524 0 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
525 >>> Locale('en', 'US').first_week_day |
17 | 526 6 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
527 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
528 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
529 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
530 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
531 def weekend_start(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
532 return self._data['week_data']['weekend_start'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
533 weekend_start = property(weekend_start, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
534 The day the weekend starts. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
535 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
536 >>> Locale('de', 'DE').weekend_start |
17 | 537 5 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
538 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
539 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
540 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
541 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
542 def weekend_end(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
543 return self._data['week_data']['weekend_end'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
544 weekend_end = property(weekend_end, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
545 The day the weekend ends. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
546 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
547 >>> Locale('de', 'DE').weekend_end |
17 | 548 6 |
10
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
549 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
550 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
551 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
552 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
553 def min_week_days(self): |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
554 return self._data['week_data']['min_days'] |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
555 min_week_days = property(min_week_days, doc="""\ |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
556 The minimum number of days in a week so that the week is counted as the |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
557 first week of a year or month. |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
558 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
559 >>> Locale('de', 'DE').min_week_days |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
560 4 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
561 |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
562 :type: `int` |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
563 """) |
0ca5dd65594f
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
3
diff
changeset
|
564 |
3 | 565 def date_formats(self): |
566 return self._data['date_formats'] | |
567 date_formats = property(date_formats, doc="""\ | |
568 Locale patterns for date formatting. | |
569 | |
570 >>> Locale('en', 'US').date_formats['short'] | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
571 <DateTimePattern u'M/d/yy'> |
3 | 572 >>> Locale('fr', 'FR').date_formats['long'] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
573 <DateTimePattern u'd MMMM yyyy'> |
3 | 574 |
575 :type: `dict` | |
576 """) | |
577 | |
578 def time_formats(self): | |
579 return self._data['time_formats'] | |
580 time_formats = property(time_formats, doc="""\ | |
581 Locale patterns for time formatting. | |
582 | |
583 >>> Locale('en', 'US').time_formats['short'] | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
584 <DateTimePattern u'h:mm a'> |
3 | 585 >>> Locale('fr', 'FR').time_formats['long'] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
11
diff
changeset
|
586 <DateTimePattern u'HH:mm:ss z'> |
11 | 587 |
588 :type: `dict` | |
589 """) | |
590 | |
35 | 591 def datetime_formats(self): |
592 return self._data['datetime_formats'] | |
593 datetime_formats = property(datetime_formats, doc="""\ | |
594 Locale patterns for datetime formatting. | |
595 | |
596 >>> Locale('en').datetime_formats[None] | |
597 u'{1} {0}' | |
598 >>> Locale('th').datetime_formats[None] | |
599 u'{1}, {0}' | |
600 | |
601 :type: `dict` | |
602 """) | |
603 | |
3 | 604 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
605 def default_locale(category=None): |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
606 """Returns the system default locale for a given category, based on |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
607 environment variables. |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
608 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
609 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
610 ... os.environ[name] = '' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
611 >>> os.environ['LANG'] = 'fr_FR.UTF-8' |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
612 >>> default_locale('LC_MESSAGES') |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
613 'fr_FR' |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
614 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
615 :param category: one of the ``LC_XXX`` environment variable names |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
616 :return: the value of the variable, or any of the fallbacks (``LANGUAGE``, |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
617 ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
618 |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
619 :rtype: `str` |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
620 """ |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
621 varnames = (category, 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG') |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
622 for name in filter(None, varnames): |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
623 locale = os.getenv(name) |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
624 if locale: |
150
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
625 if name == 'LANGUAGE' and ':' in locale: |
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
626 # the LANGUAGE variable may contain a colon-separated list of |
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
627 # language codes; we just pick the language on the list |
1662e1a4fc7b
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
127
diff
changeset
|
628 locale = locale.split(':')[0] |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
629 return '_'.join(filter(None, parse_locale(locale))) |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
630 |
261 | 631 def negotiate_locale(preferred, available, sep='_', aliases=LOCALE_ALIASES): |
3 | 632 """Find the best match between available and requested locale strings. |
633 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
634 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
3 | 635 'de_DE' |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
636 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) |
3 | 637 'de' |
638 | |
158 | 639 Case is ignored by the algorithm, the result uses the case of the preferred |
640 locale identifier: | |
641 | |
642 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) | |
643 'de_DE' | |
644 | |
261 | 645 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) |
646 'de_DE' | |
647 | |
648 By default, some web browsers unfortunately do not include the territory | |
649 in the locale identifier for many locales, and some don't even allow the | |
650 user to easily add the territory. So while you may prefer using qualified | |
651 locale identifiers in your web-application, they would not normally match | |
652 the language-only locale sent by such browsers. To workaround that, this | |
653 function uses a default mapping of commonly used langauge-only locale | |
654 identifiers to identifiers including the territory: | |
655 | |
656 >>> negotiate_locale(['ja', 'en_US'], ['ja_JP', 'en_US']) | |
657 'ja_JP' | |
658 | |
659 Some browsers even use an incorrect or outdated language code, such as "no" | |
660 for Norwegian, where the correct locale identifier would actually be "nb_NO" | |
661 (Bokmål) or "nn_NO" (Nynorsk). The aliases are intended to take care of | |
662 such cases, too: | |
663 | |
664 >>> negotiate_locale(['no', 'sv'], ['nb_NO', 'sv_SE']) | |
665 'nb_NO' | |
666 | |
667 You can override this default mapping by passing a different `aliases` | |
668 dictionary to this function, or you can bypass the behavior althogher by | |
669 setting the `aliases` parameter to `None`. | |
670 | |
3 | 671 :param preferred: the list of locale strings preferred by the user |
672 :param available: the list of locale strings available | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
673 :param sep: character that separates the different parts of the locale |
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
674 strings |
261 | 675 :param aliases: a dictionary of aliases for locale identifiers |
3 | 676 :return: the locale identifier for the best match, or `None` if no match |
677 was found | |
678 :rtype: `str` | |
679 """ | |
158 | 680 available = [a.lower() for a in available if a] |
3 | 681 for locale in preferred: |
261 | 682 ll = locale.lower() |
683 if ll in available: | |
3 | 684 return locale |
261 | 685 if aliases: |
686 alias = aliases.get(ll) | |
687 if alias: | |
688 alias = alias.replace('_', sep) | |
689 if alias.lower() in available: | |
690 return alias | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
691 parts = locale.split(sep) |
158 | 692 if len(parts) > 1 and parts[0].lower() in available: |
3 | 693 return parts[0] |
694 return None | |
695 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
696 def parse_locale(identifier, sep='_'): |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
697 """Parse a locale identifier into a tuple of the form:: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
698 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
699 ``(language, territory, script, variant)`` |
3 | 700 |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
701 >>> parse_locale('zh_CN') |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
702 ('zh', 'CN', None, None) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
703 >>> parse_locale('zh_Hans_CN') |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
704 ('zh', 'CN', 'Hans', None) |
3 | 705 |
706 The default component separator is "_", but a different separator can be | |
707 specified using the `sep` parameter: | |
708 | |
74
d9c34d2f3d1d
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
55
diff
changeset
|
709 >>> parse_locale('zh-CN', sep='-') |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
710 ('zh', 'CN', None, None) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
711 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
712 If the identifier cannot be parsed into a locale, a `ValueError` exception |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
713 is raised: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
714 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
715 >>> parse_locale('not_a_LOCALE_String') |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
716 Traceback (most recent call last): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
717 ... |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
718 ValueError: 'not_a_LOCALE_String' is not a valid locale identifier |
3 | 719 |
720 :param identifier: the locale identifier string | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
721 :param sep: character that separates the different components of the locale |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
722 identifier |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
723 :return: the ``(language, territory, script, variant)`` tuple |
3 | 724 :rtype: `tuple` |
725 :raise `ValueError`: if the string does not appear to be a valid locale | |
726 identifier | |
727 | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
728 :see: `IETF RFC 4646 <http://www.ietf.org/rfc/rfc4646.txt>`_ |
3 | 729 """ |
41
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
730 if '.' in identifier: |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
731 # this is probably the charset/encoding, which we don't care about |
359ec55de578
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
36
diff
changeset
|
732 identifier = identifier.split('.', 1)[0] |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
733 |
3 | 734 parts = identifier.split(sep) |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
735 lang = parts.pop(0).lower() |
3 | 736 if not lang.isalpha(): |
737 raise ValueError('expected only letters, got %r' % lang) | |
184
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
738 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
739 script = territory = variant = None |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
740 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
741 if len(parts[0]) == 4 and parts[0].isalpha(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
742 script = parts.pop(0).title() |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
743 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
744 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
745 if len(parts[0]) == 2 and parts[0].isalpha(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
746 territory = parts.pop(0).upper() |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
747 elif len(parts[0]) == 3 and parts[0].isdigit(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
748 territory = parts.pop(0) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
749 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
750 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
751 if len(parts[0]) == 4 and parts[0][0].isdigit() or \ |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
752 len(parts[0]) >= 5 and parts[0][0].isalpha(): |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
753 variant = parts.pop() |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
754 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
755 if parts: |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
756 raise ValueError('%r is not a valid locale identifier' % identifier) |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
757 |
170c99195460
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
158
diff
changeset
|
758 return lang, territory, script, variant |