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