annotate babel/core.py @ 535:2cfe20fd80a0 stable-0.9.x

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