cmds: properly support xfce4-terminal and gnome-terminal
[git-cola.git] / cola / i18n.py
blobdfebc7231ef1bb4b5064b6e505b406865d7a72b8
1 """i18n and l10n support for git-cola"""
2 from __future__ import division, absolute_import, unicode_literals
4 import gettext as _gettext
5 import os
6 import sys
8 from cola import compat
9 from cola import core
10 from cola import resources
12 _null_translation = _gettext.NullTranslations()
13 # Python 3 compat
14 if not hasattr(_null_translation, 'ugettext'):
15 _null_translation.ugettext = _null_translation.gettext
16 _null_translation.ungettext = _null_translation.ngettext
17 _translation = _null_translation
20 def gettext(s):
21 txt = _translation.ugettext(s)
22 if txt[-6:-4] == '@@': # handle @@verb / @@noun
23 txt = txt[:-6]
24 return txt
27 def ngettext(s, p, n):
28 return _translation.ungettext(s, p, n)
31 def N_(s):
32 return gettext(s)
35 def install(locale):
36 global _translation
37 if sys.platform == 'win32':
38 _check_win32_locale()
39 if locale:
40 compat.setenv('LANGUAGE', locale)
41 compat.setenv('LANG', locale)
42 compat.setenv('LC_MESSAGES', locale)
43 _install_custom_language()
44 _gettext.textdomain('messages')
45 _translation = _gettext.translation('git-cola',
46 localedir=_get_locale_dir(),
47 fallback=True)
48 # Python 3 compat
49 if not hasattr(_translation, 'ugettext'):
50 _translation.ugettext = _translation.gettext
51 _translation.ungettext = _translation.ngettext
53 def uninstall():
54 global _translation
55 _translation = _null_translation
58 def _get_locale_dir():
59 return resources.prefix('share', 'locale')
62 def _install_custom_language():
63 """Allow a custom language to be set in ~/.config/git-cola/language"""
64 lang_file = resources.config_home('language')
65 if not core.exists(lang_file):
66 return
67 try:
68 lang = core.read(lang_file).strip()
69 except:
70 return
71 if lang:
72 compat.setenv('LANGUAGE', lang)
75 def _check_win32_locale():
76 for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
77 if os.environ.get(i):
78 break
79 else:
80 lang = None
81 import locale
82 try:
83 import ctypes
84 except ImportError:
85 # use only user's default locale
86 lang = locale.getdefaultlocale()[0]
87 else:
88 # using ctypes to determine all locales
89 lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
90 lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
91 if lcid_user != lcid_system:
92 lcid = [lcid_user, lcid_system]
93 else:
94 lcid = [lcid_user]
95 lang = [locale.windows_locale.get(i) for i in lcid]
96 lang = ':'.join([i for i in lang if i])
97 # set lang code for gettext
98 if lang:
99 compat.setenv('LANGUAGE', lang)