doc: remove empty line
[git-cola.git] / cola / i18n.py
blob073f700472d276a8cc5c60ca7531cea84687b419
1 """i18n and l10n support for git-cola"""
2 from __future__ import division, absolute_import, unicode_literals
3 import gettext as _gettext
4 import os
5 import sys
7 from . import compat
8 from . import core
9 from . import resources
11 _null_translation = _gettext.NullTranslations()
12 _translation = _null_translation
15 def gettext(s):
16 try:
17 txt = _translation.ugettext(s)
18 except AttributeError:
19 # Python 3 compat
20 _translation.ugettext = _translation.gettext
21 txt = _translation.gettext(s)
22 if txt[-6:-4] == '@@': # handle @@verb / @@noun
23 txt = txt[:-6]
24 return txt
27 def ngettext(s, p, n):
28 try:
29 txt = _translation.ungettext(s, p, n)
30 except AttributeError:
31 # Python 3 compat
32 _translation.ungettext = _translation.ngettext
33 txt = _translation.ngettext(s, p, n)
34 return txt
37 def N_(s):
38 return gettext(s)
41 def install(locale):
42 # pylint: disable=global-statement
43 global _translation
44 if sys.platform == 'win32':
45 _check_win32_locale()
46 if locale:
47 compat.setenv('LANGUAGE', locale)
48 compat.setenv('LANG', locale)
49 compat.setenv('LC_MESSAGES', locale)
50 _install_custom_language()
51 _gettext.textdomain('messages')
52 _translation = _gettext.translation('git-cola',
53 localedir=_get_locale_dir(),
54 fallback=True)
57 def uninstall():
58 # pylint: disable=global-statement
59 global _translation
60 _translation = _null_translation
63 def _get_locale_dir():
64 return resources.prefix('share', 'locale')
67 def _install_custom_language():
68 """Allow a custom language to be set in ~/.config/git-cola/language"""
69 lang_file = resources.config_home('language')
70 if not core.exists(lang_file):
71 return
72 try:
73 lang = core.read(lang_file).strip()
74 except (OSError, IOError):
75 return
76 if lang:
77 compat.setenv('LANGUAGE', lang)
80 def _check_win32_locale():
81 for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
82 if os.environ.get(i):
83 break
84 else:
85 lang = None
86 import locale
87 try:
88 import ctypes
89 except ImportError:
90 # use only user's default locale
91 lang = locale.getdefaultlocale()[0]
92 else:
93 # using ctypes to determine all locales
94 lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
95 lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
96 if lcid_user != lcid_system:
97 lcid = [lcid_user, lcid_system]
98 else:
99 lcid = [lcid_user]
100 lang = [locale.windows_locale.get(i) for i in lcid]
101 lang = ':'.join([i for i in lang if i])
102 # set lang code for gettext
103 if lang:
104 compat.setenv('LANGUAGE', lang)