66
70
return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
74
"""Do not allow i18n to be enabled. Useful for third party users
77
_translations = _gettext.NullTranslations()
81
"""Returns whether translations are in use or not."""
70
82
return _translations is not None
73
85
def install(lang=None):
86
"""Enables gettext translations in bzr."""
74
87
global _translations
90
_translations = install_translations(lang)
93
def install_translations(lang=None, domain='bzr', locale_base=None):
94
"""Create a gettext translation object.
96
:param lang: language to install.
97
:param domain: translation domain to install.
98
:param locale_base: plugins can specify their own directory.
100
:returns: a gettext translations object to use
76
103
lang = _get_current_locale()
77
_translations = _gettext.translation(
79
localedir=_get_locale_dir(),
80
languages=lang.split(':'),
105
languages = lang.split(':')
108
translation = _gettext.translation(
110
localedir=_get_locale_dir(locale_base),
116
def add_fallback(fallback):
118
Add a fallback translations object. Typically used by plugins.
120
:param fallback: gettext.GNUTranslations object
123
_translations.add_fallback(fallback)
127
"""Disables gettext translations."""
85
128
global _translations
86
129
_translations = None
89
def _get_locale_dir():
132
def _get_locale_dir(base):
133
"""Returns directory to find .mo translations file in, either local or system
135
:param base: plugins can specify their own local directory
90
137
if hasattr(sys, 'frozen'):
91
base = os.path.dirname(
139
base = os.path.dirname(
92
140
unicode(sys.executable, sys.getfilesystemencoding()))
93
141
return os.path.join(base, u'locale')
95
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
144
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
96
145
dirpath = os.path.realpath(os.path.join(base, u'locale'))
97
146
if os.path.exists(dirpath):
130
179
def _get_current_locale():
131
180
if not os.environ.get('LANGUAGE'):
132
181
from bzrlib import config
133
lang = config.GlobalConfig().get_user_option('language')
182
lang = config.GlobalStack().get('language')
135
184
os.environ['LANGUAGE'] = lang
195
def load_plugin_translations(domain):
196
"""Load the translations for a specific plugin.
198
:param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
200
locale_base = os.path.dirname(
201
unicode(__file__, sys.getfilesystemencoding()))
202
translation = install_translations(domain=domain,
203
locale_base=locale_base)
204
add_fallback(translation)