annotate babel/core.py @ 184:170c99195460

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