annotate babel/core.py @ 22:7d37639a7411

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