jenkins-bot has submitted this change and it was merged.

Change subject: plugin: get the title of langlinks for a page
......................................................................


plugin: get the title of langlinks for a page

And assume this is a valid translation of the page title.

Change-Id: I62a2a5f41e75a79ddecec93463b302d546584cbd
Signed-off-by: Loic Dachary <l...@dachary.org>
---
M FLOSSbot/plugin.py
M tests/test_plugin.py
2 files changed, 20 insertions(+), 0 deletions(-)

Approvals:
  Dachary: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/FLOSSbot/plugin.py b/FLOSSbot/plugin.py
index a0d309a..cba787e 100644
--- a/FLOSSbot/plugin.py
+++ b/FLOSSbot/plugin.py
@@ -32,6 +32,7 @@
         self.args = args
         self.bot = bot
         self.reset_cache()
+        self.title_translation = {}
         self.dbname2item = {}
 
     @staticmethod
@@ -277,6 +278,17 @@
             log.debug("GET failed with " + str(e))
             return None
 
+    def translate_title(self, title, lang):
+        if title not in self.title_translation:
+            site = pywikibot.site.APISite.fromDBName('enwiki')
+            translation = {'en': title}
+            p = pywikibot.Page(site, title)
+            for l in p.langlinks():
+                # License (juridique): the (...) is for disambiguation
+                translation[l.site.code] = re.sub('\s*\([^)]*\)', '', l.title)
+            self.title_translation[title] = translation
+        return self.title_translation[title].get(lang)
+
     def get_redirects(self, title, lang):
         log.debug("get_redirects " + title + " " + lang)
         site = pywikibot.site.APISite.fromDBName(lang + 'wiki')
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 8a8b367..1edd2fa 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -151,6 +151,14 @@
         found = plugin.search_entity(plugin.bot.site, name, type='item')
         assert found.getID() == second.getID()
 
+    def test_translate_title(self):
+        bot = Bot.factory(['--verbose'])
+        plugin = Plugin(bot, bot.args)
+        assert 'GNU Emacs' == plugin.translate_title('GNU Emacs', 'fr')
+        assert 'ГНУ Емакс' == plugin.translate_title('GNU Emacs', 'sr')
+        assert 'Licence' == plugin.translate_title('License', 'fr')
+        assert plugin.translate_title('License', '??') is None
+
     def test_get_redirects(self):
         bot = Bot.factory(['--verbose'])
         plugin = Plugin(bot, bot.args)

-- 
To view, visit https://gerrit.wikimedia.org/r/316056
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I62a2a5f41e75a79ddecec93463b302d546584cbd
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/bots/FLOSSbot
Gerrit-Branch: master
Gerrit-Owner: Dachary <l...@dachary.org>
Gerrit-Reviewer: Dachary <l...@dachary.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to