XZise has uploaded a new change for review.

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

Change subject: [FIX] i18n: twntranslate gets used code
......................................................................

[FIX] i18n: twntranslate gets used code

It fixes an issue with 5767d73d, that twntranslate didn't get the language code
which was actually used. With fallback=True twtranslate might use another
language than the language given in the first place and before that patch other
scripts could supply a list and twtranslate would modify the last entry so that
it contained that language which was actually used.

With that patch it just removed the last entry from the list and added later
the same value again.

Change-Id: I819b18f82809380964aec51d7988396457725c8b
---
M pywikibot/i18n.py
1 file changed, 49 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/95/213795/1

diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 67b227f..ba69104 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -34,12 +34,14 @@
 import pkgutil
 
 from collections import defaultdict
+from warnings import warn
 
 from pywikibot import Error
 from .plural import plural_rules
 
 import pywikibot
 
+from pywikibot import site
 from . import config2 as config
 
 if sys.version_info[0] > 2:
@@ -458,11 +460,50 @@
     fallback parameter must be True for i18n and False for L10N or testing
     purposes.
 
-    @param code: The language code
+    @param code: When it's a site it's using the code attribute and otherwise 
it
+        is using the value directly.
+    @type code: BaseSite or str
     @param twtitle: The TranslateWiki string title, in <package>-<key> format
     @param parameters: For passing parameters.
     @param fallback: Try an alternate language code
     @type fallback: boolean
+    """
+    # For backwards compatibility still support lists, when twntranslate was
+    # calling twtranslate and needed a way to get the used language code back
+    if isinstance(code, list):
+        warn('The code argument should not be a list but either a BaseSite or '
+             'a str/unicode.', DeprecationWarning, 2)
+        lang = code[-1]
+    else:
+        lang = code
+    trans, lang = _twtranslate(lang, twtitle, fallback)
+    # Write the value back as it did previously
+    if isinstance(code, list):
+        code[-1] = lang
+    if parameters:
+        return trans % parameters
+    else:
+        return trans
+
+
+def _get_language_code(code):
+    """Get the code if it's a BaseSite and code otherwise."""
+    # If a site is given instead of a code, use its language
+    if isinstance(code, site.BaseSite):
+        return code.code
+    else:
+        return code
+
+
+def _twtranslate(code, twtitle, fallback=True):
+    """
+    Translate the given twtitle according to code.
+
+    It is using the same parameters as L{twtranslate} without the 'parameters'
+    parameter.
+
+    @return: The translated text and used language code
+    @rtype: str, str
     """
     if not messages_available():
         raise TranslationError(
@@ -471,25 +512,15 @@
             'Read https://mediawiki.org/wiki/PWB/i18n'
             % (_messages_package_name, twtitle))
 
-    code_needed = False
-    # If a site is given instead of a code, use its language
-    if hasattr(code, 'code'):
-        lang = code.code
-    # check whether we need the language code back
-    elif isinstance(code, list):
-        lang = code.pop()
-        code_needed = True
-    else:
-        lang = code
+    langs = [_get_language_code(code)]
 
     # There are two possible failure modes: the translation dict might not have
     # the language altogether, or a specific key could be untranslated. Both
     # modes are caught with the KeyError.
-    langs = [lang]
     if fallback:
-        langs += _altlang(lang) + ['en']
-    for alt in langs:
-        trans = _get_translation(alt, twtitle)
+        langs += _altlang(langs[0]) + ['en']
+    for lang in langs:
+        trans = _get_translation(lang, twtitle)
         if trans:
             break
     else:
@@ -497,13 +528,7 @@
             'No English translation has been defined for TranslateWiki key'
             ' %r\nIt can happen due to lack of i18n submodule or files. '
             'Read https://mediawiki.org/wiki/PWB/i18n' % twtitle)
-    # send the language code back via the given list
-    if code_needed:
-        code.append(lang)
-    if parameters:
-        return trans % parameters
-    else:
-        return trans
+    return trans, lang
 
 
 # Maybe this function should be merged with twtranslate
@@ -554,14 +579,7 @@
     @param parameters: For passing (plural) parameters.
 
     """
-    # If a site is given instead of a code, use its language
-    if hasattr(code, 'code'):
-        code = code.code
-    # we send the code via list and get the alternate code back
-    code = [code]
-    trans = twtranslate(code, twtitle)
-    # get the alternate language code modified by twtranslate
-    lang = code.pop()
+    trans, lang = _twtranslate(code, twtitle)
     # check for PLURAL variants
     trans = _extract_plural(lang, trans, parameters)
     # we always have a dict for replacement of translatewiki messages
@@ -586,9 +604,7 @@
     @param code: The language code
     @param twtitle: The TranslateWiki string title, in <package>-<key> format
     """
-    # If a site is given instead of a code, use its language
-    if hasattr(code, 'code'):
-        code = code.code
+    code = _get_language_code(code)
     transdict = _get_translation(code, twtitle)
     if transdict is None:
         return False

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I819b18f82809380964aec51d7988396457725c8b
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <commodorefabia...@gmx.de>

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

Reply via email to