Page MenuHomePhabricator

Depecate the string type for code parameter in i18n.translate() and force a Site object to be used
Closed, ResolvedPublic

Description

Current implementation

  • param code: The site code as string or Site object.

Problem with string

  • if xdict is an extended dictionary with family name as key for the underlying code dictionary, the config.family is used as family name. This may lead to unexpected family lookup: The family of the site object may be different from the default site, e.g. if using a MultipleSiteBot.

For example have a look to the netext dict in welcome.py. Looking up the 'commons' entry is successful for a site object but it fails for a bare 'commons' code:

>>> import pwb, pywikibot as py
>>> from pywikibot.i18n import translate
>>> from scripts.welcome import netext
>>> s = py.Site('commons', 'commons')
>>> translate(s, netext)
'{{subst:welcome}} %s'
>>> translate('commons', netext) is None
True

Proposed solution

  • A Site object should be used in favour of the code string. Keep the old implementation but show an appropriate warning

Event Timeline

Xqt triaged this task as Low priority.Jun 20 2020, 1:11 PM
Xqt added subscribers: Dvorapa, valhallasw.

Change 606814 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [bugfix] code parameter of i18n.translate should be a BaseSite

https://gerrit.wikimedia.org/r/606814

already solved:

import pywikibot as py
from pywikibot.i18n import translate
from scripts.welcome import netext
s = py.Site('commons', 'commons')
translate(s, netext)
'{{subst:welcome}} %s'
translate('commons', netext) is None
False
translate('commons', netext)
'{{subst:welcome}} %s'

Change 606814 abandoned by Xqt:

[pywikibot/core@master] [bugfix] code parameter of i18n.translate should be a BaseSite

Reason:

already solved

https://gerrit.wikimedia.org/r/606814