Mercurial > babel > old > babel-test
comparison babel/core.py @ 157:dbad98f3dbf6 stable-0.8.x
Ported [159] to 0.8.x branch.
author | cmlenz |
---|---|
date | Thu, 21 Jun 2007 11:43:36 +0000 |
parents | f21c0725cdb4 |
children | 0db5d8723c76 |
comparison
equal
deleted
inserted
replaced
155:e043fa423ace | 157:dbad98f3dbf6 |
---|---|
109 return cls(default_locale(category)) | 109 return cls(default_locale(category)) |
110 default = classmethod(default) | 110 default = classmethod(default) |
111 | 111 |
112 def negotiate(cls, preferred, available, sep='_'): | 112 def negotiate(cls, preferred, available, sep='_'): |
113 """Find the best match between available and requested locale strings. | 113 """Find the best match between available and requested locale strings. |
114 | 114 |
115 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) | 115 >>> Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
116 <Locale "de_DE"> | 116 <Locale "de_DE"> |
117 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) | 117 >>> Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) |
118 <Locale "de"> | 118 <Locale "de"> |
119 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) | 119 >>> Locale.negotiate(['de_DE', 'de'], ['en_US']) |
120 | 120 |
121 You can specify the character used in the locale identifiers to separate | |
122 the differnet components. This separator is applied to both lists. Also, | |
123 case is ignored in the comparison: | |
124 | |
125 >>> Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') | |
126 <Locale "de_DE"> | |
127 | |
121 :param preferred: the list of locale identifers preferred by the user | 128 :param preferred: the list of locale identifers preferred by the user |
122 :param available: the list of locale identifiers available | 129 :param available: the list of locale identifiers available |
123 :return: the `Locale` object for the best match, or `None` if no match | 130 :return: the `Locale` object for the best match, or `None` if no match |
124 was found | 131 was found |
125 :rtype: `Locale` | 132 :rtype: `Locale` |
126 """ | 133 """ |
127 identifier = negotiate_locale(preferred, available, sep=sep) | 134 identifier = negotiate_locale(preferred, available, sep=sep) |
128 if identifier: | 135 if identifier: |
129 return Locale.parse(identifier) | 136 return Locale.parse(identifier, sep=sep) |
130 negotiate = classmethod(negotiate) | 137 negotiate = classmethod(negotiate) |
131 | 138 |
132 def parse(cls, identifier, sep='_'): | 139 def parse(cls, identifier, sep='_'): |
133 """Create a `Locale` instance for the given locale identifier. | 140 """Create a `Locale` instance for the given locale identifier. |
134 | 141 |
551 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) | 558 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) |
552 'de_DE' | 559 'de_DE' |
553 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) | 560 >>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) |
554 'de' | 561 'de' |
555 | 562 |
563 Case is ignored by the algorithm, the result uses the case of the preferred | |
564 locale identifier: | |
565 | |
566 >>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) | |
567 'de_DE' | |
568 | |
556 :param preferred: the list of locale strings preferred by the user | 569 :param preferred: the list of locale strings preferred by the user |
557 :param available: the list of locale strings available | 570 :param available: the list of locale strings available |
558 :param sep: character that separates the different parts of the locale | 571 :param sep: character that separates the different parts of the locale |
559 strings | 572 strings |
560 :return: the locale identifier for the best match, or `None` if no match | 573 :return: the locale identifier for the best match, or `None` if no match |
561 was found | 574 was found |
562 :rtype: `str` | 575 :rtype: `str` |
563 """ | 576 """ |
577 available = [a.lower() for a in available if a] | |
564 for locale in preferred: | 578 for locale in preferred: |
565 if locale in available: | 579 if locale.lower() in available: |
566 return locale | 580 return locale |
567 parts = locale.split(sep) | 581 parts = locale.split(sep) |
568 if len(parts) > 1 and parts[0] in available: | 582 if len(parts) > 1 and parts[0].lower() in available: |
569 return parts[0] | 583 return parts[0] |
570 return None | 584 return None |
571 | 585 |
572 def parse_locale(identifier, sep='_'): | 586 def parse_locale(identifier, sep='_'): |
573 """Parse a locale identifier into a ``(language, territory, variant)`` | 587 """Parse a locale identifier into a ``(language, territory, variant)`` |