jenkins-bot has submitted this change and it was merged. Change subject: [FIX] welcome: Fallback for i18n translate ......................................................................
[FIX] welcome: Fallback for i18n translate In pywikibot compat i18n.translate fell back by default but with core this was disabled, but this scripts still requires it. This adds the functionality to give an iterable as fallback. Bug: T95921 Change-Id: I1ff78112d6cf8f05a587aff690576346a2490c5e --- M pywikibot/i18n.py M scripts/welcome.py 2 files changed, 28 insertions(+), 15 deletions(-) Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 2fff519..06c4049 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,28 @@ 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 False and fallback is not True: + # future versions shouldn't simply return "any one" code but + # no translation as this is not very deterministic. When + # fallback is iterable it's a new mode previously not supported + 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 16403be..37b7737 100644 --- a/scripts/welcome.py +++ b/scripts/welcome.py @@ -657,7 +657,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')) -- To view, visit https://gerrit.wikimedia.org/r/203942 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I1ff78112d6cf8f05a587aff690576346a2490c5e Gerrit-PatchSet: 3 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: XZise <commodorefabia...@gmx.de> Gerrit-Reviewer: John Vandenberg <jay...@gmail.com> Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com> Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl> Gerrit-Reviewer: XZise <commodorefabia...@gmx.de> Gerrit-Reviewer: Xqt <i...@gno.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ Pywikibot-commits mailing list Pywikibot-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits