"""Title translate module."""## (C) Pywikibot team, 2003-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportpywikibotfrompywikibotimportconfig,date
[docs]deftranslate(page=None,hints:list[str]|None=None,auto:bool=True,removebrackets:bool=False,site=None)->list[pywikibot.Link]:"""Return a list of links to pages on other sites based on hints. Entries for single page titles list those pages. Page titles for entries such as "all:" or "xyz:" or "20:" are first built from the page title of 'page' and then listed. .. versionchanged:: 9.6 Raise ``RuntimeError`` instead of ``AssertionError`` if neither *page* nor *site* parameter is given. :param auto: If true, known year and date page titles are autotranslated to all known target languages and inserted into the list. :param removebrackets: If True, a trailing pair of brackets and the text between them is removed from the page title. :raises RuntimeError: Either page or site parameter must be given. """ifnotpageandnotsite:raiseRuntimeError('Either page or site parameter must be given with translate()')site=siteorpage.siteresult=set()ifhintsisNone:hints=[]forhinhints:# argument may be given as -hint:xy where xy is a language codecodes,_,newname=h.partition(':')ifnotnewname:# if given as -hint:xy or -hint:xy:, assume that there should# be a page in language xy with the same title as the page# we're currently working on ...ifpageisNone:continuenewname=page.title(with_ns=False,without_brackets=removebrackets)ifcodes.isdigit():codes=site.family.languages_by_size[:int(codes)]elifcodes=='all':codes=list(site.family.codes)else:codes=site.family.language_groups.get(codes,codes.split(','))fornewcodeincodes:ifnewcodeinsite.codes:ifnewcode!=site.code:ns=page.namespace()ifpageelse0link=pywikibot.Link(newname,site.getSite(code=newcode),default_namespace=ns)result.add(link)elifconfig.verbose_output:pywikibot.info(f'Ignoring unknown language code {newcode}')# Autotranslate dates into all other languages, the rest will come from# existing interwiki links.ifautoandpage:# search inside all dictionaries for this linkdict_name,value=page.autoFormat()ifdict_name:pywikibot.info(f'TitleTranslate: {page.title()} was recognized as 'f'{dict_name} with value {value}')forentry_lang,entryindate.formats[dict_name].items():ifentry_langnotinsite.codes:continueifentry_lang!=page.site.lang:link=pywikibot.Link(entry(value),site.getSite(entry_lang))result.add(link)returnlist(result)