annotate babel/core.py @ 11:11f64b232b04

Add basic support for number format patterns.
author jonas
date Wed, 30 May 2007 21:30:14 +0000
parents 0ca5dd65594f
children 29ef15a6fd75
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 #
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006 Edgewall Software
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
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
14 """Core locale representation and locale data access gateway."""
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
15
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
16 import pickle
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
17 from pkg_resources import resource_filename
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
18 try:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
19 import threading
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
20 except ImportError:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
21 import dummy_threading as threading
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
22
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
23 __all__ = ['Locale', 'negotiate', 'parse']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
24 __docformat__ = 'restructuredtext en'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
25
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
26
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
27 class Locale(object):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
28 """Representation of a specific locale.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
29
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
30 >>> locale = Locale('en', territory='US')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
31 >>> repr(locale)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
32 '<Locale "en_US">'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
33 >>> locale.display_name
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
34 u'English (United States)'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
35
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
36 A `Locale` object can also be instantiated from a raw locale string:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
37
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
38 >>> locale = Locale.parse('en-US', sep='-')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
39 >>> repr(locale)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
40 '<Locale "en_US">'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
41
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
42 `Locale` objects provide access to a collection of locale data, such as
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
43 territory and language names, number and date format patterns, and more:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
44
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
45 >>> locale.number_symbols['decimal']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
46 u'.'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
47
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
48 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
49 """
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
50 _cache = {}
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
51 _cache_lock = threading.Lock()
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
52
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
53 def __new__(cls, language, territory=None, variant=None):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
54 """Create new locale object, or load it from the cache if it had already
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
55 been instantiated.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
56
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
57 >>> l1 = Locale('en')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
58 >>> l2 = Locale('en')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
59 >>> l1 is l2
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
60 True
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
61
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
62 :param language: the language code
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
63 :param territory: the territory (country or region) code
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
64 :param variant: the variant code
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
65 :return: new or existing `Locale` instance
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
66 :rtype: `Locale`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
67 """
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
68 key = (language, territory, variant)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
69 cls._cache_lock.acquire()
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
70 try:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
71 self = cls._cache.get(key)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
72 if self is None:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
73 self = super(Locale, cls).__new__(cls, language, territory,
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
74 variant)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
75 cls._cache[key] = self
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
76 return self
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
77 finally:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
78 self._cache_lock.release()
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
79
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
80 def __init__(self, language, territory=None, variant=None):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
81 """Initialize the locale object from the given identifier components.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
82
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
83 >>> locale = Locale('en', 'US')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
84 >>> locale.language
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
85 'en'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
86 >>> locale.territory
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
87 'US'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
88
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
89 :param language: the language code
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
90 :param territory: the territory (country or region) code
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
91 :param variant: the variant code
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
92 """
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
93 self.language = language
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
94 self.territory = territory
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
95 self.variant = variant
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
96 self.__data = None
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
97
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
98 def parse(cls, identifier, sep='_'):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
99 """Create a `Locale` instance for the given locale identifier.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
100
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
101 >>> l = Locale.parse('de-DE', sep='-')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
102 >>> l.display_name
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
103 u'Deutsch (Deutschland)'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
104
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
105 If the `identifier` parameter is not a string, but actually a `Locale`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
106 object, that object is returned:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
107
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
108 >>> Locale.parse(l)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
109 <Locale "de_DE">
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
110
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
111 :param identifier: the locale identifier string
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
112 :param sep: optional component separator
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
113 :return: a corresponding `Locale` instance
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
114 :rtype: `Locale`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
115 :raise `ValueError`: if the string does not appear to be a valid locale
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
116 identifier
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
117 """
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
118 if type(identifier) is cls:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
119 return identifier
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
120 return cls(*parse(identifier, sep=sep))
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
121 parse = classmethod(parse)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
122
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
123 def __repr__(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
124 return '<Locale "%s">' % str(self)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
125
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
126 def __str__(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
127 return '_'.join(filter(None, [self.language, self.territory,
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
128 self.variant]))
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
129
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
130 def _data(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
131 if self.__data is None:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
132 filename = resource_filename(__name__, 'localedata/%s.dat' % self)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
133 fileobj = open(filename, 'rb')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
134 try:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
135 self.__data = pickle.load(fileobj)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
136 finally:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
137 fileobj.close()
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
138 return self.__data
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
139 _data = property(_data)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
140
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
141 def display_name(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
142 retval = self.languages.get(self.language)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
143 if self.territory:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
144 variant = ''
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
145 if self.variant:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
146 variant = ', %s' % self.variants.get(self.variant)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
147 retval += ' (%s%s)' % (self.territories.get(self.territory), variant)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
148 return retval
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
149 display_name = property(display_name, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
150 The localized display name of the locale.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
151
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
152 >>> Locale('en').display_name
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
153 u'English'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
154 >>> Locale('en', 'US').display_name
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
155 u'English (United States)'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
156
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
157 :type: `unicode`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
158 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
159
10
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
160 #{ 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
161
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
162 def languages(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
163 return self._data['languages']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
164 languages = property(languages, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
165 Mapping of language codes to translated language names.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
166
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
167 >>> Locale('de', 'DE').languages['ja']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
168 u'Japanisch'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
169
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
170 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
171 :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
172 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
173
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
174 def scripts(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
175 return self._data['scripts']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
176 scripts = property(scripts, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
177 Mapping of script codes to translated script names.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
178
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
179 >>> Locale('en', 'US').scripts['Hira']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
180 u'Hiragana'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
181
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
182 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
183 :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
184 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
185
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
186 def territories(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
187 return self._data['territories']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
188 territories = property(territories, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
189 Mapping of script codes to translated script names.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
190
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
191 >>> Locale('es', 'CO').territories['DE']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
192 u'Alemania'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
193
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
194 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
195 :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
196 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
197
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
198 def variants(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
199 return self._data['variants']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
200 variants = property(variants, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
201 Mapping of script codes to translated script names.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
202
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
203 >>> Locale('de', 'DE').variants['1901']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
204 u'alte deutsche Rechtschreibung'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
205
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
206 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
207 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
208
10
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
209 #{ 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
210
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
211 def number_symbols(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
212 return self._data['number_symbols']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
213 number_symbols = property(number_symbols, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
214 Symbols used in number formatting.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
215
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
216 >>> Locale('fr', 'FR').number_symbols['decimal']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
217 u','
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
218
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
219 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
220 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
221
10
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
222 #{ 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
223
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
224 def periods(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
225 return self._data['periods']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
226 periods = property(periods, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
227 Locale display names for day periods (AM/PM).
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
228
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
229 >>> Locale('en', 'US').periods['am']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
230 u'AM'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
231
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
232 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
233 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
234
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
235 def days(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
236 return self._data['days']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
237 days = property(days, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
238 Locale display names for weekdays.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
239
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
240 >>> Locale('de', 'DE').days['format']['wide'][4]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
241 u'Donnerstag'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
242
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
243 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
244 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
245
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
246 def months(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
247 return self._data['months']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
248 months = property(months, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
249 Locale display names for months.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
250
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
251 >>> Locale('de', 'DE').months['format']['wide'][10]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
252 u'Oktober'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
253
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
254 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
255 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
256
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
257 def quarters(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
258 return self._data['quarters']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
259 quarters = property(quarters, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
260 Locale display names for quarters.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
261
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
262 >>> Locale('de', 'DE').quarters['format']['wide'][1]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
263 u'1. Quartal'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
264
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
265 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
266 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
267
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
268 def eras(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
269 return self._data['eras']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
270 eras = property(eras, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
271 Locale display names for eras.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
272
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
273 >>> Locale('en', 'US').eras['wide'][1]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
274 u'Anno Domini'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
275 >>> Locale('en', 'US').eras['abbreviated'][0]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
276 u'BC'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
277
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
278 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
279 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
280
10
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
281 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
282 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
283 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
284 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
285
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
286 >>> Locale('de', 'DE').first_week_day
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
287 1
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
288 >>> Locale('en', 'US').first_week_day
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
289 7
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
290
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
291 :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
292 """)
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
293
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
294 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
295 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
296 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
297 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
298
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
299 >>> Locale('de', 'DE').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
300 6
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
301
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
302 :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
303 """)
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
304
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
305 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
306 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
307 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
308 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
309
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
310 >>> Locale('de', 'DE').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
311 7
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
312
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
313 :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
314 """)
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
315
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
316 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
317 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
318 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
319 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
320 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
321
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
322 >>> 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
323 4
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
324
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
325 :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
326 """)
0ca5dd65594f Pull in some supplemental data from the CLDR, for things like the first day of the week.
cmlenz
parents: 3
diff changeset
327
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
328 def date_formats(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
329 return self._data['date_formats']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
330 date_formats = property(date_formats, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
331 Locale patterns for date formatting.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
332
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
333 >>> Locale('en', 'US').date_formats['short']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
334 <DateTimeFormatPattern u'M/d/yy'>
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
335 >>> Locale('fr', 'FR').date_formats['long']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
336 <DateTimeFormatPattern u'd MMMM yyyy'>
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
337
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
338 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
339 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
340
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
341 def time_formats(self):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
342 return self._data['time_formats']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
343 time_formats = property(time_formats, doc="""\
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
344 Locale patterns for time formatting.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
345
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
346 >>> Locale('en', 'US').time_formats['short']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
347 <DateTimeFormatPattern u'h:mm a'>
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
348 >>> Locale('fr', 'FR').time_formats['long']
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
349 <DateTimeFormatPattern u'HH:mm:ss z'>
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
350
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
351 :type: `dict`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
352 """)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
353
11
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
354 def decimal_formats(self):
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
355 return self._data['decimal_formats']
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
356 decimal_formats = property(decimal_formats, doc="""\
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
357 Locale patterns for decimal number formatting.
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
358
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
359 > Locale('en', 'US').decimal_formats[None]
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
360 <NumberFormatPattern u'#,##0.###'>
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
361
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
362 :type: `dict`
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
363 """)
11f64b232b04 Add basic support for number format patterns.
jonas
parents: 10
diff changeset
364
3
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
365
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
366 def negotiate(preferred, available):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
367 """Find the best match between available and requested locale strings.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
368
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
369 >>> negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
370 'de_DE'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
371 >>> negotiate(['de_DE', 'en_US'], ['en', 'de'])
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
372 'de'
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
373
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
374 :param preferred: the list of locale strings preferred by the user
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
375 :param available: the list of locale strings available
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
376 :return: the locale identifier for the best match, or `None` if no match
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
377 was found
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
378 :rtype: `str`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
379 """
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
380 for locale in preferred:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
381 if locale in available:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
382 return locale
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
383 parts = locale.split('_')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
384 if len(parts) > 1 and parts[0] in available:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
385 return parts[0]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
386 return None
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
387
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
388 def parse(identifier, sep='_'):
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
389 """Parse a locale identifier into a ``(language, territory, variant)``
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
390 tuple.
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
391
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
392 >>> parse('zh_CN')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
393 ('zh', 'CN', None)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
394
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
395 The default component separator is "_", but a different separator can be
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
396 specified using the `sep` parameter:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
397
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
398 >>> parse('zh-CN', sep='-')
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
399 ('zh', 'CN', None)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
400
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
401 :param identifier: the locale identifier string
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
402 :param sep: character that separates the different parts of the locale
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
403 string
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
404 :return: the ``(language, territory, variant)`` tuple
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
405 :rtype: `tuple`
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
406 :raise `ValueError`: if the string does not appear to be a valid locale
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
407 identifier
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
408
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
409 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
410 """
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
411 parts = identifier.split(sep)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
412 lang, territory, variant = parts[0].lower(), None, None
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
413 if not lang.isalpha():
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
414 raise ValueError('expected only letters, got %r' % lang)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
415 if len(parts) > 1:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
416 territory = parts[1].upper().split('.', 1)[0]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
417 if not territory.isalpha():
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
418 raise ValueError('expected only letters, got %r' % territory)
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
419 if len(parts) > 2:
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
420 variant = parts[2].upper().split('.', 1)[0]
e9eaddab598e Import of initial code base.
cmlenz
parents:
diff changeset
421 return lang, territory, variant
Copyright (C) 2012-2017 Edgewall Software