Show More
@@ -1,26 +1,51 | |||
|
1 | 1 | """ |
|
2 | 2 | i18n.py - internationalization support for mercurial |
|
3 | 3 | |
|
4 | 4 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms |
|
7 | 7 | of the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | import gettext, sys, os |
|
11 | 11 | |
|
12 | 12 | # modelled after templater.templatepath: |
|
13 | 13 | if hasattr(sys, 'frozen'): |
|
14 | 14 | module = sys.executable |
|
15 | 15 | else: |
|
16 | 16 | module = __file__ |
|
17 | 17 | |
|
18 | 18 | base = os.path.dirname(module) |
|
19 | 19 | for dir in ('.', '..'): |
|
20 | 20 | localedir = os.path.normpath(os.path.join(base, dir, 'locale')) |
|
21 | 21 | if os.path.isdir(localedir): |
|
22 | 22 | break |
|
23 | 23 | |
|
24 | 24 | t = gettext.translation('hg', localedir, fallback=True) |
|
25 | gettext = t.gettext | |
|
25 | ||
|
26 | def gettext(message): | |
|
27 | """Translate message. | |
|
28 | ||
|
29 | The message is looked up in the catalog to get a Unicode string, | |
|
30 | which is encoded in the local encoding before being returned. | |
|
31 | ||
|
32 | Important: message is restricted to characters in the encoding | |
|
33 | given by sys.getdefaultencoding() which is most likely 'ascii'. | |
|
34 | """ | |
|
35 | # If message is None, t.ugettext will return u'None' as the | |
|
36 | # translation whereas our callers expect us to return None. | |
|
37 | if message is None: | |
|
38 | return message | |
|
39 | ||
|
40 | # We cannot just run the text through util.tolocal since that | |
|
41 | # leads to infinite recursion when util._encoding is invalid. | |
|
42 | try: | |
|
43 | u = t.ugettext(message) | |
|
44 | return u.encode(util._encoding, "replace") | |
|
45 | except LookupError: | |
|
46 | return message | |
|
47 | ||
|
26 | 48 | _ = gettext |
|
49 | ||
|
50 | # Moved after _ because of circular import. | |
|
51 | import util |
General Comments 0
You need to be logged in to leave comments.
Login now