http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9948
Revision: 9948
Author: xqt
Date: 2012-03-01 12:49:16 +0000 (Thu, 01 Mar 2012)
Log Message:
-----------
fallback parameter for i18n.translate()
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/i18n.py
Modified: trunk/pywikipedia/pywikibot/i18n.py
===================================================================
--- trunk/pywikipedia/pywikibot/i18n.py 2012-03-01 09:53:21 UTC (rev 9947)
+++ trunk/pywikipedia/pywikibot/i18n.py 2012-03-01 12:49:16 UTC (rev 9948)
@@ -207,13 +207,13 @@
#Default value
return []
-def translate(code, xdict):
+def translate(code, xdict, fallback=True):
"""Return the most appropriate translation from a translation dict.
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'.
+ language 'code' except fallback is False.
The language itself is always checked first, then languages that
have been defined to be alternatives, and finally English. If none of
@@ -238,12 +238,14 @@
if code in xdict:
return xdict[code]
+ if not fallback:
+ return None
for alt in _altlang(code):
if alt in xdict:
return xdict[alt]
if '_default' in xdict:
return xdict['_default']
- elif 'en' in xdict:
+ if 'en' in xdict:
return xdict['en']
return xdict.values()[0]