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