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