jenkins-bot merged this change.

View Change

Approvals: D3r1ck01: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
[feat] Allow i18n.translate fallback to wikibase item

Bug: T173103
Bug: T218467
Change-Id: Ib1867c75643bfa7275a137d3f893bb3f91c35780
---
M pywikibot/i18n.py
M tests/i18n_tests.py
2 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 4000a51..bff1416 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -43,7 +43,8 @@
from pywikibot.exceptions import Error
from pywikibot.plural import plural_rules
from pywikibot.tools import (
- deprecated, deprecated_args, issue_deprecation_warning, StringTypes)
+ deprecated, deprecated_args, issue_deprecation_warning, StringTypes,
+ UnicodeType)

PLURAL_PATTERN = r'{{PLURAL:(?:%\()?([^\)]*?)(?:\)d)?\|(.*?)}}'

@@ -583,8 +584,9 @@
@param parameters: For passing (plural) parameters
@type parameters: dict, string, unicode, int
@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
+ also try those entries and choose the first match. If it's wikibase
+ item, it'll try to load local page title from here.
+ @type fallback: bool, iterable or str
@raise IndexError: If the language supports and requires more plurals than
defined for the given translation template.
"""
@@ -610,7 +612,7 @@
codes = [code]
if fallback is True:
codes += _altlang(code) + ['_default', 'en']
- elif fallback is not False:
+ elif fallback is not False and not isinstance(fallback, UnicodeType):
codes += list(fallback)
for code in codes:
if code in xdict:
@@ -618,11 +620,17 @@
break
else:
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
+ if isinstance(fallback, UnicodeType):
+ r_page = pywikibot.Site(code, family).page_from_repository(
+ fallback)
+ trans = r_page.title()
+ else:
+ # 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:
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 3b700fe..6d5396f 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -87,6 +87,22 @@
'test-no-english JA')


+class TestTranslateNonDry(TestCase):
+
+ """Test translate method."""
+
+ family = 'wikipedia'
+ code = 'cs'
+
+ msg_localized = {'en': 'Universe', 'de': 'Universum'}
+
+ def testWikibaseFallback(self):
+ """Test Wikibase item fallback."""
+ self.assertEqual(i18n.translate(self.code, self.msg_localized,
+ fallback='Q1'),
+ 'Vesmír')
+
+
class UserInterfaceLangTestCase(TestCase):

"""Base class for tests using config.userinterface_lang."""

To view, visit change 498714. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib1867c75643bfa7275a137d3f893bb3f91c35780
Gerrit-Change-Number: 498714
Gerrit-PatchSet: 15
Gerrit-Owner: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: XZise <CommodoreFabianus@gmx.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999@gmail.com>
Gerrit-Reviewer: jenkins-bot (75)