Mercurial > babel > mirror
annotate babel/core.py @ 592:e6b1efa9a255 trunk
fix Locale.default to return Locales with correct territory information
author | fschwarz |
---|---|
date | Thu, 09 Aug 2012 07:47:09 +0000 |
parents | d1618dfaf114 |
children |
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) |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
86 "Locale('en', territory='US')" |
1 | 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) | |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
94 "Locale('en', territory='US')" |
1 | 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 |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
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') |
592
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
147 Locale('fr', territory='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 """ |
592
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
156 locale_string = default_locale(category, aliases=aliases) |
e6b1efa9a255
fix Locale.default to return Locales with correct territory information
fschwarz
parents:
591
diff
changeset
|
157 return cls.parse(locale_string) |
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
|
158 |
579 | 159 @classmethod |
259 | 160 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
|
161 """Find the best match between available and requested locale strings. |
156 | 162 |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
163 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
164 Locale('de', territory='DE') |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
165 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
166 Locale('de') |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
167 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) |
156 | 168 |
169 You can specify the character used in the locale identifiers to separate | |
170 the differnet components. This separator is applied to both lists. Also, | |
171 case is ignored in the comparison: | |
172 | |
173 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') | |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
174 Locale('de', territory='DE') |
156 | 175 |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
176 :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
|
177 :param available: the list of locale identifiers available |
259 | 178 :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
|
179 :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
|
180 was found |
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
181 :rtype: `Locale` |
259 | 182 :see: `negotiate_locale` |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
183 """ |
259 | 184 identifier = negotiate_locale(preferred, available, sep=sep, |
185 aliases=aliases) | |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
186 if identifier: |
156 | 187 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
|
188 |
579 | 189 @classmethod |
1 | 190 def parse(cls, identifier, sep='_'): |
191 """Create a `Locale` instance for the given locale identifier. | |
192 | |
193 >>> l = Locale.parse('de-DE', sep='-') | |
194 >>> l.display_name | |
195 u'Deutsch (Deutschland)' | |
196 | |
197 If the `identifier` parameter is not a string, but actually a `Locale` | |
198 object, that object is returned: | |
199 | |
200 >>> Locale.parse(l) | |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
201 Locale('de', territory='DE') |
1 | 202 |
203 :param identifier: the locale identifier string | |
204 :param sep: optional component separator | |
205 :return: a corresponding `Locale` instance | |
206 :rtype: `Locale` | |
207 :raise `ValueError`: if the string does not appear to be a valid locale | |
208 identifier | |
31 | 209 :raise `UnknownLocaleError`: if no locale data is available for the |
210 requested locale | |
259 | 211 :see: `parse_locale` |
1 | 212 """ |
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
|
213 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
|
214 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
|
215 return identifier |
1 | 216 |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
217 def __eq__(self, other): |
591
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
218 for key in ('language', 'territory', 'script', 'variant'): |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
219 if not hasattr(other, key): |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
220 return False |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
221 return (self.language == other.language) and \ |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
222 (self.territory == other.territory) and \ |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
223 (self.script == other.script) and \ |
d1618dfaf114
change Locale comparison: Locales are now considered equal if all of their attributes (language, territory, script, variant) are equal. Before __eq__ used the simple string representation which hides errors in Locale instantiation (see #279 and #311 for more information).
fschwarz
parents:
590
diff
changeset
|
224 (self.variant == other.variant) |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
225 |
432 | 226 def __ne__(self, other): |
227 return not self.__eq__(other) | |
228 | |
1 | 229 def __repr__(self): |
590
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
230 parameters = [''] |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
231 for key in ('territory', 'script', 'variant'): |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
232 value = getattr(self, key) |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
233 if value is not None: |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
234 parameters.append('%s=%r' % (key, value)) |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
235 parameter_string = '%r' % self.language + ', '.join(parameters) |
6f86d60dab56
change repr output for babel.Locale so all attributes (territory, script, variant) are shown by itself. This should help identifying bad usage of the Locale class (e.g. Locale('de_DE') instead of Locale('de', 'DE'), #279
fschwarz
parents:
579
diff
changeset
|
236 return 'Locale(%s)' % parameter_string |
1 | 237 |
238 def __str__(self): | |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
239 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
|
240 self.territory, self.variant])) |
1 | 241 |
579 | 242 @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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
248 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
|
249 """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
|
250 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
251 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
|
252 variant, if those are specified. |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
253 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
254 >>> 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
|
255 u'Chinese (Simplified Han, China)' |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
256 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
257 :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
|
258 :return: the display name |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
259 """ |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
260 if locale is None: |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
261 locale = self |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
262 locale = Locale.parse(locale) |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
263 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
|
264 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
|
265 details = [] |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
266 if self.script: |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
267 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
|
268 if self.territory: |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
269 details.append(locale.territories.get(self.territory)) |
1 | 270 if self.variant: |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
271 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
|
272 details = filter(None, details) |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
273 if details: |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
274 retval += ' (%s)' % u', '.join(details) |
1 | 275 return retval |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
276 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
182
diff
changeset
|
277 display_name = property(get_display_name, doc="""\ |
1 | 278 The localized display name of the locale. |
279 | |
280 >>> Locale('en').display_name | |
281 u'English' | |
282 >>> Locale('en', 'US').display_name | |
283 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
|
284 >>> 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
|
285 u'svenska' |
1 | 286 |
287 :type: `unicode` | |
288 """) | |
289 | |
579 | 290 @property |
53 | 291 def english_name(self): |
579 | 292 """The english display name of the locale. |
53 | 293 |
294 >>> Locale('de').english_name | |
295 u'German' | |
296 >>> Locale('de', 'DE').english_name | |
297 u'German (Germany)' | |
298 | |
579 | 299 :type: `unicode`""" |
300 return self.get_display_name(Locale('en')) | |
53 | 301 |
8
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
302 #{ 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
|
303 |
579 | 304 @property |
1 | 305 def languages(self): |
579 | 306 """Mapping of language codes to translated language names. |
1 | 307 |
308 >>> Locale('de', 'DE').languages['ja'] | |
309 u'Japanisch' | |
310 | |
311 :type: `dict` | |
579 | 312 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_""" |
313 return self._data['languages'] | |
1 | 314 |
579 | 315 @property |
1 | 316 def scripts(self): |
579 | 317 """Mapping of script codes to translated script names. |
1 | 318 |
319 >>> Locale('en', 'US').scripts['Hira'] | |
320 u'Hiragana' | |
321 | |
322 :type: `dict` | |
579 | 323 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_""" |
324 return self._data['scripts'] | |
1 | 325 |
579 | 326 @property |
1 | 327 def territories(self): |
579 | 328 """Mapping of script codes to translated script names. |
1 | 329 |
330 >>> Locale('es', 'CO').territories['DE'] | |
331 u'Alemania' | |
332 | |
333 :type: `dict` | |
579 | 334 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_""" |
335 return self._data['territories'] | |
1 | 336 |
579 | 337 @property |
1 | 338 def variants(self): |
579 | 339 """Mapping of script codes to translated script names. |
1 | 340 |
341 >>> 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
|
342 u'Alte deutsche Rechtschreibung' |
1 | 343 |
579 | 344 :type: `dict`""" |
345 return self._data['variants'] | |
1 | 346 |
8
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
347 #{ 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
|
348 |
579 | 349 @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
|
350 def currencies(self): |
579 | 351 """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
|
352 |
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 >>> 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
|
354 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
|
355 >>> 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
|
356 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
|
357 |
579 | 358 :type: `dict`""" |
359 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
|
360 |
579 | 361 @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
|
362 def currency_symbols(self): |
579 | 363 """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
|
364 |
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
|
365 >>> 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
|
366 u'$' |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
367 >>> Locale('es', 'CO').currency_symbols['USD'] |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
368 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
|
369 |
579 | 370 :type: `dict`""" |
371 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
|
372 |
579 | 373 @property |
1 | 374 def number_symbols(self): |
579 | 375 """Symbols used in number formatting. |
1 | 376 |
377 >>> Locale('fr', 'FR').number_symbols['decimal'] | |
378 u',' | |
379 | |
579 | 380 :type: `dict`""" |
381 return self._data['number_symbols'] | |
1 | 382 |
579 | 383 @property |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
384 def decimal_formats(self): |
579 | 385 """Locale patterns for decimal number formatting. |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
386 |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
387 >>> Locale('en', 'US').decimal_formats[None] |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
388 <NumberPattern u'#,##0.###'> |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
389 |
579 | 390 :type: `dict`""" |
391 return self._data['decimal_formats'] | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
392 |
579 | 393 @property |
125 | 394 def currency_formats(self): |
579 | 395 """Locale patterns for currency number formatting. |
125 | 396 |
397 >>> print Locale('en', 'US').currency_formats[None] | |
579 | 398 <NumberPattern u'\\xa4#,##0.00'> |
125 | 399 |
579 | 400 :type: `dict`""" |
401 return self._data['currency_formats'] | |
125 | 402 |
579 | 403 @property |
22 | 404 def percent_formats(self): |
579 | 405 """Locale patterns for percent number formatting. |
22 | 406 |
407 >>> Locale('en', 'US').percent_formats[None] | |
408 <NumberPattern u'#,##0%'> | |
409 | |
579 | 410 :type: `dict`""" |
411 return self._data['percent_formats'] | |
22 | 412 |
579 | 413 @property |
125 | 414 def scientific_formats(self): |
579 | 415 """Locale patterns for scientific number formatting. |
125 | 416 |
417 >>> Locale('en', 'US').scientific_formats[None] | |
418 <NumberPattern u'#E0'> | |
419 | |
579 | 420 :type: `dict`""" |
421 return self._data['scientific_formats'] | |
125 | 422 |
8
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
423 #{ 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
|
424 |
579 | 425 @property |
1 | 426 def periods(self): |
579 | 427 """Locale display names for day periods (AM/PM). |
1 | 428 |
429 >>> Locale('en', 'US').periods['am'] | |
430 u'AM' | |
431 | |
579 | 432 :type: `dict`""" |
433 return self._data['periods'] | |
1 | 434 |
579 | 435 @property |
1 | 436 def days(self): |
579 | 437 """Locale display names for weekdays. |
1 | 438 |
15 | 439 >>> Locale('de', 'DE').days['format']['wide'][3] |
1 | 440 u'Donnerstag' |
441 | |
579 | 442 :type: `dict`""" |
443 return self._data['days'] | |
1 | 444 |
579 | 445 @property |
1 | 446 def months(self): |
579 | 447 """Locale display names for months. |
1 | 448 |
449 >>> Locale('de', 'DE').months['format']['wide'][10] | |
450 u'Oktober' | |
451 | |
579 | 452 :type: `dict`""" |
453 return self._data['months'] | |
1 | 454 |
579 | 455 @property |
1 | 456 def quarters(self): |
579 | 457 """Locale display names for quarters. |
1 | 458 |
459 >>> Locale('de', 'DE').quarters['format']['wide'][1] | |
460 u'1. Quartal' | |
461 | |
579 | 462 :type: `dict`""" |
463 return self._data['quarters'] | |
1 | 464 |
579 | 465 @property |
1 | 466 def eras(self): |
579 | 467 """Locale display names for eras. |
1 | 468 |
469 >>> Locale('en', 'US').eras['wide'][1] | |
470 u'Anno Domini' | |
471 >>> Locale('en', 'US').eras['abbreviated'][0] | |
472 u'BC' | |
473 | |
579 | 474 :type: `dict`""" |
475 return self._data['eras'] | |
1 | 476 |
579 | 477 @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
|
478 def time_zones(self): |
579 | 479 """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
|
480 |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
481 >>> Locale('en', 'US').time_zones['Europe/London']['long']['daylight'] |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
482 u'British Summer Time' |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
483 >>> 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
|
484 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
|
485 |
579 | 486 :type: `dict`""" |
487 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
|
488 |
579 | 489 @property |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
490 def meta_zones(self): |
579 | 491 """Locale display names for meta time zones. |
34 | 492 |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
493 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
|
494 have the same GMT offset and daylight savings time. |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
495 |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
496 >>> Locale('en', 'US').meta_zones['Europe_Central']['long']['daylight'] |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
497 u'Central European Summer Time' |
34 | 498 |
499 :type: `dict` | |
579 | 500 :since: version 0.9""" |
501 return self._data['meta_zones'] | |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
502 |
579 | 503 @property |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
504 def zone_formats(self): |
579 | 505 """Patterns related to the formatting of time zones. |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
506 |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
507 >>> Locale('en', 'US').zone_formats['fallback'] |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
508 u'%(1)s (%(0)s)' |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
509 >>> Locale('pt', 'BR').zone_formats['region'] |
579 | 510 u'Hor\\xe1rio %s' |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
511 |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
185
diff
changeset
|
512 :type: `dict` |
579 | 513 :since: version 0.9""" |
514 return self._data['zone_formats'] | |
34 | 515 |
579 | 516 @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
|
517 def first_week_day(self): |
579 | 518 """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
|
519 |
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
520 >>> Locale('de', 'DE').first_week_day |
15 | 521 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
|
522 >>> Locale('en', 'US').first_week_day |
15 | 523 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
|
524 |
579 | 525 :type: `int`""" |
526 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
|
527 |
579 | 528 @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
|
529 def weekend_start(self): |
579 | 530 """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
|
531 |
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
532 >>> Locale('de', 'DE').weekend_start |
15 | 533 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
|
534 |
579 | 535 :type: `int`""" |
536 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
|
537 |
579 | 538 @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
|
539 def weekend_end(self): |
579 | 540 """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
|
541 |
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
542 >>> Locale('de', 'DE').weekend_end |
15 | 543 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
|
544 |
579 | 545 :type: `int`""" |
546 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
|
547 |
579 | 548 @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
|
549 def min_week_days(self): |
579 | 550 """The minimum number of days in a week so that the week is counted as |
551 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
|
552 |
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
553 >>> 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
|
554 4 |
29f6f9a90f14
Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents:
1
diff
changeset
|
555 |
579 | 556 :type: `int`""" |
557 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
|
558 |
579 | 559 @property |
1 | 560 def date_formats(self): |
579 | 561 """Locale patterns for date formatting. |
1 | 562 |
563 >>> Locale('en', 'US').date_formats['short'] | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
564 <DateTimePattern u'M/d/yy'> |
1 | 565 >>> Locale('fr', 'FR').date_formats['long'] |
430 | 566 <DateTimePattern u'd MMMM y'> |
1 | 567 |
579 | 568 :type: `dict`""" |
569 return self._data['date_formats'] | |
1 | 570 |
579 | 571 @property |
1 | 572 def time_formats(self): |
579 | 573 """Locale patterns for time formatting. |
1 | 574 |
575 >>> Locale('en', 'US').time_formats['short'] | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
576 <DateTimePattern u'h:mm a'> |
1 | 577 >>> Locale('fr', 'FR').time_formats['long'] |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
9
diff
changeset
|
578 <DateTimePattern u'HH:mm:ss z'> |
9 | 579 |
579 | 580 :type: `dict`""" |
581 return self._data['time_formats'] | |
9 | 582 |
579 | 583 @property |
33 | 584 def datetime_formats(self): |
579 | 585 """Locale patterns for datetime formatting. |
33 | 586 |
439
3961415e8743
Current CDLR has implemented the full type specifiers on the dateTimeFormats,
jruigrok
parents:
432
diff
changeset
|
587 >>> Locale('en').datetime_formats['full'] |
33 | 588 u'{1} {0}' |
439
3961415e8743
Current CDLR has implemented the full type specifiers on the dateTimeFormats,
jruigrok
parents:
432
diff
changeset
|
589 >>> Locale('th').datetime_formats['medium'] |
33 | 590 u'{1}, {0}' |
591 | |
579 | 592 :type: `dict`""" |
593 return self._data['datetime_formats'] | |
33 | 594 |
579 | 595 @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
|
596 def plural_form(self): |
579 | 597 """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
|
598 |
c5bc0f6822a9
Preliminary support for timedelta formatting (see #126), and import/expose the locale plural rules from the CLDR.
cmlenz
parents:
384
diff
changeset
|
599 >>> 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
|
600 'one' |
c5bc0f6822a9
Preliminary support for timedelta formatting (see #126), and import/expose the locale plural rules from the CLDR.
cmlenz
parents:
384
diff
changeset
|
601 >>> 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
|
602 'other' |
c5bc0f6822a9
Preliminary support for timedelta formatting (see #126), and import/expose the locale plural rules from the CLDR.
cmlenz
parents:
384
diff
changeset
|
603 >>> 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
|
604 'one' |
394
0ca28858045e
Doc improvements for the new `format_timedelta` function.
cmlenz
parents:
390
diff
changeset
|
605 >>> Locale('ru').plural_form(100) |
0ca28858045e
Doc improvements for the new `format_timedelta` function.
cmlenz
parents:
390
diff
changeset
|
606 '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
|
607 |
579 | 608 :type: `PluralRule`""" |
609 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
|
610 |
1 | 611 |
403
3ded94255d9a
Strip locale modifiers when parsing (#136), and treat 'C' and 'POSIX' as aliases for 'en_US_POSIX'.
cmlenz
parents:
394
diff
changeset
|
612 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
|
613 """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
|
614 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
|
615 |
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 >>> 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
|
617 ... 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
|
618 >>> 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
|
619 >>> 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
|
620 '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
|
621 |
3ded94255d9a
Strip locale modifiers when parsing (#136), and treat 'C' and 'POSIX' as aliases for 'en_US_POSIX'.
cmlenz
parents:
394
diff
changeset
|
622 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
|
623 "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
|
624 |
3ded94255d9a
Strip locale modifiers when parsing (#136), and treat 'C' and 'POSIX' as aliases for 'en_US_POSIX'.
cmlenz
parents:
394
diff
changeset
|
625 >>> 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
|
626 >>> 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
|
627 '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
|
628 |
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
|
629 :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
|
630 :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
|
631 :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
|
632 ``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
|
633 :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
|
634 """ |
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
|
635 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
|
636 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
|
637 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
|
638 if locale: |
148
6bb1026756f2
The `LANGUAGE` environment variable may contain a colon-separated list of language codes.
cmlenz
parents:
125
diff
changeset
|
639 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
|
640 # 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
|
641 # 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
|
642 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
|
643 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
|
644 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
|
645 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
|
646 locale = aliases[locale] |
534
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
647 try: |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
648 return '_'.join(filter(None, parse_locale(locale))) |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
649 except ValueError: |
c938dbfb8944
catch exception if environment contains an invalid locale setting (fixes #200)
fschwarz
parents:
530
diff
changeset
|
650 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
|
651 |
259 | 652 def negotiate_locale(preferred, available, sep='_', aliases=LOCALE_ALIASES): |
1 | 653 """Find the best match between available and requested locale strings. |
654 | |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
655 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
1 | 656 'de_DE' |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
657 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) |
1 | 658 'de' |
659 | |
156 | 660 Case is ignored by the algorithm, the result uses the case of the preferred |
661 locale identifier: | |
662 | |
663 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) | |
664 'de_DE' | |
665 | |
259 | 666 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) |
667 'de_DE' | |
668 | |
669 By default, some web browsers unfortunately do not include the territory | |
670 in the locale identifier for many locales, and some don't even allow the | |
671 user to easily add the territory. So while you may prefer using qualified | |
672 locale identifiers in your web-application, they would not normally match | |
673 the language-only locale sent by such browsers. To workaround that, this | |
674 function uses a default mapping of commonly used langauge-only locale | |
675 identifiers to identifiers including the territory: | |
676 | |
677 >>> negotiate_locale(['ja', 'en_US'], ['ja_JP', 'en_US']) | |
678 'ja_JP' | |
679 | |
680 Some browsers even use an incorrect or outdated language code, such as "no" | |
681 for Norwegian, where the correct locale identifier would actually be "nb_NO" | |
682 (Bokmål) or "nn_NO" (Nynorsk). The aliases are intended to take care of | |
683 such cases, too: | |
684 | |
685 >>> negotiate_locale(['no', 'sv'], ['nb_NO', 'sv_SE']) | |
686 'nb_NO' | |
687 | |
688 You can override this default mapping by passing a different `aliases` | |
689 dictionary to this function, or you can bypass the behavior althogher by | |
690 setting the `aliases` parameter to `None`. | |
691 | |
1 | 692 :param preferred: the list of locale strings preferred by the user |
693 :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
|
694 :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
|
695 strings |
259 | 696 :param aliases: a dictionary of aliases for locale identifiers |
1 | 697 :return: the locale identifier for the best match, or `None` if no match |
698 was found | |
699 :rtype: `str` | |
700 """ | |
156 | 701 available = [a.lower() for a in available if a] |
1 | 702 for locale in preferred: |
259 | 703 ll = locale.lower() |
704 if ll in available: | |
1 | 705 return locale |
259 | 706 if aliases: |
707 alias = aliases.get(ll) | |
708 if alias: | |
709 alias = alias.replace('_', sep) | |
710 if alias.lower() in available: | |
711 return alias | |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
712 parts = locale.split(sep) |
156 | 713 if len(parts) > 1 and parts[0].lower() in available: |
1 | 714 return parts[0] |
715 return None | |
716 | |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
717 def parse_locale(identifier, sep='_'): |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
718 """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
|
719 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
720 ``(language, territory, script, variant)`` |
1 | 721 |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
722 >>> parse_locale('zh_CN') |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
723 ('zh', 'CN', None, None) |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
724 >>> parse_locale('zh_Hans_CN') |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
725 ('zh', 'CN', 'Hans', None) |
1 | 726 |
727 The default component separator is "_", but a different separator can be | |
728 specified using the `sep` parameter: | |
729 | |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
53
diff
changeset
|
730 >>> parse_locale('zh-CN', sep='-') |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
731 ('zh', 'CN', None, None) |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
732 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
733 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
|
734 is raised: |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
735 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
736 >>> parse_locale('not_a_LOCALE_String') |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
737 Traceback (most recent call last): |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
738 ... |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
739 ValueError: 'not_a_LOCALE_String' is not a valid locale identifier |
1 | 740 |
403
3ded94255d9a
Strip locale modifiers when parsing (#136), and treat 'C' and 'POSIX' as aliases for 'en_US_POSIX'.
cmlenz
parents:
394
diff
changeset
|
741 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
|
742 |
3ded94255d9a
Strip locale modifiers when parsing (#136), and treat 'C' and 'POSIX' as aliases for 'en_US_POSIX'.
cmlenz
parents:
394
diff
changeset
|
743 >>> 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
|
744 ('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
|
745 >>> 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
|
746 ('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
|
747 >>> 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
|
748 ('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
|
749 |
1 | 750 :param identifier: the locale identifier string |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
751 :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
|
752 identifier |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
753 :return: the ``(language, territory, script, variant)`` tuple |
1 | 754 :rtype: `tuple` |
755 :raise `ValueError`: if the string does not appear to be a valid locale | |
756 identifier | |
757 | |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
758 :see: `IETF RFC 4646 <http://www.ietf.org/rfc/rfc4646.txt>`_ |
1 | 759 """ |
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
|
760 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
|
761 # 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
|
762 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
|
763 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
|
764 # 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
|
765 # either |
3ded94255d9a
Strip locale modifiers when parsing (#136), and treat 'C' and 'POSIX' as aliases for 'en_US_POSIX'.
cmlenz
parents:
394
diff
changeset
|
766 identifier = identifier.split('@', 1)[0] |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
767 |
1 | 768 parts = identifier.split(sep) |
182
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
769 lang = parts.pop(0).lower() |
1 | 770 if not lang.isalpha(): |
771 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
|
772 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
773 script = territory = variant = None |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
774 if parts: |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
775 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
|
776 script = parts.pop(0).title() |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
777 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
778 if parts: |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
779 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
|
780 territory = parts.pop(0).upper() |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
781 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
|
782 territory = parts.pop(0) |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
783 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
784 if parts: |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
785 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
|
786 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
|
787 variant = parts.pop() |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
788 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
789 if parts: |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
790 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
|
791 |
9da358020629
More robust locale string parsing, with support for scripts. Closes #27.
cmlenz
parents:
156
diff
changeset
|
792 return lang, territory, script, variant |