1 """i18n and l10n support for git-cola"""
2 from __future__
import division
, absolute_import
, unicode_literals
4 import gettext
as _gettext
8 from cola
import compat
10 from cola
import resources
12 _null_translation
= _gettext
.NullTranslations()
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
21 txt
= _translation
.ugettext(s
)
22 if txt
[-6:-4] == '@@': # handle @@verb / @@noun
27 def ngettext(s
, p
, n
):
28 return _translation
.ungettext(s
, p
, n
)
37 if sys
.platform
== 'win32':
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(),
49 if not hasattr(_translation
, 'ugettext'):
50 _translation
.ugettext
= _translation
.gettext
51 _translation
.ungettext
= _translation
.ngettext
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
):
68 lang
= core
.read(lang_file
).strip()
72 compat
.setenv('LANGUAGE', lang
)
75 def _check_win32_locale():
76 for i
in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
85 # use only user's default locale
86 lang
= locale
.getdefaultlocale()[0]
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
]
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
99 compat
.setenv('LANGUAGE', lang
)