Patchwork [7,of,7] i18n: use datapath for i18n like for templates and help

login
register
mail settings
Submitter Mads Kiilerich
Date Sept. 28, 2014, 3:03 p.m.
Message ID <9e1e1e6845cf62993879.1411916584@localhost.localdomain>
Download mbox | patch
Permalink /patch/6011/
State Accepted
Headers show

Comments

Mads Kiilerich - Sept. 28, 2014, 3:03 p.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1411916267 -7200
#      Sun Sep 28 16:57:47 2014 +0200
# Node ID 9e1e1e6845cf6299387918804159d11e53700edd
# Parent  eccbabf21e878d6ad4aacc5168e2f3bd0be758f0
i18n: use datapath for i18n like for templates and help

To avoid circular module dependencies we initialize i18n from util when
datapath is found.
Matt Mackall - Oct. 2, 2014, 7:02 p.m.
On Sun, 2014-09-28 at 17:03 +0200, Mads Kiilerich wrote:
> # HG changeset patch
> # User Mads Kiilerich <madski@unity3d.com>
> # Date 1411916267 -7200
> #      Sun Sep 28 16:57:47 2014 +0200
> # Node ID 9e1e1e6845cf6299387918804159d11e53700edd
> # Parent  eccbabf21e878d6ad4aacc5168e2f3bd0be758f0
> i18n: use datapath for i18n like for templates and help

These are queued for default, thanks. I'm a little terrified of this
last one, but I couldn't think of a better solution.

Patch

diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -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:
@@ -15,10 +15,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 +34,13 @@  if (os.name == 'nt'
         # 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 +53,7 @@  def gettext(message):
     """
     # 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 +63,7 @@  def gettext(message):
         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
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -13,7 +13,8 @@  This contains helper routines that are i
 hide platform-specific details from the core.
 """
 
-from i18n import _
+import i18n
+_ = i18n._
 import error, osutil, encoding
 import errno, shutil, sys, tempfile, traceback
 import re as remod
@@ -467,6 +468,8 @@  if mainfrozen():
 else:
     datapath = os.path.dirname(__file__)
 
+i18n.setdatapath(datapath)
+
 _hgexecutable = None
 
 def hgexecutable():