66
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."""
70
84
return _translations is not None
73
87
def install(lang=None):
88
"""Enables gettext translations in bzr."""
74
89
global _translations
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
76
105
lang = _get_current_locale()
77
106
if lang is not None:
78
107
languages = lang.split(':')
81
_translations = _gettext.translation(
83
localedir=_get_locale_dir(),
110
translation = _gettext.translation(
112
localedir=_get_locale_dir(locale_base),
84
113
languages=languages,
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."""
89
130
global _translations
90
131
_translations = None
93
def _get_locale_dir():
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
94
139
if hasattr(sys, 'frozen'):
95
base = os.path.dirname(
141
base = os.path.dirname(
96
142
unicode(sys.executable, sys.getfilesystemencoding()))
97
143
return os.path.join(base, u'locale')
99
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
146
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
100
147
dirpath = os.path.realpath(os.path.join(base, u'locale'))
101
148
if os.path.exists(dirpath):
134
181
def _get_current_locale():
135
182
if not os.environ.get('LANGUAGE'):
136
183
from bzrlib import config
137
lang = config.GlobalConfig().get_user_option('language')
184
lang = config.GlobalStack().get('language')
139
186
os.environ['LANGUAGE'] = lang
197
def load_plugin_translations(domain):
198
"""Load the translations for a specific plugin.
200
:param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
202
locale_base = os.path.dirname(
203
unicode(__file__, sys.getfilesystemencoding()))
204
translation = install_translations(domain=domain,
205
locale_base=locale_base)
206
add_fallback(translation)