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 modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (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 = _gettext.NullTranslations()
35
:returns: translated message as unicode.
37
return _translation.ugettext(message)
40
def ngettext(s, p, n):
41
"""Translate message based on `n`.
43
:returns: translated message as unicode.
45
return _translation.ungettext(s, p, n)
49
"""Mark message for translation but don't translate it right away."""
53
def gettext_per_paragraph(message):
54
"""Translate message per paragraph.
56
:returns: concatenated translated message as unicode.
58
paragraphs = message.split(u'\n\n')
59
ugettext = _translation.ugettext
60
# Be careful not to translate the empty string -- it holds the
61
# meta data of the .po file.
62
return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
65
def install(lang=None):
68
lang = _get_current_locale()
69
_translation = _gettext.translation(
71
localedir=_get_locale_dir(),
72
languages=lang.split(':'),
78
_translation = _null_translation
81
def _get_locale_dir():
82
if hasattr(sys, 'frozen'):
83
base = os.path.dirname(
84
unicode(sys.executable, sys.getfilesystemencoding()))
85
return os.path.join(base, u'locale')
87
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
88
dirpath = os.path.realpath(os.path.join(base, u'locale'))
89
if os.path.exists(dirpath):
92
return '/usr/share/locale'
95
def _check_win32_locale():
96
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
105
# use only user's default locale
106
lang = locale.getdefaultlocale()[0]
108
# using ctypes to determine all locales
109
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
110
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
111
if lcid_user != lcid_system:
112
lcid = [lcid_user, lcid_system]
115
lang = [locale.windows_locale.get(i) for i in lcid]
116
lang = ':'.join([i for i in lang if i])
117
# set lang code for gettext
119
os.environ['LANGUAGE'] = lang
122
def _get_current_locale():
123
if not os.environ.get('LANGUAGE'):
124
from bzrlib import config
125
lang = config.GlobalConfig().get_user_option('language')
127
os.environ['LANGUAGE'] = lang
129
if sys.platform == 'win32':
130
_check_win32_locale()
131
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
132
lang = os.environ.get(i)