jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] welcome: Fallback for i18n translate ......................................................................
[FIX] welcome: Fallback for i18n translate
Reimplement 65518573d2b07c7b807fd890b0e18613e885ecbe after that has been reverted in 7e401c34621d0a2ae76d8a896ab93d8984df13d6 because it introduced T97291.
Bug: T95921 Change-Id: I04694f179a3b58058d01c9b71a53862f97f91bf9 --- M pywikibot/i18n.py M scripts/welcome.py 2 files changed, 29 insertions(+), 15 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved Xqt: Looks good to me, but someone else must approve jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 2fff519..77e57c6 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -365,6 +365,9 @@ return message
+DEFAULT_FALLBACK = ('_default', ) + + def translate(code, xdict, parameters=None, fallback=False): """Return the most appropriate translation from a translation dict.
@@ -375,8 +378,9 @@
The language itself is always checked first, then languages that have been defined to be alternatives, and finally English. If none of - the options gives result, we just take the first language in the - list. + the options gives result, we just take the one language from xdict which may + not be always the same. When fallback is iterable it'll return None if no + code applies (instead of returning one).
For PLURAL support have a look at the twntranslate method
@@ -389,9 +393,9 @@ @type xdict: dict, string, unicode @param parameters: For passing (plural) parameters @type parameters: dict, string, unicode, int - @param fallback: Try an alternate language code - @type fallback: boolean - + @param fallback: Try an alternate language code. If it's iterable it'll + also try those entries and choose the first match. + @type fallback: boolean or iterable """ family = pywikibot.config.family # If a site is given instead of a code, use its language @@ -407,20 +411,29 @@ xdict = xdict['wikipedia']
# Get the translated string - trans = None if not isinstance(xdict, dict): trans = xdict - elif code in xdict: - trans = xdict[code] - elif fallback: - for alt in _altlang(code) + ['_default', 'en']: - if alt in xdict: - trans = xdict[alt] - code = alt + elif not xdict: + trans = None + else: + codes = [code] + if fallback is True: + codes += _altlang(code) + ['_default', 'en'] + elif fallback is not False: + codes += list(fallback) + for code in codes: + if code in xdict: + trans = xdict[code] break else: - trans = list(xdict.values())[0] + if fallback is not True: + # this shouldn't simply return "any one" code but when fallback + # was True before 65518573d2b0, it did just that. When False it + # did just return None. It's now also returning None in the new + # iterable mode. + return code = list(xdict.keys())[0] + trans = xdict[code] if trans is None: return # return None if we have no translation found if parameters is None: diff --git a/scripts/welcome.py b/scripts/welcome.py index 65d0e67..cefbf93 100755 --- a/scripts/welcome.py +++ b/scripts/welcome.py @@ -658,7 +658,8 @@ showStatus() pywikibot.output( 'Log page is not exist, getting information for page creation') - text = i18n.translate(self.site, logpage_header) + text = i18n.translate(self.site, logpage_header, + fallback=i18n.DEFAULT_FALLBACK) text += u'\n!%s' % self.site.namespace(2) text += u'\n!%s' % str.capitalize( self.site.mediawiki_message('contribslink'))
pywikibot-commits@lists.wikimedia.org