comparison babel/core.py @ 590:6f86d60dab56 trunk

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
author fschwarz
date Thu, 09 Aug 2012 06:56:23 +0000
parents 99d51589c822
children d1618dfaf114
comparison
equal deleted inserted replaced
589:c8bef607128c 590:6f86d60dab56
81 class Locale(object): 81 class Locale(object):
82 """Representation of a specific locale. 82 """Representation of a specific locale.
83 83
84 >>> locale = Locale('en', 'US') 84 >>> locale = Locale('en', 'US')
85 >>> repr(locale) 85 >>> repr(locale)
86 '<Locale "en_US">' 86 "Locale('en', territory='US')"
87 >>> locale.display_name 87 >>> locale.display_name
88 u'English (United States)' 88 u'English (United States)'
89 89
90 A `Locale` object can also be instantiated from a raw locale string: 90 A `Locale` object can also be instantiated from a raw locale string:
91 91
92 >>> locale = Locale.parse('en-US', sep='-') 92 >>> locale = Locale.parse('en-US', sep='-')
93 >>> repr(locale) 93 >>> repr(locale)
94 '<Locale "en_US">' 94 "Locale('en', territory='US')"
95 95
96 `Locale` objects provide access to a collection of locale data, such as 96 `Locale` objects provide access to a collection of locale data, such as
97 territory and language names, number and date format patterns, and more: 97 territory and language names, number and date format patterns, and more:
98 98
99 >>> locale.number_symbols['decimal'] 99 >>> locale.number_symbols['decimal']
129 self.language = language 129 self.language = language
130 self.territory = territory 130 self.territory = territory
131 self.script = script 131 self.script = script
132 self.variant = variant 132 self.variant = variant
133 self.__data = None 133 self.__data = None
134 134
135 identifier = str(self) 135 identifier = str(self)
136 if not localedata.exists(identifier): 136 if not localedata.exists(identifier):
137 raise UnknownLocaleError(identifier) 137 raise UnknownLocaleError(identifier)
138 138
139 @classmethod 139 @classmethod
142 142
143 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: 143 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']:
144 ... os.environ[name] = '' 144 ... os.environ[name] = ''
145 >>> os.environ['LANG'] = 'fr_FR.UTF-8' 145 >>> os.environ['LANG'] = 'fr_FR.UTF-8'
146 >>> Locale.default('LC_MESSAGES') 146 >>> Locale.default('LC_MESSAGES')
147 <Locale "fr_FR"> 147 Locale('fr_FR')
148 148
149 :param category: one of the ``LC_XXX`` environment variable names 149 :param category: one of the ``LC_XXX`` environment variable names
150 :param aliases: a dictionary of aliases for locale identifiers 150 :param aliases: a dictionary of aliases for locale identifiers
151 :return: the value of the variable, or any of the fallbacks 151 :return: the value of the variable, or any of the fallbacks
152 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``) 152 (``LANGUAGE``, ``LC_ALL``, ``LC_CTYPE``, and ``LANG``)
158 @classmethod 158 @classmethod
159 def negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES): 159 def negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES):
160 """Find the best match between available and requested locale strings. 160 """Find the best match between available and requested locale strings.
161 161
162 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) 162 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
163 <Locale "de_DE"> 163 Locale('de', territory='DE')
164 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) 164 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de'])
165 <Locale "de"> 165 Locale('de')
166 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) 166 >>> Locale.negotiate(['de_DE', 'de'], ['en_US'])
167 167
168 You can specify the character used in the locale identifiers to separate 168 You can specify the character used in the locale identifiers to separate
169 the differnet components. This separator is applied to both lists. Also, 169 the differnet components. This separator is applied to both lists. Also,
170 case is ignored in the comparison: 170 case is ignored in the comparison:
171 171
172 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') 172 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-')
173 <Locale "de_DE"> 173 Locale('de', territory='DE')
174 174
175 :param preferred: the list of locale identifers preferred by the user 175 :param preferred: the list of locale identifers preferred by the user
176 :param available: the list of locale identifiers available 176 :param available: the list of locale identifiers available
177 :param aliases: a dictionary of aliases for locale identifiers 177 :param aliases: a dictionary of aliases for locale identifiers
178 :return: the `Locale` object for the best match, or `None` if no match 178 :return: the `Locale` object for the best match, or `None` if no match
195 195
196 If the `identifier` parameter is not a string, but actually a `Locale` 196 If the `identifier` parameter is not a string, but actually a `Locale`
197 object, that object is returned: 197 object, that object is returned:
198 198
199 >>> Locale.parse(l) 199 >>> Locale.parse(l)
200 <Locale "de_DE"> 200 Locale('de', territory='DE')
201 201
202 :param identifier: the locale identifier string 202 :param identifier: the locale identifier string
203 :param sep: optional component separator 203 :param sep: optional component separator
204 :return: a corresponding `Locale` instance 204 :return: a corresponding `Locale` instance
205 :rtype: `Locale` 205 :rtype: `Locale`
218 218
219 def __ne__(self, other): 219 def __ne__(self, other):
220 return not self.__eq__(other) 220 return not self.__eq__(other)
221 221
222 def __repr__(self): 222 def __repr__(self):
223 return '<Locale "%s">' % str(self) 223 parameters = ['']
224 for key in ('territory', 'script', 'variant'):
225 value = getattr(self, key)
226 if value is not None:
227 parameters.append('%s=%r' % (key, value))
228 parameter_string = '%r' % self.language + ', '.join(parameters)
229 return 'Locale(%s)' % parameter_string
224 230
225 def __str__(self): 231 def __str__(self):
226 return '_'.join(filter(None, [self.language, self.script, 232 return '_'.join(filter(None, [self.language, self.script,
227 self.territory, self.variant])) 233 self.territory, self.variant]))
228 234
Copyright (C) 2012-2017 Edgewall Software