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