35
38
:returns: translated message as unicode.
37
return _translation.ugettext(message)
40
def ngettext(s, p, n):
41
"""Translate message based on `n`.
41
return _translations.ugettext(message)
44
def ngettext(singular, plural, number):
45
"""Translate message with plural forms based on `number`.
47
:param singular: English language message in singular form
48
:param plural: English language message in plural form
49
:param number: the number this message should be translated for
43
51
:returns: translated message as unicode.
45
return _translation.ungettext(s, p, n)
54
return _translations.ungettext(singular, plural, number)
56
65
:returns: concatenated translated message as unicode.
58
68
paragraphs = message.split(u'\n\n')
59
ugettext = _translation.ugettext
69
ugettext = _translations.ugettext
60
70
# Be careful not to translate the empty string -- it holds the
61
71
# meta data of the .po file.
62
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."""
84
return _translations is not None
65
87
def install(lang=None):
88
"""Enables gettext translations in bzr."""
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
68
105
lang = _get_current_locale()
69
_translation = _gettext.translation(
71
localedir=_get_locale_dir(),
72
languages=lang.split(':'),
107
languages = lang.split(':')
110
translation = _gettext.translation(
112
localedir=_get_locale_dir(locale_base),
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)
78
_translation = _null_translation
81
def _get_locale_dir():
129
"""Disables gettext translations."""
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
82
139
if hasattr(sys, 'frozen'):
83
base = os.path.dirname(
141
base = os.path.dirname(
84
142
unicode(sys.executable, sys.getfilesystemencoding()))
85
143
return os.path.join(base, u'locale')
87
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
146
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
88
147
dirpath = os.path.realpath(os.path.join(base, u'locale'))
89
148
if os.path.exists(dirpath):
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)