annotate babel/core.py @ 579:99d51589c822 trunk

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