jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1313084?usp=email )
Change subject: i18n.altlang: Improve fallback language lookup ......................................................................
i18n.altlang: Improve fallback language lookup
- use a dict for _LANG_TO_GROUP_NAME to avoid adding empty strings for missing keys - make the lang parameter positional-only - do not include lang in the returned fallback list - return fallback languages as tuple - update documentation - update tests
Change-Id: Ia8b219bc2a995738014152425ea4ffd29fb61348 --- M pywikibot/i18n.py M tests/i18n_tests.py 2 files changed, 27 insertions(+), 16 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 5e1eb1a..89a3c93 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -25,7 +25,7 @@ import os import pkgutil import re -from collections import abc, defaultdict +from collections import abc from collections.abc import Generator, Iterable, Iterator, Mapping, Sequence from contextlib import suppress from functools import cache @@ -48,7 +48,7 @@ # Flag to indicate whether translation messages are available _messages_available = None
-_LANG_TO_GROUP_NAME = defaultdict(str, { +_LANG_TO_GROUP_NAME: dict[str, str] = { 'aa': 'aa', 'ab': 'ab', 'ace': 'ace', @@ -264,10 +264,11 @@ 'zh-hans': 'zh-classical', 'zh-min-nan': 'zh-min-nan', 'zh-tw': 'zh-classical', - 'zh-yue': 'cdo'}) + 'zh-yue': 'cdo' +}
_GROUP_NAME_TO_FALLBACKS: dict[str, list[str]] = { - '': [], + '_default': [], 'aa': ['am'], 'ab': ['ru'], 'ace': ['id', 'ms', 'jv'], @@ -392,25 +393,36 @@ return _messages_available
-def altlang(lang: str) -> list[str]: +def altlang(lang: str, /) -> tuple[str, ...]: """Define fallback languages for particular languages.
- If no translation is available to a specified language, translate() will - try each of the specified fallback languages, in order, until it finds - one with a translation, with 'en' and '_default' as a last resort. + If no translation is available to a specified language, + :func:`translate` will try each of the specified fallback languages, + in order, until it finds one with a translation, with ``'_default'`` + (for :func:`translate`) and finally ``'en'`` as a last resort.
- For example, if for language 'xx', you want the preference of languages - to be: xx > fr > ru > en, you let this method return ['fr', 'ru']. + For example, if for language 'xx', you want the preference of + languages to be: ``xx > fr > ru > en``, you let this method return + ``('fr', 'ru')``.
- This code is used by other translating methods below. + This function is used by :func:`translate` and :func:`twtranslate`.
.. version-changed:: 11.6 - renamed from :func:`_altlang`. + Renamed from ``_altlang``. + .. version-changed:: 11.7 + The *lang* parameter is now positional-only. The function now + returns a tuple of fallback anguages instead of a list and no + longer includes *lang* itself.
:param lang: The language code - :return: Language codes + :return: Fallback language codes """ - return _GROUP_NAME_TO_FALLBACKS[_LANG_TO_GROUP_NAME[lang]] + return tuple( + code for code in _GROUP_NAME_TO_FALLBACKS[ + _LANG_TO_GROUP_NAME.get(lang, '_default') + ] + if code != lang + )
@cache diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py index cbb9a8e..9d9871f 100755 --- a/tests/i18n_tests.py +++ b/tests/i18n_tests.py @@ -40,10 +40,9 @@ def test_groupnames(self): """Test that groupnames are in groups.""" groupnames = set(i18n._LANG_TO_GROUP_NAME.values()) - groupnames.discard('') # might be created by defaultdict self.assertLess(groupnames, i18n._LANG_TO_GROUP_NAME.keys()) groups = list(i18n._GROUP_NAME_TO_FALLBACKS) - groups.remove('') # remove empty fallback + groups.remove('_default') # remove default fallback self.assertEqual(sorted(groupnames), groups)
pywikibot-commits@lists.wikimedia.org