~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

(gz) Remove bzrlib/util/elementtree/ package (Martin Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
"""i18n and l10n support for Bazaar."""
24
24
 
 
25
from __future__ import absolute_import
 
26
 
25
27
import gettext as _gettext
26
28
import os
27
29
import sys
28
30
 
29
 
_translation = _gettext.NullTranslations()
 
31
 
 
32
_translations = None
30
33
 
31
34
 
32
35
def gettext(message):
34
37
    
35
38
    :returns: translated message as unicode.
36
39
    """
37
 
    return _translation.ugettext(message)
38
 
 
39
 
 
40
 
def ngettext(s, p, n):
41
 
    """Translate message based on `n`.
 
40
    install()
 
41
    return _translations.ugettext(message)
 
42
 
 
43
 
 
44
def ngettext(singular, plural, number):
 
45
    """Translate message with plural forms based on `number`.
 
46
 
 
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
42
50
 
43
51
    :returns: translated message as unicode.
44
52
    """
45
 
    return _translation.ungettext(s, p, n)
 
53
    install()
 
54
    return _translations.ungettext(singular, plural, number)
46
55
 
47
56
 
48
57
def N_(msg):
55
64
 
56
65
    :returns: concatenated translated message as unicode.
57
66
    """
 
67
    install()
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)
63
73
 
64
74
 
 
75
def disable_i18n():
 
76
    """Do not allow i18n to be enabled.  Useful for third party users
 
77
    of bzrlib."""
 
78
    global _translations
 
79
    _translations = _gettext.NullTranslations()
 
80
 
 
81
 
 
82
def installed():
 
83
    """Returns whether translations are in use or not."""
 
84
    return _translations is not None
 
85
 
 
86
 
65
87
def install(lang=None):
66
 
    global _translation
 
88
    """Enables gettext translations in bzr."""
 
89
    global _translations
 
90
    if installed():
 
91
        return
 
92
    _translations = install_translations(lang)
 
93
 
 
94
 
 
95
def install_translations(lang=None, domain='bzr', locale_base=None):
 
96
    """Create a gettext translation object.
 
97
    
 
98
    :param lang: language to install.
 
99
    :param domain: translation domain to install.
 
100
    :param locale_base: plugins can specify their own directory.
 
101
 
 
102
    :returns: a gettext translations object to use
 
103
    """
67
104
    if lang is None:
68
105
        lang = _get_current_locale()
69
 
    _translation = _gettext.translation(
70
 
            'bzr',
71
 
            localedir=_get_locale_dir(),
72
 
            languages=lang.split(':'),
 
106
    if lang is not None:
 
107
        languages = lang.split(':')
 
108
    else:
 
109
        languages = None
 
110
    translation = _gettext.translation(
 
111
            domain,
 
112
            localedir=_get_locale_dir(locale_base),
 
113
            languages=languages,
73
114
            fallback=True)
 
115
    return translation
 
116
 
 
117
 
 
118
def add_fallback(fallback):
 
119
    """
 
120
    Add a fallback translations object.  Typically used by plugins.
 
121
 
 
122
    :param fallback: gettext.GNUTranslations object
 
123
    """
 
124
    install()
 
125
    _translations.add_fallback(fallback)
74
126
 
75
127
 
76
128
def uninstall():
77
 
    global _translation
78
 
    _translation = _null_translation
79
 
 
80
 
 
81
 
def _get_locale_dir():
 
129
    """Disables gettext translations."""
 
130
    global _translations
 
131
    _translations = None
 
132
 
 
133
 
 
134
def _get_locale_dir(base):
 
135
    """Returns directory to find .mo translations file in, either local or system
 
136
 
 
137
    :param base: plugins can specify their own local directory
 
138
    """
82
139
    if hasattr(sys, 'frozen'):
83
 
        base = os.path.dirname(
 
140
        if base is None:
 
141
            base = os.path.dirname(
84
142
                unicode(sys.executable, sys.getfilesystemencoding()))
85
143
        return os.path.join(base, u'locale')
86
144
    else:
87
 
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
 
145
        if base is None:
 
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):
90
149
            return dirpath
122
181
def _get_current_locale():
123
182
    if not os.environ.get('LANGUAGE'):
124
183
        from bzrlib import config
125
 
        lang = config.GlobalConfig().get_user_option('language')
 
184
        lang = config.GlobalStack().get('language')
126
185
        if lang:
127
186
            os.environ['LANGUAGE'] = lang
128
187
            return lang
133
192
        if lang:
134
193
            return lang
135
194
    return None
 
195
 
 
196
 
 
197
def load_plugin_translations(domain):
 
198
    """Load the translations for a specific plugin.
 
199
 
 
200
    :param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
 
201
    """
 
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)
 
207
    return translation