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