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
35
:returns: translated message as unicode.
37
return _translations.ugettext(message)
40
def ngettext(s, p, n):
41
"""Translate message based on `n`.
43
:returns: translated message as unicode.
45
return _translations.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 = _translations.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)
66
return _translations is not None
69
def install(lang=None):
72
lang = _get_current_locale()
73
_translations = _gettext.translation(
75
localedir=_get_locale_dir(),
76
languages=lang.split(':'),
85
def _get_locale_dir():
86
if hasattr(sys, 'frozen'):
87
base = os.path.dirname(
88
unicode(sys.executable, sys.getfilesystemencoding()))
89
return os.path.join(base, u'locale')
91
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
92
dirpath = os.path.realpath(os.path.join(base, u'locale'))
93
if os.path.exists(dirpath):
96
return '/usr/share/locale'
99
def _check_win32_locale():
100
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
101
if os.environ.get(i):
109
# use only user's default locale
110
lang = locale.getdefaultlocale()[0]
112
# using ctypes to determine all locales
113
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
114
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
115
if lcid_user != lcid_system:
116
lcid = [lcid_user, lcid_system]
119
lang = [locale.windows_locale.get(i) for i in lcid]
120
lang = ':'.join([i for i in lang if i])
121
# set lang code for gettext
123
os.environ['LANGUAGE'] = lang
126
def _get_current_locale():
127
if not os.environ.get('LANGUAGE'):
128
from bzrlib import config
129
lang = config.GlobalConfig().get_user_option('language')
131
os.environ['LANGUAGE'] = lang
133
if sys.platform == 'win32':
134
_check_win32_locale()
135
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
136
lang = os.environ.get(i)