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(singular, plural, number):
41
"""Translate message with plural forms based on `number`.
43
:param singular: English language message in singular form
44
:param plural: English language message in plural form
45
:param number: the number this message should be translated for
47
:returns: translated message as unicode.
49
return _translations.ungettext(singular, plural, number)
53
"""Mark message for translation but don't translate it right away."""
57
def gettext_per_paragraph(message):
58
"""Translate message per paragraph.
60
:returns: concatenated translated message as unicode.
62
paragraphs = message.split(u'\n\n')
63
ugettext = _translations.ugettext
64
# Be careful not to translate the empty string -- it holds the
65
# meta data of the .po file.
66
return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
70
return _translations is not None
73
def install(lang=None):
78
lang = _get_current_locale()
80
languages = lang.split(':')
83
_translations = _gettext.translation(
85
localedir=_get_locale_dir(),
95
def _get_locale_dir():
96
if hasattr(sys, 'frozen'):
97
base = os.path.dirname(
98
unicode(sys.executable, sys.getfilesystemencoding()))
99
return os.path.join(base, u'locale')
101
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
102
dirpath = os.path.realpath(os.path.join(base, u'locale'))
103
if os.path.exists(dirpath):
106
return '/usr/share/locale'
109
def _check_win32_locale():
110
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
111
if os.environ.get(i):
119
# use only user's default locale
120
lang = locale.getdefaultlocale()[0]
122
# using ctypes to determine all locales
123
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
124
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
125
if lcid_user != lcid_system:
126
lcid = [lcid_user, lcid_system]
129
lang = [locale.windows_locale.get(i) for i in lcid]
130
lang = ':'.join([i for i in lang if i])
131
# set lang code for gettext
133
os.environ['LANGUAGE'] = lang
136
def _get_current_locale():
137
if not os.environ.get('LANGUAGE'):
138
from bzrlib import config
139
lang = config.GlobalStack().get('language')
141
os.environ['LANGUAGE'] = lang
143
if sys.platform == 'win32':
144
_check_win32_locale()
145
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
146
lang = os.environ.get(i)