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 import encoding |
8 import encoding |
9 import gettext, sys, os, locale |
9 import gettext as gettextmod, sys, os, locale |
10 |
10 |
11 # modelled after templater.templatepath: |
11 # modelled after templater.templatepath: |
12 if getattr(sys, 'frozen', None) is not None: |
12 if getattr(sys, 'frozen', None) is not None: |
13 module = sys.executable |
13 module = sys.executable |
14 else: |
14 else: |
15 module = __file__ |
15 module = __file__ |
16 |
16 |
17 base = os.path.dirname(module) |
|
18 for dir in ('.', '..'): |
|
19 localedir = os.path.join(base, dir, 'locale') |
|
20 if os.path.isdir(localedir): |
|
21 break |
|
22 |
17 |
23 _languages = None |
18 _languages = None |
24 if (os.name == 'nt' |
19 if (os.name == 'nt' |
25 and 'LANGUAGE' not in os.environ |
20 and 'LANGUAGE' not in os.environ |
26 and 'LC_ALL' not in os.environ |
21 and 'LC_ALL' not in os.environ |
36 _languages = [locale.windows_locale[langid]] |
31 _languages = [locale.windows_locale[langid]] |
37 except (ImportError, AttributeError, KeyError): |
32 except (ImportError, AttributeError, KeyError): |
38 # ctypes not found or unknown langid |
33 # ctypes not found or unknown langid |
39 pass |
34 pass |
40 |
35 |
41 t = gettext.translation('hg', localedir, _languages, fallback=True) |
36 _ugettext = None |
|
37 |
|
38 def setdatapath(datapath): |
|
39 localedir = os.path.join(datapath, 'locale') |
|
40 t = gettextmod.translation('hg', localedir, _languages, fallback=True) |
|
41 global _ugettext |
|
42 _ugettext = t.ugettext |
42 |
43 |
43 def gettext(message): |
44 def gettext(message): |
44 """Translate message. |
45 """Translate message. |
45 |
46 |
46 The message is looked up in the catalog to get a Unicode string, |
47 The message is looked up in the catalog to get a Unicode string, |
49 Important: message is restricted to characters in the encoding |
50 Important: message is restricted to characters in the encoding |
50 given by sys.getdefaultencoding() which is most likely 'ascii'. |
51 given by sys.getdefaultencoding() which is most likely 'ascii'. |
51 """ |
52 """ |
52 # If message is None, t.ugettext will return u'None' as the |
53 # If message is None, t.ugettext will return u'None' as the |
53 # translation whereas our callers expect us to return None. |
54 # translation whereas our callers expect us to return None. |
54 if message is None: |
55 if message is None or not _ugettext: |
55 return message |
56 return message |
56 |
57 |
57 if type(message) is unicode: |
58 if type(message) is unicode: |
58 # goofy unicode docstrings in test |
59 # goofy unicode docstrings in test |
59 paragraphs = message.split(u'\n\n') |
60 paragraphs = message.split(u'\n\n') |
60 else: |
61 else: |
61 paragraphs = [p.decode("ascii") for p in message.split('\n\n')] |
62 paragraphs = [p.decode("ascii") for p in message.split('\n\n')] |
62 # Be careful not to translate the empty string -- it holds the |
63 # Be careful not to translate the empty string -- it holds the |
63 # meta data of the .po file. |
64 # meta data of the .po file. |
64 u = u'\n\n'.join([p and t.ugettext(p) or '' for p in paragraphs]) |
65 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs]) |
65 try: |
66 try: |
66 # encoding.tolocal cannot be used since it will first try to |
67 # encoding.tolocal cannot be used since it will first try to |
67 # decode the Unicode string. Calling u.decode(enc) really |
68 # decode the Unicode string. Calling u.decode(enc) really |
68 # means u.encode(sys.getdefaultencoding()).decode(enc). Since |
69 # means u.encode(sys.getdefaultencoding()).decode(enc). Since |
69 # the Python encoding defaults to 'ascii', this fails if the |
70 # the Python encoding defaults to 'ascii', this fails if the |