##// END OF EJS Templates
i18n: use absolute_import
Gregory Szorc -
r25955:2c07c688 default
parent child Browse files
Show More
@@ -1,90 +1,96 b''
1 # i18n.py - internationalization support for mercurial
1 # i18n.py - internationalization support for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 import encoding
8 from __future__ import absolute_import
9 import gettext as gettextmod, sys, os, locale
9
10 import gettext as gettextmod
11 import locale
12 import os
13 import sys
14
15 from . import encoding
10
16
11 # modelled after templater.templatepath:
17 # modelled after templater.templatepath:
12 if getattr(sys, 'frozen', None) is not None:
18 if getattr(sys, 'frozen', None) is not None:
13 module = sys.executable
19 module = sys.executable
14 else:
20 else:
15 module = __file__
21 module = __file__
16
22
17
23
18 _languages = None
24 _languages = None
19 if (os.name == 'nt'
25 if (os.name == 'nt'
20 and 'LANGUAGE' not in os.environ
26 and 'LANGUAGE' not in os.environ
21 and 'LC_ALL' not in os.environ
27 and 'LC_ALL' not in os.environ
22 and 'LC_MESSAGES' not in os.environ
28 and 'LC_MESSAGES' not in os.environ
23 and 'LANG' not in os.environ):
29 and 'LANG' not in os.environ):
24 # Try to detect UI language by "User Interface Language Management" API
30 # Try to detect UI language by "User Interface Language Management" API
25 # if no locale variables are set. Note that locale.getdefaultlocale()
31 # if no locale variables are set. Note that locale.getdefaultlocale()
26 # uses GetLocaleInfo(), which may be different from UI language.
32 # uses GetLocaleInfo(), which may be different from UI language.
27 # (See http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx )
33 # (See http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx )
28 try:
34 try:
29 import ctypes
35 import ctypes
30 langid = ctypes.windll.kernel32.GetUserDefaultUILanguage()
36 langid = ctypes.windll.kernel32.GetUserDefaultUILanguage()
31 _languages = [locale.windows_locale[langid]]
37 _languages = [locale.windows_locale[langid]]
32 except (ImportError, AttributeError, KeyError):
38 except (ImportError, AttributeError, KeyError):
33 # ctypes not found or unknown langid
39 # ctypes not found or unknown langid
34 pass
40 pass
35
41
36 _ugettext = None
42 _ugettext = None
37
43
38 def setdatapath(datapath):
44 def setdatapath(datapath):
39 localedir = os.path.join(datapath, 'locale')
45 localedir = os.path.join(datapath, 'locale')
40 t = gettextmod.translation('hg', localedir, _languages, fallback=True)
46 t = gettextmod.translation('hg', localedir, _languages, fallback=True)
41 global _ugettext
47 global _ugettext
42 _ugettext = t.ugettext
48 _ugettext = t.ugettext
43
49
44 _msgcache = {}
50 _msgcache = {}
45
51
46 def gettext(message):
52 def gettext(message):
47 """Translate message.
53 """Translate message.
48
54
49 The message is looked up in the catalog to get a Unicode string,
55 The message is looked up in the catalog to get a Unicode string,
50 which is encoded in the local encoding before being returned.
56 which is encoded in the local encoding before being returned.
51
57
52 Important: message is restricted to characters in the encoding
58 Important: message is restricted to characters in the encoding
53 given by sys.getdefaultencoding() which is most likely 'ascii'.
59 given by sys.getdefaultencoding() which is most likely 'ascii'.
54 """
60 """
55 # If message is None, t.ugettext will return u'None' as the
61 # If message is None, t.ugettext will return u'None' as the
56 # translation whereas our callers expect us to return None.
62 # translation whereas our callers expect us to return None.
57 if message is None or not _ugettext:
63 if message is None or not _ugettext:
58 return message
64 return message
59
65
60 if message not in _msgcache:
66 if message not in _msgcache:
61 if type(message) is unicode:
67 if type(message) is unicode:
62 # goofy unicode docstrings in test
68 # goofy unicode docstrings in test
63 paragraphs = message.split(u'\n\n')
69 paragraphs = message.split(u'\n\n')
64 else:
70 else:
65 paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
71 paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
66 # Be careful not to translate the empty string -- it holds the
72 # Be careful not to translate the empty string -- it holds the
67 # meta data of the .po file.
73 # meta data of the .po file.
68 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
74 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
69 try:
75 try:
70 # encoding.tolocal cannot be used since it will first try to
76 # encoding.tolocal cannot be used since it will first try to
71 # decode the Unicode string. Calling u.decode(enc) really
77 # decode the Unicode string. Calling u.decode(enc) really
72 # means u.encode(sys.getdefaultencoding()).decode(enc). Since
78 # means u.encode(sys.getdefaultencoding()).decode(enc). Since
73 # the Python encoding defaults to 'ascii', this fails if the
79 # the Python encoding defaults to 'ascii', this fails if the
74 # translated string use non-ASCII characters.
80 # translated string use non-ASCII characters.
75 _msgcache[message] = u.encode(encoding.encoding, "replace")
81 _msgcache[message] = u.encode(encoding.encoding, "replace")
76 except LookupError:
82 except LookupError:
77 # An unknown encoding results in a LookupError.
83 # An unknown encoding results in a LookupError.
78 _msgcache[message] = message
84 _msgcache[message] = message
79 return _msgcache[message]
85 return _msgcache[message]
80
86
81 def _plain():
87 def _plain():
82 if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
88 if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
83 return False
89 return False
84 exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',')
90 exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',')
85 return 'i18n' not in exceptions
91 return 'i18n' not in exceptions
86
92
87 if _plain():
93 if _plain():
88 _ = lambda message: message
94 _ = lambda message: message
89 else:
95 else:
90 _ = gettext
96 _ = gettext
General Comments 0
You need to be logged in to leave comments. Login now