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