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
from __future__ import absolute_import
27
import gettext as _gettext
38
:returns: translated message as unicode.
41
return _translations.ugettext(message)
44
def ngettext(singular, plural, number):
45
"""Translate message with plural forms based on `number`.
47
:param singular: English language message in singular form
48
:param plural: English language message in plural form
49
:param number: the number this message should be translated for
51
:returns: translated message as unicode.
54
return _translations.ungettext(singular, plural, number)
58
"""Mark message for translation but don't translate it right away."""
62
def gettext_per_paragraph(message):
63
"""Translate message per paragraph.
65
:returns: concatenated translated message as unicode.
68
paragraphs = message.split(u'\n\n')
69
ugettext = _translations.ugettext
70
# Be careful not to translate the empty string -- it holds the
71
# meta data of the .po file.
72
return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
76
"""Do not allow i18n to be enabled. Useful for third party users
79
_translations = _gettext.NullTranslations()
83
"""Returns whether translations are in use or not."""
84
return _translations is not None
87
def install(lang=None):
88
"""Enables gettext translations in bzr."""
92
_translations = install_translations(lang)
95
def install_translations(lang=None, domain='bzr', locale_base=None):
96
"""Create a gettext translation object.
98
:param lang: language to install.
99
:param domain: translation domain to install.
100
:param locale_base: plugins can specify their own directory.
102
:returns: a gettext translations object to use
105
lang = _get_current_locale()
107
languages = lang.split(':')
110
translation = _gettext.translation(
112
localedir=_get_locale_dir(locale_base),
118
def add_fallback(fallback):
120
Add a fallback translations object. Typically used by plugins.
122
:param fallback: gettext.GNUTranslations object
125
_translations.add_fallback(fallback)
129
"""Disables gettext translations."""
134
def _get_locale_dir(base):
135
"""Returns directory to find .mo translations file in, either local or system
137
:param base: plugins can specify their own local directory
139
fs_enc = sys.getfilesystemencoding()
140
if getattr(sys, 'frozen', False):
142
base = os.path.dirname(unicode(sys.executable, fs_enc))
143
return os.path.join(base, u'locale')
146
base = os.path.dirname(unicode(__file__, fs_enc))
147
dirpath = os.path.realpath(os.path.join(base, u'locale'))
148
if os.path.exists(dirpath):
150
return os.path.join(unicode(sys.prefix, fs_enc), u"share", u"locale")
153
def _check_win32_locale():
154
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
155
if os.environ.get(i):
163
# use only user's default locale
164
lang = locale.getdefaultlocale()[0]
166
# using ctypes to determine all locales
167
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
168
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
169
if lcid_user != lcid_system:
170
lcid = [lcid_user, lcid_system]
173
lang = [locale.windows_locale.get(i) for i in lcid]
174
lang = ':'.join([i for i in lang if i])
175
# set lang code for gettext
177
os.environ['LANGUAGE'] = lang
180
def _get_current_locale():
181
if not os.environ.get('LANGUAGE'):
182
from bzrlib import config
183
lang = config.GlobalStack().get('language')
185
os.environ['LANGUAGE'] = lang
187
if sys.platform == 'win32':
188
_check_win32_locale()
189
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
190
lang = os.environ.get(i)
196
def load_plugin_translations(domain):
197
"""Load the translations for a specific plugin.
199
:param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
201
locale_base = os.path.dirname(
202
unicode(__file__, sys.getfilesystemencoding()))
203
translation = install_translations(domain=domain,
204
locale_base=locale_base)
205
add_fallback(translation)