1
# -*- coding: utf-8 -*-
3
# Copyright (C) 2007 Lukáš Lalinský <lalinsky@gmail.com>
4
# Copyright (C) 2007,2009 Alexander Belchenko <bialix@ukr.net>
5
# Copyright (C) 2011 Canonical Ltd
7
# This program is free software; you can redistribute it and/or
8
# modify it under the terms of the GNU General Public License
9
# as published by the Free Software Foundation; either version 2
10
# of the License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
# This module is copied from Bazaar Explorer and modified for bzr.
23
"""i18n and l10n support for Bazaar."""
25
import gettext as _gettext
29
_translation = _null_translation = _gettext.NullTranslations()
30
_installed_language = None
33
def install(lang=None):
34
global _translation, _installed_language
36
lang = _get_current_locale()
37
_translation = _gettext.translation(
39
localedir=_get_locale_dir(),
40
languages=lang.split(':'),
45
_translation = _null_translation
48
def _get_locale_dir():
49
if hasattr(sys, 'frozen'):
50
base = os.path.dirname(
51
unicode(sys.executable, sys.getfilesystemencoding()))
52
return os.path.join(base, u'locale')
54
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
55
return os.path.realpath(os.path.join(base, u'locale'))
58
def _check_win32_locale():
59
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
68
# use only user's default locale
69
lang = locale.getdefaultlocale()[0]
71
# using ctypes to determine all locales
72
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
73
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
74
if lcid_user != lcid_system:
75
lcid = [lcid_user, lcid_system]
78
lang = [locale.windows_locale.get(i) for i in lcid]
79
lang = ':'.join([i for i in lang if i])
80
# set lang code for gettext
82
os.environ['LANGUAGE'] = lang
85
def _get_current_locale():
86
if not os.environ.get('LANGUAGE'):
87
from bzrlib import config
88
lang = config.GlobalConfig().get_user_option('language')
90
os.environ['LANGUAGE'] = lang
92
if sys.platform == 'win32':
94
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
95
lang = os.environ.get(i)
100
# special zzz translation for debugging i18n stuff
101
class _ZzzTranslations(object):
104
return u'zz{{%s}}' % s
106
def ugettext(self, s):
107
return self.zzz(_null_translation.ugettext(s))
109
def ungettext(self, s, p, n):
110
return self.zzz(_null_translation.ungettext(s, p, n))
114
_translation = _ZzzTranslations()
116
def gettext(message):
117
"""Translate message.
118
Returns translated message as unicode."""
119
paragraphs = message.split(u'\n\n')
120
ugettext = _translation.ugettext
121
# Be careful not to translate the empty string -- it holds the
122
# meta data of the .po file.
123
return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
125
def ngettext(s, p, n):
126
return gettext(s if n == 1 else p)