# HG changeset patch # User fschwarz # Date 1344495383 0 # Node ID 6f86d60dab569ae31c1eb46cf3db35b3571980af # Parent c8bef607128cbc59ea61679bbba50a1c1f57b988 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 diff --git a/babel/core.py b/babel/core.py --- a/babel/core.py +++ b/babel/core.py @@ -83,7 +83,7 @@ >>> locale = Locale('en', 'US') >>> repr(locale) - '' + "Locale('en', territory='US')" >>> locale.display_name u'English (United States)' @@ -91,7 +91,7 @@ >>> locale = Locale.parse('en-US', sep='-') >>> repr(locale) - '' + "Locale('en', territory='US')" `Locale` objects provide access to a collection of locale data, such as territory and language names, number and date format patterns, and more: @@ -131,7 +131,7 @@ self.script = script self.variant = variant self.__data = None - + identifier = str(self) if not localedata.exists(identifier): raise UnknownLocaleError(identifier) @@ -144,7 +144,7 @@ ... os.environ[name] = '' >>> os.environ['LANG'] = 'fr_FR.UTF-8' >>> Locale.default('LC_MESSAGES') - + Locale('fr_FR') :param category: one of the ``LC_XXX`` environment variable names :param aliases: a dictionary of aliases for locale identifiers @@ -160,9 +160,9 @@ """Find the best match between available and requested locale strings. >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) - + Locale('de', territory='DE') >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) - + Locale('de') >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) You can specify the character used in the locale identifiers to separate @@ -170,7 +170,7 @@ case is ignored in the comparison: >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') - + Locale('de', territory='DE') :param preferred: the list of locale identifers preferred by the user :param available: the list of locale identifiers available @@ -197,7 +197,7 @@ object, that object is returned: >>> Locale.parse(l) - + Locale('de', territory='DE') :param identifier: the locale identifier string :param sep: optional component separator @@ -220,7 +220,13 @@ return not self.__eq__(other) def __repr__(self): - return '' % str(self) + parameters = [''] + for key in ('territory', 'script', 'variant'): + value = getattr(self, key) + if value is not None: + parameters.append('%s=%r' % (key, value)) + parameter_string = '%r' % self.language + ', '.join(parameters) + return 'Locale(%s)' % parameter_string def __str__(self): return '_'.join(filter(None, [self.language, self.script, diff --git a/babel/tests/core.py b/babel/tests/core.py --- a/babel/tests/core.py +++ b/babel/tests/core.py @@ -16,7 +16,22 @@ import unittest from babel import core -from babel.core import default_locale +from babel.core import default_locale, Locale + + +class LocaleTest(unittest.TestCase): + + def test_locale_provides_access_to_cldr_locale_data(self): + locale = Locale('en', 'US') + self.assertEqual(u'English (United States)', locale.display_name) + self.assertEqual(u'.', locale.number_symbols['decimal']) + + def test_repr(self): + self.assertEqual("Locale('de', territory='DE')", + repr(Locale('de', 'DE'))) + self.assertEqual("Locale('zh', territory='CN', script='Hans')", + repr(Locale('zh', 'CN', script='Hans'))) + class DefaultLocaleTest(unittest.TestCase): @@ -43,9 +58,11 @@ # must not throw an exception default_locale('LC_CTYPE') + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(core)) + suite.addTest(unittest.makeSuite(LocaleTest)) suite.addTest(unittest.makeSuite(DefaultLocaleTest)) return suite