mercurial/i18n.py
changeset 22638 0d0350cfc7ab
parent 21987 4953cd193e84
child 23031 3c0983cc279e
--- a/mercurial/i18n.py	Sun Sep 28 16:57:47 2014 +0200
+++ b/mercurial/i18n.py	Sun Sep 28 16:57:47 2014 +0200
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 
 import encoding
-import gettext, sys, os, locale
+import gettext as gettextmod, sys, os, locale
 
 # modelled after templater.templatepath:
 if getattr(sys, 'frozen', None) is not None:
@@ -14,11 +14,6 @@
 else:
     module = __file__
 
-base = os.path.dirname(module)
-for dir in ('.', '..'):
-    localedir = os.path.join(base, dir, 'locale')
-    if os.path.isdir(localedir):
-        break
 
 _languages = None
 if (os.name == 'nt'
@@ -38,7 +33,13 @@
         # ctypes not found or unknown langid
         pass
 
-t = gettext.translation('hg', localedir, _languages, fallback=True)
+_ugettext = None
+
+def setdatapath(datapath):
+    localedir = os.path.join(datapath, 'locale')
+    t = gettextmod.translation('hg', localedir, _languages, fallback=True)
+    global _ugettext
+    _ugettext = t.ugettext
 
 def gettext(message):
     """Translate message.
@@ -51,7 +52,7 @@
     """
     # If message is None, t.ugettext will return u'None' as the
     # translation whereas our callers expect us to return None.
-    if message is None:
+    if message is None or not _ugettext:
         return message
 
     if type(message) is unicode:
@@ -61,7 +62,7 @@
         paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
     # Be careful not to translate the empty string -- it holds the
     # meta data of the .po file.
-    u = u'\n\n'.join([p and t.ugettext(p) or '' for p in paragraphs])
+    u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
     try:
         # encoding.tolocal cannot be used since it will first try to
         # decode the Unicode string. Calling u.decode(enc) really