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
36
:returns: translated message as unicode.
39
return _translations.ugettext(message)
42
def ngettext(singular, plural, number):
43
"""Translate message with plural forms based on `number`.
45
:param singular: English language message in singular form
46
:param plural: English language message in plural form
47
:param number: the number this message should be translated for
49
:returns: translated message as unicode.
52
return _translations.ungettext(singular, plural, number)
56
"""Mark message for translation but don't translate it right away."""
60
def gettext_per_paragraph(message):
61
"""Translate message per paragraph.
63
:returns: concatenated translated message as unicode.
66
paragraphs = message.split(u'\n\n')
67
ugettext = _translations.ugettext
68
# Be careful not to translate the empty string -- it holds the
69
# meta data of the .po file.
70
return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
74
"""Do not allow i18n to be enabled. Useful for third party users
77
_translations = _gettext.NullTranslations()
81
"""Returns whether translations are in use or not."""
82
return _translations is not None
85
def install(lang=None):
86
"""Enables gettext translations in bzr."""
90
_translations = install_translations(lang)
93
def install_translations(lang=None, domain='bzr', locale_base=None):
94
"""Create a gettext translation object.
96
:param lang: language to install.
97
:param domain: translation domain to install.
98
:param locale_base: plugins can specify their own directory.
100
:returns: a gettext translations object to use
103
lang = _get_current_locale()
105
languages = lang.split(':')
108
translation = _gettext.translation(
110
localedir=_get_locale_dir(locale_base),
116
def add_fallback(fallback):
118
Add a fallback translations object. Typically used by plugins.
120
:param fallback: gettext.GNUTranslations object
123
_translations.add_fallback(fallback)
127
"""Disables gettext translations."""
132
def _get_locale_dir(base):
133
"""Returns directory to find .mo translations file in, either local or system
135
:param base: plugins can specify their own local directory
137
if hasattr(sys, 'frozen'):
139
base = os.path.dirname(
140
unicode(sys.executable, sys.getfilesystemencoding()))
141
return os.path.join(base, u'locale')
144
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
145
dirpath = os.path.realpath(os.path.join(base, u'locale'))
146
if os.path.exists(dirpath):
149
return '/usr/share/locale'
152
def _check_win32_locale():
153
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
154
if os.environ.get(i):
162
# use only user's default locale
163
lang = locale.getdefaultlocale()[0]
165
# using ctypes to determine all locales
166
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
167
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
168
if lcid_user != lcid_system:
169
lcid = [lcid_user, lcid_system]
172
lang = [locale.windows_locale.get(i) for i in lcid]
173
lang = ':'.join([i for i in lang if i])
174
# set lang code for gettext
176
os.environ['LANGUAGE'] = lang
179
def _get_current_locale():
180
if not os.environ.get('LANGUAGE'):
181
from bzrlib import config
182
lang = config.GlobalStack().get('language')
184
os.environ['LANGUAGE'] = lang
186
if sys.platform == 'win32':
187
_check_win32_locale()
188
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
189
lang = os.environ.get(i)
195
def load_plugin_translations(domain):
196
"""Load the translations for a specific plugin.
198
:param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
200
locale_base = os.path.dirname(
201
unicode(__file__, sys.getfilesystemencoding()))
202
translation = install_translations(domain=domain,
203
locale_base=locale_base)
204
add_fallback(translation)