Revision: 4496 Author: huji Date: 2007-11-02 07:56:45 +0000 (Fri, 02 Nov 2007)
Log Message: ----------- * Renaming the new translate() function to gettranslation() to allow adding the old translate() function back. * Changing other modules accordingly, to use this newly named gettranslation() function. * Adding translate() function back in wikipedia.py to allow user-created bots to work the previous way. * Adding details about why we have two function for transalting edit summaries.
Modified Paths: -------------- branches/pywikipedia/messages/basic.py branches/pywikipedia/messages/capitalize_redirects.py branches/pywikipedia/messages/catall.py branches/pywikipedia/messages/category.py branches/pywikipedia/messages/catlib.py branches/pywikipedia/messages/clean_sandbox.py branches/pywikipedia/messages/commons_link.py branches/pywikipedia/messages/copyright_clean.py branches/pywikipedia/messages/cosmetic_changes.py branches/pywikipedia/messages/messages.py branches/pywikipedia/messages/wikipedia.py
Modified: branches/pywikipedia/messages/basic.py =================================================================== --- branches/pywikipedia/messages/basic.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/basic.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -43,7 +43,7 @@
def run(self): # Set the edit summary message - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Adding "test" to the beginning of the page.')) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), u'Adding "test" to the beginning of the page.')) for page in self.generator: self.treat(page)
Modified: branches/pywikipedia/messages/capitalize_redirects.py =================================================================== --- branches/pywikipedia/messages/capitalize_redirects.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/capitalize_redirects.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -62,7 +62,7 @@ if self.acceptall or choice in ['y', 'Y']: try: wikipedia.setAction( - wikipedia.translate(wikipedia.getSite(), u'Robot: Create redirect to [[%s]]') + wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Create redirect to [[%s]]') % page_t) np.put(u"#REDIRECT [[%s]]" % page_t) print
Modified: branches/pywikipedia/messages/catall.py =================================================================== --- branches/pywikipedia/messages/catall.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/catall.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -60,7 +60,7 @@ for p in list: cattitle="%s:%s" % (site.category_namespace(), p) pllist.append(wikipedia.Page(site,cattitle)) - page.put(wikipedia.replaceCategoryLinks(page.get(), pllist), comment = wikipedia.translate(site.lang, u'Bot: Changing categories')) + page.put(wikipedia.replaceCategoryLinks(page.get(), pllist), comment = wikipedia.gettranslation(site.lang, u'Bot: Changing categories'))
docorrections=True start=[]
Modified: branches/pywikipedia/messages/category.py =================================================================== --- branches/pywikipedia/messages/category.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/category.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -230,7 +230,7 @@ newcatTitle = newcatTitle[:1].capitalize() + newcatTitle[1:]
# set edit summary message - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Adding [[Category:%s]]') % newcatTitle) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), u'Adding [[Category:%s]]') % newcatTitle)
cat_namespace = wikipedia.getSite().category_namespaces()[0]
@@ -290,7 +290,7 @@ if self.editSummary: wikipedia.setAction(self.editSummary) else: - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Robot: Changing %s') % self.oldCat.title()) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Changing %s') % self.oldCat.title())
def run(self): newCat = catlib.Category(wikipedia.getSite(), 'Category:' + self.newCatTitle) @@ -310,9 +310,9 @@ catlib.change_category(subcategory, self.oldCat, newCat, comment = self.editSummary, inPlace=self.inPlace) if self.oldCat.exists() and self.moveCatPage: # try to copy page contents to new cat page - if self.oldCat.copyAndKeep(self.newCatTitle, wikipedia.translate(wikipedia.getSite(), cfd_templates)): + if self.oldCat.copyAndKeep(self.newCatTitle, wikipedia.gettranslation(wikipedia.getSite(), cfd_templates)): if self.oldCat.isEmpty() and self.deleteEmptySourceCat == True: - reason = wikipedia.translate(wikipedia.getSite(), u'Robot: Category was moved to [[Category:%s|%s]]') % (self.newCatTitle, self.newCatTitle) + reason = wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Category was moved to [[Category:%s|%s]]') % (self.newCatTitle, self.newCatTitle) if self.batchMode: self.oldCat.delete(reason, False) else: @@ -342,7 +342,7 @@ if self.editSummary: wikipedia.setAction(self.editSummary) else: - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Robot: Listifying from %s (%d entries)') % (self.cat.title(), len(listOfArticles))) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Listifying from %s (%d entries)') % (self.cat.title(), len(listOfArticles)))
listString = "" for article in listOfArticles: @@ -379,7 +379,7 @@ if self.editSummary: wikipedia.setAction(self.editSummary) else: - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Robot: Removing from %s') % self.cat.title()) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Removing from %s') % self.cat.title())
def run(self): articles = self.cat.articlesList(recurse = 0) @@ -400,7 +400,7 @@ if self.useSummaryForDeletion: reason = self.editSummary else: - reason = wikipedia.translate(wikipedia.getSite(), u'Robot: Category was disbanded') + reason = wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Category was disbanded') if self.batchMode: self.cat.delete(reason, False) else: @@ -550,7 +550,7 @@ cat = catlib.Category(wikipedia.getSite(), 'Category:' + self.catTitle)
# get edit summary message - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), msg_change) % cat.title()) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), msg_change) % cat.title())
articles = cat.articlesList(recurse = False) if len(articles) == 0: @@ -615,7 +615,7 @@ # create a list of wiki links to the supercategories supercat_names.append('[[:%s|%s]]' % (supercats[i].title(), supercats[i].title().split(':', 1)[1])) # print this list, separated with commas - result += ' ' + wikipedia.translate(wikipedia.getSite(), u'(also in %s)') % ', '.join(supercat_names) + result += ' ' + wikipedia.gettranslation(wikipedia.getSite(), u'(also in %s)') % ', '.join(supercat_names) result += '\n' if currentDepth < self.maxDepth: for subcat in self.catDB.getSubcats(cat):
Modified: branches/pywikipedia/messages/catlib.py =================================================================== --- branches/pywikipedia/messages/catlib.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/catlib.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -358,7 +358,7 @@ else: wikipedia.output('Moving text from %s to %s.' % (self.title(), targetCat.title())) authors = ', '.join(self.contributingUsers()) - creationSummary = wikipedia.translate(wikipedia.getSite(), u'Robot: Moved from %s. Authors: %s') % (self.title(), authors) + creationSummary = wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Moved from %s. Authors: %s') % (self.title(), authors) targetCat.put(self.get(), creationSummary) return True
@@ -378,7 +378,7 @@ else: wikipedia.output('Moving text from %s to %s.' % (self.title(), targetCat.title())) authors = ', '.join(self.contributingUsers()) - creationSummary = wikipedia.translate(wikipedia.getSite(), msg_created_for_renaming) % (self.title(), authors) + creationSummary = wikipedia.gettranslation(wikipedia.getSite(), msg_created_for_renaming) % (self.title(), authors) newtext = self.get() for regexName in cfdTemplates: matchcfd = re.compile(r"{{%s.*?}}" % regexName, re.IGNORECASE)
Modified: branches/pywikipedia/messages/clean_sandbox.py =================================================================== --- branches/pywikipedia/messages/clean_sandbox.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/clean_sandbox.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -55,15 +55,15 @@ mySite = wikipedia.getSite() while True: now = time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) - localSandboxTitle = wikipedia.translate(mySite, sandboxTitle) + localSandboxTitle = wikipedia.gettranslation(mySite, sandboxTitle) sandboxPage = wikipedia.Page(mySite, localSandboxTitle) try: text = sandboxPage.get() - translatedContent = wikipedia.translate(mySite, content) + translatedContent = wikipedia.gettranslation(mySite, content) if text.strip() == translatedContent.strip(): wikipedia.output(u'The sandbox is still clean, no change necessary.') else: - translatedMsg = wikipedia.translate(mySite, u'Robot: This page will automatically be cleaned.') + translatedMsg = wikipedia.gettranslation(mySite, u'Robot: This page will automatically be cleaned.') sandboxPage.put(translatedContent, translatedMsg) except wikipedia.EditConflict: wikipedia.output(u'*** Loading again because of edit conflict.\n')
Modified: branches/pywikipedia/messages/commons_link.py =================================================================== --- branches/pywikipedia/messages/commons_link.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/commons_link.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -70,7 +70,7 @@ self.acceptall = True if self.acceptall or choice in ['y', 'Y']: try: - msg = wikipedia.translate(wikipedia.getSite(), u'Robot: Include commons template') + msg = wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Include commons template') page.put(text, msg) except wikipedia.EditConflict: wikipedia.output(u'Skipping %s because of edit conflict' % (page.title())) @@ -114,7 +114,7 @@ self.acceptall = True if self.acceptall or choice in ['y', 'Y']: try: - msg = wikipedia.translate(wikipedia.getSite(), u'Robot: Include commonscat template') + msg = wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Include commonscat template') page.put(text, msg) except wikipedia.EditConflict: wikipedia.output(u'Skipping %s because of edit conflict' % (page.title()))
Modified: branches/pywikipedia/messages/copyright_clean.py =================================================================== --- branches/pywikipedia/messages/copyright_clean.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/copyright_clean.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -62,7 +62,7 @@ wikipedia.output('* ' + revid) return False
-cat = catlib.Category(wikipedia.getSite(), 'Category:%s' % wikipedia.translate(wikipedia.getSite(), reports_cat)) +cat = catlib.Category(wikipedia.getSite(), 'Category:%s' % wikipedia.gettranslation(wikipedia.getSite(), reports_cat)) gen = pagegenerators.CategorizedPageGenerator(cat, recurse = True)
for page in gen: @@ -132,7 +132,7 @@ output += data[next_head.start():next_head.end()] + '\n' add_separator = False
- add_comment = u'%s: %s' % (wikipedia.translate(wikipedia.getSite(), u'Removing'),", ".join(comment_entry)) + add_comment = u'%s: %s' % (wikipedia.gettranslation(wikipedia.getSite(), u'Removing'),", ".join(comment_entry))
# remove useless newlines output = re.sub("(?m)^\n", "", output)
Modified: branches/pywikipedia/messages/cosmetic_changes.py =================================================================== --- branches/pywikipedia/messages/cosmetic_changes.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/cosmetic_changes.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -304,7 +304,7 @@ self.generator = generator self.acceptall = acceptall # Load default summary message. - wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Robot: Cosmetic changes')) + wikipedia.setAction(wikipedia.gettranslation(wikipedia.getSite(), u'Robot: Cosmetic changes'))
def treat(self, page): try:
Modified: branches/pywikipedia/messages/messages.py =================================================================== --- branches/pywikipedia/messages/messages.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/messages.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -9,7 +9,7 @@ u'Adding "test" to the beginning of the page.': { 'nl':u'"Test" toegevoegd aan het begin van de pagina.', - 'fa':u'افزودن «Test» به ابتدای صفحه', + 'fa':u'افزودن «Test» به ابتدای صفحه', }, #capitalize_redirects.py u'Robot: Create redirect to [[%s]]':
Modified: branches/pywikipedia/messages/wikipedia.py =================================================================== --- branches/pywikipedia/messages/wikipedia.py 2007-11-01 17:24:35 UTC (rev 4495) +++ branches/pywikipedia/messages/wikipedia.py 2007-11-02 07:56:45 UTC (rev 4496) @@ -4535,7 +4535,7 @@ # languages to be: # xx:, then fr:, then ru:, then en: # you let altlang return ['fr','ru']. -# This code is used by translate() below. +# This code is used by translate() and gettranslation() below.
def altlang(code): if code=='aa': @@ -4634,9 +4634,38 @@ return ['io','eo'] return []
+def translate(code, xdict): + """Return the most appropriate translation from a translation dict.
-def translate(code, msg): + Given a language code and a dictionary, returns the dictionary's value for + key 'code' if this key exists; otherwise tries to return a value for an + alternative language that is most applicable to use on the Wikipedia in + language 'code'. + + The language itself is always checked first, then languages that + have been defined to be alternatives, and finally English. If none of + the options gives result, we just take the first language in the + list. + + User created bots can use translate() for ease. Built-in bots are suposed + to use gettranslation() instead, which allows a further advance towards + gettext(). """ + # If a site is given instead of a code, use its language + if hasattr(code,'lang'): + code = code.lang + + if xdict.has_key(code): + return xdict[code] + for alt in altlang(code): + if xdict.has_key(alt): + return xdict[alt] + if xdict.has_key('en'): + return xdict['en'] + return xdict.values()[0] + +def gettranslation(code, msg): + """ Given a language code and a message name, it returns the value appropriate edit comment, according to message dictionary in message.py, if this key exists; otherwise tries to return a value for an alternative language that @@ -4645,6 +4674,11 @@ have been defined to be alternatives, and finally English. If none of the options gives result, we just take the first language in the list. + + User bots can use translate() instead, and include the translations in a + dictionary inside the bot code. Built-in bots use gettranslation() which + requires the messages to be included in message.py; this allows further + advancements by using gettext() in future. """ edit_summaries = messages.edit_summaries
pywikipedia-l@lists.wikimedia.org