jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/904878 )
Change subject: [i18n] Enable preleading Bot: prefix with twtranslate messages ...................................................................... [i18n] Enable preleading Bot: prefix with twtranslate messages - add new function get_bot_prefix() to get a Bot: prefix which depends on config setting: - modify twtranslate to prepend the bot prefix - add 'bot_prefix_summary' to config.py - add default pywikibot-bot-prefix token for fallback Bug: T161459 Change-Id: I86da3e4baebb96054d604f13bcb8a4f48274ecce --- M pywikibot/i18n.py M pywikibot/config.py M pywikibot/scripts/i18n/pywikibot/en.json 3 files changed, 81 insertions(+), 13 deletions(-) Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified diff --git a/pywikibot/config.py b/pywikibot/config.py index 02ff93e..03628a6 100644 --- a/pywikibot/config.py +++ b/pywikibot/config.py @@ -241,6 +241,14 @@ # relevant summary for bot edits default_edit_summary = 'Pywikibot ' + pwb_version +# Edit summary prefix +# if a str, always use this as summary prefix e.g. 'Bot:' for all sites +# if True, always use a summary prefix from i18n +# if False, never use a summary prefix +# if None, the i18n summary prefix is used for botflag accounts only +# NOTE: this feature is not yet implemented for all scripts +bot_prefix_summary: Union[bool, str, None] = None + # What permissions to use to set private files to it # such as password file. # diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index a18fa8a..32383f1 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -15,7 +15,7 @@ See :py:obj:`twtranslate` for more information on the messages. """ # -# (C) Pywikibot team, 2004-2022 +# (C) Pywikibot team, 2004-2023 # # Distributed under the terms of the MIT license. # @@ -653,14 +653,51 @@ return trans -def twtranslate(source: STR_OR_SITE_TYPE, - twtitle: str, - parameters: Union[Sequence[str], Mapping[str, int], - None] = None, - *, - fallback: bool = True, - fallback_prompt: Optional[str] = None, - only_plural: bool = False) -> Optional[str]: +def get_bot_prefix(source: STR_OR_SITE_TYPE, use_prefix: bool) -> str: + """Get the bot prefix string like 'Bot: ' including space delimiter. + + .. note: If *source* is a str and ``config.bot_prefix`` is set to + None, it cannot be determined whether the current user is a bot + account. In this cas the prefix will be returned. + .. versionadded:: 8.1 + + :param source: When it's a site it's using the lang attribute and otherwise + it is using the value directly. + :param use_prefix: If True, return a bot prefix which depends on the + ``config.bot_prefix`` setting. + """ + config_prefix = config.bot_prefix_summary + if not use_prefix or config_prefix is False: + return '' + + if isinstance(config_prefix, str): + return config_prefix + ' ' + + try: + prefix = twtranslate(source, 'pywikibot-bot-prefix') + ' ' + except pywikibot.exceptions.TranslationError: + # the 'pywikibot' package is available but the message key may + # be missing + prefix = 'Bot: ' + + if config_prefix is True \ + or not hasattr(source, 'lang') \ + or source.isBot(source.username()): + return prefix + + return '' + + +def twtranslate( + source: STR_OR_SITE_TYPE, + twtitle: str, + parameters: Union[Sequence[str], Mapping[str, int], None] = None, + *, + fallback: bool = True, + fallback_prompt: Optional[str] = None, + only_plural: bool = False, + bot_prefix: bool = False +) -> Optional[str]: r""" Translate a message using JSON files in messages_package_name. @@ -712,8 +749,11 @@ ... ) % {'descr': 'seulement'}) 'Robot: Changer seulement quelques pages.' + .. versionchanged:: 8.1 + the *bot_prefix* parameter was added. + :param source: When it's a site it's using the lang attribute and otherwise - it is using the value directly. + it is using the value directly. The site object is recommended. :param twtitle: The TranslateWiki string title, in <package>-<key> format :param parameters: For passing parameters. It should be a mapping but for backwards compatibility can also be a list, tuple or a single value. @@ -725,9 +765,13 @@ plural instances. If this is False it will apply the parameters also to the resulting string. If this is True the placeholders must be manually applied afterwards. + :param bot_prefix: If True, prepend the message with a bot prefix + which depends on the ``config.bot_prefix`` setting :raise IndexError: If the language supports and requires more plurals than defined for the given translation template. """ + prefix = get_bot_prefix(source, use_prefix=bot_prefix) + if not messages_available(): if fallback_prompt: if parameters and not only_plural: @@ -741,7 +785,6 @@ .format(_messages_package_name, twtitle, __url__)) # if source is a site then use its lang attribute, otherwise it's a str - lang = getattr(source, 'lang', source) # There are two possible failure modes: the translation dict might not have @@ -774,8 +817,8 @@ .format(type(parameters).__name__)) if not only_plural and parameters: - return trans % parameters - return trans + trans = trans % parameters + return prefix + trans def twhas_key(source: STR_OR_SITE_TYPE, twtitle: str) -> bool: diff --git a/pywikibot/scripts/i18n/pywikibot/en.json b/pywikibot/scripts/i18n/pywikibot/en.json index d731bfe..ece08e2 100644 --- a/pywikibot/scripts/i18n/pywikibot/en.json +++ b/pywikibot/scripts/i18n/pywikibot/en.json @@ -9,6 +9,7 @@ "Xqt" ] }, + "pywikibot-bot-prefix": "Bot:", "pywikibot-cosmetic-changes": "; cosmetic changes", "pywikibot-enter-category-name": "Please enter the category name:", "pywikibot-enter-file-links-processing": "Links to which file page should be processed?", -- To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/904878 To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Change-Id: I86da3e4baebb96054d604f13bcb8a4f48274ecce Gerrit-Change-Number: 904878 Gerrit-PatchSet: 4 Gerrit-Owner: Xqt <i...@gno.de> Gerrit-Reviewer: Dvorapa <dvor...@seznam.cz> Gerrit-Reviewer: Framawiki <framaw...@tools.wmflabs.org> Gerrit-Reviewer: MarcoAurelio <maure...@toolforge.org> Gerrit-Reviewer: Xqt <i...@gno.de> Gerrit-Reviewer: jenkins-bot Gerrit-MessageType: merged
_______________________________________________ Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org