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 brz.
23
"""i18n and l10n support for Bazaar."""
25
from __future__ import absolute_import
27
import gettext as _gettext
31
from .sixish import text_type
40
:returns: translated message as unicode.
44
return _translations.ugettext(message)
45
except AttributeError:
46
return _translations.gettext(message)
49
def ngettext(singular, plural, number):
50
"""Translate message with plural forms based on `number`.
52
:param singular: English language message in singular form
53
:param plural: English language message in plural form
54
:param number: the number this message should be translated for
56
:returns: translated message as unicode.
60
return _translations.ungettext(singular, plural, number)
61
except AttributeError:
62
return _translations.ngettext(singular, plural, number)
66
"""Mark message for translation but don't translate it right away."""
70
def gettext_per_paragraph(message):
71
"""Translate message per paragraph.
73
:returns: concatenated translated message as unicode.
76
paragraphs = message.split(u'\n\n')
77
# Be careful not to translate the empty string -- it holds the
78
# meta data of the .po file.
79
return u'\n\n'.join(gettext(p) if p else u'' for p in paragraphs)
83
"""Do not allow i18n to be enabled. Useful for third party users
86
_translations = _gettext.NullTranslations()
90
"""Returns whether translations are in use or not."""
91
return _translations is not None
94
def install(lang=None):
95
"""Enables gettext translations in brz."""
99
_translations = install_translations(lang)
102
def install_translations(lang=None, domain='brz', locale_base=None):
103
"""Create a gettext translation object.
105
:param lang: language to install.
106
:param domain: translation domain to install.
107
:param locale_base: plugins can specify their own directory.
109
:returns: a gettext translations object to use
112
lang = _get_current_locale()
114
languages = lang.split(':')
117
translation = _gettext.translation(
119
localedir=_get_locale_dir(locale_base),
125
def add_fallback(fallback):
127
Add a fallback translations object. Typically used by plugins.
129
:param fallback: gettext.GNUTranslations object
132
_translations.add_fallback(fallback)
136
"""Disables gettext translations."""
141
def _get_locale_dir(base):
142
"""Returns directory to find .mo translations file in, either local or system
144
:param base: plugins can specify their own local directory
146
if sys.version_info > (3,):
149
fs_enc = sys.getfilesystemencoding()
150
def decode_path(path):
151
return path.decode(fs_enc)
152
if getattr(sys, 'frozen', False):
154
base = os.path.dirname(decode_path(sys.executable))
155
return os.path.join(base, u'locale')
158
base = os.path.dirname(decode_path(__file__))
159
dirpath = os.path.realpath(os.path.join(base, u'locale'))
160
if os.path.exists(dirpath):
162
return os.path.join(decode_path(sys.prefix), u"share", u"locale")
165
def _check_win32_locale():
166
for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
167
if os.environ.get(i):
175
# use only user's default locale
176
lang = locale.getdefaultlocale()[0]
178
# using ctypes to determine all locales
179
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
180
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
181
if lcid_user != lcid_system:
182
lcid = [lcid_user, lcid_system]
185
lang = [locale.windows_locale.get(i) for i in lcid]
186
lang = ':'.join([i for i in lang if i])
187
# set lang code for gettext
189
os.environ['LANGUAGE'] = lang
192
def _get_current_locale():
193
if not os.environ.get('LANGUAGE'):
195
lang = config.GlobalStack().get('language')
197
os.environ['LANGUAGE'] = lang
199
if sys.platform == 'win32':
200
_check_win32_locale()
201
for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
202
lang = os.environ.get(i)
208
def load_plugin_translations(domain):
209
"""Load the translations for a specific plugin.
211
:param domain: Gettext domain name (usually 'brz-PLUGINNAME')
213
locale_base = os.path.dirname(__file__)
214
translation = install_translations(domain=domain,
215
locale_base=locale_base)
216
add_fallback(translation)