Merge pull request #230 from filippsen/makefile-fix
[git-cola.git] / cola / i18n.py
blob76e58318b3b832efaf5b85f4b1136b5b871eb493
1 """i18n and l10n support for git-cola"""
3 import gettext as _gettext
4 import os
5 import sys
7 from cola import compat
8 from cola import core
9 from cola import resources
11 _null_translation = _gettext.NullTranslations()
12 _translation = _null_translation
15 def gettext(s):
16 txt = _translation.ugettext(s)
17 if txt[-6:-4] == '@@': # handle @@verb / @@noun
18 txt = txt[:-6]
19 return txt
22 def ngettext(s, p, n):
23 return _translation.ungettext(s, p, n)
26 def N_(s):
27 return gettext(s)
30 def install(locale):
31 global _translation
32 if sys.platform == 'win32':
33 _check_win32_locale()
34 if locale:
35 compat.setenv('LANG', locale)
36 compat.setenv('LC_MESSAGES', locale)
37 _install_custom_language()
38 _gettext.textdomain('messages')
39 _translation = _gettext.translation('git-cola',
40 localedir=_get_locale_dir(),
41 fallback=True)
43 def uninstall():
44 global _translation
45 _translation = _null_translation
48 def _get_locale_dir():
49 return resources.prefix('share', 'locale')
52 def _install_custom_language():
53 """Allow a custom language to be set in ~/.config/git-cola/language"""
54 lang_file = resources.config_home('language')
55 if not core.exists(lang_file):
56 return
57 try:
58 lang = core.read(lang_file).strip()
59 except:
60 return
61 if lang:
62 compat.setenv('LANGUAGE', lang)
65 def _check_win32_locale():
66 for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
67 if os.environ.get(i):
68 break
69 else:
70 lang = None
71 import locale
72 try:
73 import ctypes
74 except ImportError:
75 # use only user's default locale
76 lang = locale.getdefaultlocale()[0]
77 else:
78 # using ctypes to determine all locales
79 lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
80 lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
81 if lcid_user != lcid_system:
82 lcid = [lcid_user, lcid_system]
83 else:
84 lcid = [lcid_user]
85 lang = [locale.windows_locale.get(i) for i in lcid]
86 lang = ':'.join([i for i in lang if i])
87 # set lang code for gettext
88 if lang:
89 compat.setenv('LANGUAGE', lang)