jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[bugfix] use hardcoded messages if i18n system is not available

- add fallback_prompt parameter to i18n.twtranslate();
fallback, fallback_prompt and only_plural become keyword only arguments
- use fallback_prompt with bot.open_webbrowser()
- use fallback_prompt with Page._cosmetic_changes_hook()
- use fallback_prompt with Page.touch()
- add tests to MissingPackageTestCase class

Bug: T275981
Change-Id: I4ef24d66f360c03f721ffcc84cc33c5312fb5622
---
M pywikibot/bot.py
M pywikibot/i18n.py
M pywikibot/page/__init__.py
M tests/i18n_tests.py
4 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index a49ef20..a8945ea 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -973,7 +973,8 @@
def open_webbrowser(page):
"""Open the web browser displaying the page and wait for input."""
webbrowser.open(page.full_url())
- i18n.input('pywikibot-enter-finished-browser')
+ i18n.input('pywikibot-enter-finished-browser',
+ fallback_prompt='Press Enter when finished in browser.')


class _OptionDict(dict):
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 688cfd8..3c4c96e 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -15,7 +15,7 @@
See L{twtranslate} for more information on the messages.
"""
#
-# (C) Pywikibot team, 2004-2020
+# (C) Pywikibot team, 2004-2021
#
# Distributed under the terms of the MIT license.
#
@@ -641,8 +641,9 @@
@deprecated_args(code='source')
def twtranslate(source,
twtitle: str,
- parameters: Optional[Mapping] = None,
+ parameters: Optional[Mapping] = None, *,
fallback: bool = True,
+ fallback_prompt: Optional[str] = None,
only_plural: bool = False) -> Optional[str]:
r"""
Translate a message using JSON files in messages_package_name.
@@ -704,7 +705,7 @@
They are also used for plural entries in which case they must be a
Mapping and will cause a TypeError otherwise.
@param fallback: Try an alternate language code
- @type fallback: boolean
+ @param fallback_prompt: The English message if i18n is not available
@param only_plural: Define whether the parameters should be only applied to
plural instances. If this is False it will apply the parameters also
to the resulting string. If this is True the placeholders must be
@@ -713,6 +714,11 @@
defined for the given translation template.
"""
if not messages_available():
+ if fallback_prompt:
+ if parameters and not only_plural:
+ return fallback_prompt % parameters
+ return fallback_prompt
+
raise TranslationError(
'Unable to load messages package %s for bundle %s'
'\nIt can happen due to lack of i18n submodule or files. '
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index a155e4b..f703998 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -1222,9 +1222,11 @@
CANCEL_MATCH, CosmeticChangesToolkit)
cc_toolkit = CosmeticChangesToolkit(self, ignore=CANCEL_MATCH)
self.text = cc_toolkit.change(old)
+
if summary and old.strip().replace(
'\r\n', '\n') != self.text.strip().replace('\r\n', '\n'):
- summary += i18n.twtranslate(self.site, 'cosmetic_changes-append')
+ summary += i18n.twtranslate(self.site, 'cosmetic_changes-append',
+ fallback_prompt='; cosmetic changes')
return summary

@deprecate_arg('async', 'asynchronous') # T106230
@@ -1319,7 +1321,8 @@
if self.exists():
# ensure always get the page text and not to change it.
del self.text
- summary = i18n.twtranslate(self.site, 'pywikibot-touch')
+ summary = i18n.twtranslate(self.site, 'pywikibot-touch',
+ fallback_prompt='Pywikibot touch edit')
self.save(summary=summary, watch='nochange',
minor=False, botflag=botflag, force=True,
asynchronous=False, callback=callback,
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 031effc..1732740 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -8,7 +8,7 @@

import pywikibot

-from pywikibot import bot, i18n, plural
+from pywikibot import bot, config, i18n, plural

from tests.aspects import (
AutoDeprecationTestCase,
@@ -525,13 +525,38 @@
bot.ui.output = self.orig_output
super().tearDown()

- def test_pagegen_i18n_input(self):
+ def test_i18n_input(self):
"""Test i18n.input falls back with missing message package."""
rv = i18n.input('pywikibot-enter-category-name',
fallback_prompt='dummy output')
self.assertEqual(rv, 'dummy input')
self.assertIn('dummy output: ', self.output_text)

+ def test_i18n_twtranslate(self):
+ """Test i18n.twtranslate falls back with missing message package."""
+ rv = i18n.twtranslate(self.site, 'pywikibot-enter-category-name',
+ fallback_prompt='dummy message')
+ self.assertEqual(rv, 'dummy message')
+
+ def test_cosmetic_changes_hook(self):
+ """Test summary result of Page._cosmetic_changes_hook."""
+ page = pywikibot.Page(self.site, 'Test')
+ page.text = 'Some content with spaces.'
+ # check cc settings
+ old_setting = config.cosmetic_changes_mylang_only
+ config.cosmetic_changes_mylang_only = False
+ self.assertFalse(page.isTalkPage())
+ self.assertEqual(page.content_model, 'wikitext')
+ self.assertNotIn(pywikibot.calledModuleName(),
+ config.cosmetic_changes_deny_script)
+ self.assertFalse(config.cosmetic_changes_mylang_only)
+
+ summary = 'Working on Test page at site {}'.format(self.site)
+ msg = page._cosmetic_changes_hook(summary)
+ self.assertEqual(msg, summary + '; cosmetic changes')
+ # restore setting
+ config.cosmetic_changes_mylang_only = old_setting
+

class TestExtractPlural(TestCase):


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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I4ef24d66f360c03f721ffcc84cc33c5312fb5622
Gerrit-Change-Number: 667372
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Lokal Profil <andre.costa@wikimedia.se>
Gerrit-Reviewer: Merlijn van Deen <valhallasw@arctus.nl>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged