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.
42
return _translations.ugettext(message)
43
except AttributeError:
44
return _translations.gettext(message)
47
def ngettext(singular, plural, number):
48
"""Translate message with plural forms based on `number`.
50
:param singular: English language message in singular form
51
:param plural: English language message in plural form
52
:param number: the number this message should be translated for
54
:returns: translated message as unicode.
58
return _translations.ungettext(singular, plural, number)
59
except AttributeError:
60
return _translations.ngettext(singular, plural, number)
64
"""Mark message for translation but don't translate it right away."""
68
def gettext_per_paragraph(message):
69
"""Translate message per paragraph.
71
:returns: concatenated translated message as unicode.
74
paragraphs = message.split(u'\n\n')
75
# Be careful not to translate the empty string -- it holds the
76
# meta data of the .po file.
77
return u'\n\n'.join(gettext(p) if p else u'' for p in paragraphs)
81
"""Do not allow i18n to be enabled. Useful for third party users
84
_translations = _gettext.NullTranslations()
88
"""Returns whether translations are in use or not."""
89
return _translations is not None
92
def install(lang=None):
93
"""Enables gettext translations in bzr."""
97
_translations = install_translations(lang)
100
def install_translations(lang=None, domain='bzr', locale_base=None):
101
"""Create a gettext translation object.
103
:param lang: language to install.
104
:param domain: translation domain to install.
105
:param locale_base: plugins can specify their own directory.
107
:returns: a gettext translations object to use
110
lang = _get_current_locale()
112
languages = lang.split(':')
115
translation = _gettext.translation(
117
localedir=_get_locale_dir(locale_base),
123
def add_fallback(fallback):
125
Add a fallback translations object. Typically used by plugins.
127
:param fallback: gettext.GNUTranslations object
130
_translations.add_fallback(fallback)
134
"""Disables gettext translations."""
139
def _get_locale_dir(base):
140
"""Returns directory to find .mo translations file in, either local or system
142
:param base: plugins can specify their own local directory
144
if sys.version_info > (3,):
147
fs_enc = sys.getfilesystemencoding()
148
def decode_path(path):
149
return path.decode(fs_enc)
150
if getattr(sys, 'frozen', False):
152
base = os.path.dirname(decode_path(sys.executable))
153
return os.path.join(base, u'locale')
156
base = os.path.dirname(decode_path(__file__))
157
dirpath = os.path.realpath(os.path.join(base, u'locale'))
158
if os.path.exists(dirpath):
160
return os.path.join(decode_path(sys.prefix), u"share", u"locale")
163
def _check_win32_locale():
164
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
165
if os.environ.get(i):
173
# use only user's default locale
174
lang = locale.getdefaultlocale()[0]
176
# using ctypes to determine all locales
177
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
178
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
179
if lcid_user != lcid_system:
180
lcid = [lcid_user, lcid_system]
183
lang = [locale.windows_locale.get(i) for i in lcid]
184
lang = ':'.join([i for i in lang if i])
185
# set lang code for gettext
187
os.environ['LANGUAGE'] = lang
190
def _get_current_locale():
191
if not os.environ.get('LANGUAGE'):
193
lang = config.GlobalStack().get('language')
195
os.environ['LANGUAGE'] = lang
197
if sys.platform == 'win32':
198
_check_win32_locale()
199
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
200
lang = os.environ.get(i)
206
def load_plugin_translations(domain):
207
"""Load the translations for a specific plugin.
209
:param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
211
locale_base = os.path.dirname(
212
unicode(__file__, sys.getfilesystemencoding()))
213
translation = install_translations(domain=domain,
214
locale_base=locale_base)
215
add_fallback(translation)