http://www.mediawiki.org/wiki/Special:Code/pywikipedia/8847
Revision: 8847
Author: valhallasw
Date: 2011-01-16 20:16:10 +0000 (Sun, 16 Jan 2011)
Log Message:
-----------
Documentation update for i18n.py
Modified Paths:
--------------
branches/rewrite/pywikibot/i18n.py
Modified: branches/rewrite/pywikibot/i18n.py
===================================================================
--- branches/rewrite/pywikibot/i18n.py 2011-01-16 20:16:05 UTC (rev 8846)
+++ branches/rewrite/pywikibot/i18n.py 2011-01-16 20:16:10 UTC (rev 8847)
@@ -1,6 +1,12 @@
-#----------------
-# I18N functions
-#----------------
+# -*- coding: utf-8 -*-
+""" Various i18n functions, both for the internal translation system
+ and for TranslateWiki-based translations
+"""
+#
+# (C) Pywikipedia bot team, 2004-2011
+#
+# Distributed under the terms of the MIT license.
+#
from pywikibot import Error
@@ -9,15 +15,17 @@
# 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 twtranslate() below.
def _altlang(code):
"""Define fallback languages for particular languages.
If no translation is available to a specified language, translate() will
try each of the specified fallback languages, in order, until it finds
- one with a translation, or '_default' as a last resort.
-
+ one with a translation, with 'en' and '_default' as a last resort.
+
+ For example, if for language 'xx', you want the preference of languages
+ to be: xx > fr > ru > en, you let altlang return ['fr', 'ru'].
"""
#Amharic
if code in ['aa', 'om']:
@@ -189,14 +197,17 @@
class TranslationError(Error):
+ """ Raised when no correct translation could be found """
pass
def twtranslate(code, twtitle, parameters=None):
""" Uses TranslateWiki files to provide translations based on the TW title
twtitle, which corresponds to a page on TW.
- @param parameters is for future addition of plural support
- @param twtitle is the TranslateWiki string title, in <package>-<key> format
+ @param code The language code
+ @param twtitle The TranslateWiki string title, in <package>-<key> format
+ @param parameters For passing parameters. In the future, this will
+ be used for plural support.
The translations are retrieved from i18n.<package>, based on the callers
import table.
@@ -204,8 +215,9 @@
package = twtitle.split("-")[0]
transdict = getattr(__import__("i18n", fromlist=[package]), package).msg
- # There are two possible failure modes: the msg dict might not have the
- # language altogether, or a specific key could be untranslated.
+ # 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.
trans = None
try:
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/8846
Revision: 8846
Author: valhallasw
Date: 2011-01-16 20:16:05 +0000 (Sun, 16 Jan 2011)
Log Message:
-----------
Implemented fallback translations for TW translations.
Modified Paths:
--------------
branches/rewrite/pywikibot/i18n.py
Modified: branches/rewrite/pywikibot/i18n.py
===================================================================
--- branches/rewrite/pywikibot/i18n.py 2011-01-16 20:16:01 UTC (rev 8845)
+++ branches/rewrite/pywikibot/i18n.py 2011-01-16 20:16:05 UTC (rev 8846)
@@ -2,6 +2,8 @@
# I18N functions
#----------------
+from pywikibot import Error
+
# Languages to use for comment text after the actual language but before
# en:. For example, if for language 'xx', you want the preference of
# languages to be:
@@ -185,6 +187,10 @@
return xdict['en']
return xdict.values()[0]
+
+class TranslationError(Error):
+ pass
+
def twtranslate(code, twtitle, parameters=None):
""" Uses TranslateWiki files to provide translations based on the TW title
twtitle, which corresponds to a page on TW.
@@ -197,8 +203,24 @@
"""
package = twtitle.split("-")[0]
transdict = getattr(__import__("i18n", fromlist=[package]), package).msg
- trans = translate(code, transdict)[twtitle]
+ # There are two possible failure modes: the msg dict might not have the
+ # language altogether, or a specific key could be untranslated.
+
+ trans = None
+ try:
+ trans = transdict[code][twtitle]
+ except KeyError:
+ # try alternative languages and English
+ for alt in _altlang(code) + ['en']:
+ try:
+ trans = transdict[alt][twtitle]
+ break
+ except KeyError:
+ continue
+ if not trans:
+ raise TranslationError("No English translation has been defined for TranslateWiki key %r" % twtitle)
+
if parameters:
return trans % parameters
else:
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/8844
Revision: 8844
Author: valhallasw
Date: 2011-01-16 20:15:55 +0000 (Sun, 16 Jan 2011)
Log Message:
-----------
Revert fallback implementation from r8840/r8843
These implementations do not cover fallback languages correctly.
Modified Paths:
--------------
branches/rewrite/pywikibot/i18n.py
Modified: branches/rewrite/pywikibot/i18n.py
===================================================================
--- branches/rewrite/pywikibot/i18n.py 2011-01-16 16:14:50 UTC (rev 8843)
+++ branches/rewrite/pywikibot/i18n.py 2011-01-16 20:15:55 UTC (rev 8844)
@@ -174,11 +174,10 @@
if hasattr(code,'lang'):
code = code.lang
- # check if the subkey exists anyway
- if code in xdict and xdict[code]:
+ if code in xdict:
return xdict[code]
for alt in _altlang(code):
- if alt in xdict and xdict[alt]:
+ if alt in xdict:
return xdict[alt]
if '_default' in xdict:
return xdict['_default']
@@ -198,11 +197,7 @@
"""
package = twtitle.split("-")[0]
transdict = getattr(__import__("i18n", fromlist=[package]), package).msg
- twmsg = translate(code, transdict)
- if twtitle in twmsg:
- trans = twmsg[twtitle]
- else:
- trans = transdict['en'][twtitle]
+ trans = translate(code, transdict)[twtitle]
if parameters:
return trans % parameters
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/8843
Revision: 8843
Author: xqt
Date: 2011-01-16 16:14:50 +0000 (Sun, 16 Jan 2011)
Log Message:
-----------
check whether a language has no translations at all and the subkey exists. Otherwise use 'en' as default key.
Modified Paths:
--------------
branches/rewrite/pywikibot/i18n.py
Modified: branches/rewrite/pywikibot/i18n.py
===================================================================
--- branches/rewrite/pywikibot/i18n.py 2011-01-16 15:33:14 UTC (rev 8842)
+++ branches/rewrite/pywikibot/i18n.py 2011-01-16 16:14:50 UTC (rev 8843)
@@ -198,7 +198,11 @@
"""
package = twtitle.split("-")[0]
transdict = getattr(__import__("i18n", fromlist=[package]), package).msg
- trans = translate(code, transdict)[twtitle]
+ twmsg = translate(code, transdict)
+ if twtitle in twmsg:
+ trans = twmsg[twtitle]
+ else:
+ trans = transdict['en'][twtitle]
if parameters:
return trans % parameters
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/8840
Revision: 8840
Author: xqt
Date: 2011-01-16 15:14:37 +0000 (Sun, 16 Jan 2011)
Log Message:
-----------
check if the subkey exists anyway
Modified Paths:
--------------
branches/rewrite/pywikibot/i18n.py
Modified: branches/rewrite/pywikibot/i18n.py
===================================================================
--- branches/rewrite/pywikibot/i18n.py 2011-01-16 14:52:17 UTC (rev 8839)
+++ branches/rewrite/pywikibot/i18n.py 2011-01-16 15:14:37 UTC (rev 8840)
@@ -174,10 +174,11 @@
if hasattr(code,'lang'):
code = code.lang
- if code in xdict:
+ # check if the subkey exists anyway
+ if code in xdict and xdict[code]:
return xdict[code]
for alt in _altlang(code):
- if alt in xdict:
+ if alt in xdict and xdict[alt]:
return xdict[alt]
if '_default' in xdict:
return xdict['_default']