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