Kiln » TortoiseHg » TortoiseHg
Clone URL:  
i18n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# i18n.py - TortoiseHg internationalization code # # Copyright 2009 Steve Borho <steve@borho.org> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import gettext, os, locale from mercurial import util from tortoisehg.util import paths _localeenvs = ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG') def _defaultlanguage(): if os.name != 'nt' or util.any(e in os.environ for e in _localeenvs): return # honor posix-style env var # On Windows, UI language can be determined by GetUserDefaultUILanguage(), # but gettext doesn't take it into account. # Note that locale.getdefaultlocale() uses GetLocaleInfo(), which may be # different from UI language. # # For details, please read "User Interface Language Management": # http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx try: from ctypes import windll # requires Python>=2.5 langid = windll.kernel32.GetUserDefaultUILanguage() return locale.windows_locale[langid] except (ImportError, AttributeError, KeyError): pass def setlanguage(lang=None): """Change translation catalog to the specified language""" global t, language if not lang: lang = _defaultlanguage() opts = {} if lang: opts['languages'] = (lang,) t = gettext.translation('tortoisehg', paths.get_locale_path(), fallback=True, **opts) language = lang or locale.getdefaultlocale(_localeenvs)[0] setlanguage() def availablelanguages(): """List up language code of which message catalog is available""" basedir = paths.get_locale_path() def mopath(lang): return os.path.join(basedir, lang, 'LC_MESSAGES', 'tortoisehg.mo') if os.path.exists(basedir): # locale/ is an install option langs = [e for e in os.listdir(basedir) if os.path.exists(mopath(e))] else: langs = [] langs.append('en') # means null translation return sorted(langs) def _(message, context=''): if context: sep = '\004' tmsg = t.gettext(context + sep + message) if sep not in tmsg: return tmsg return t.gettext(message) def ngettext(singular, plural, n): return t.ngettext(singular, plural, n) def agettext(message, context=''): """Translate message and convert to local encoding such as 'ascii' before being returned. Only use this if you need to output translated messages to command-line interface (ie: Windows Command Prompt). """ try: from tortoisehg.util import hglib u = _(message, context) return hglib.fromutf(u) except (LookupError, UnicodeEncodeError): return message class keepgettext(object): def _(self, message, context=''): return {'id': message, 'str': _(message, context)}