annotate babel/core.py @ 592:e6b1efa9a255 trunk

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