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