~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Jelmer Vernooij
  • Date: 2011-10-04 22:20:49 UTC
  • mto: This revision was merged to the branch mainline in revision 6190.
  • Revision ID: jelmer@samba.org-20111004222049-d9glniyleu0pppzd
Add a load_plugin_translations method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import os
27
27
import sys
28
28
 
 
29
 
29
30
_translations = None
30
31
 
31
32
 
34
35
    
35
36
    :returns: translated message as unicode.
36
37
    """
 
38
    install()
37
39
    return _translations.ugettext(message)
38
40
 
39
41
 
46
48
 
47
49
    :returns: translated message as unicode.
48
50
    """
 
51
    install()
49
52
    return _translations.ungettext(singular, plural, number)
50
53
 
51
54
 
59
62
 
60
63
    :returns: concatenated translated message as unicode.
61
64
    """
 
65
    install()
62
66
    paragraphs = message.split(u'\n\n')
63
67
    ugettext = _translations.ugettext
64
68
    # Be careful not to translate the empty string -- it holds the
66
70
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
67
71
 
68
72
 
 
73
def disable_i18n():
 
74
    """Do not allow i18n to be enabled.  Useful for third party users
 
75
    of bzrlib."""
 
76
    global _translations
 
77
    _translations = _gettext.NullTranslations()
 
78
 
 
79
 
69
80
def installed():
 
81
    """Returns whether translations are in use or not."""
70
82
    return _translations is not None
71
83
 
72
84
 
73
85
def install(lang=None):
 
86
    """Enables gettext translations in bzr."""
74
87
    global _translations
 
88
    if installed():
 
89
        return
 
90
    _translations = install_translations(lang)
 
91
 
 
92
 
 
93
def install_translations(lang=None, domain='bzr', locale_base=None):
 
94
    """Create a gettext translation object.
 
95
    
 
96
    :param lang: language to install.
 
97
    :param domain: translation domain to install.
 
98
    :param locale_base: plugins can specify their own directory.
 
99
 
 
100
    :returns: a gettext translations object to use
 
101
    """
75
102
    if lang is None:
76
103
        lang = _get_current_locale()
77
 
    _translations = _gettext.translation(
78
 
            'bzr',
79
 
            localedir=_get_locale_dir(),
80
 
            languages=lang.split(':'),
 
104
    if lang is not None:
 
105
        languages = lang.split(':')
 
106
    else:
 
107
        languages = None
 
108
    translation = _gettext.translation(
 
109
            domain,
 
110
            localedir=_get_locale_dir(locale_base),
 
111
            languages=languages,
81
112
            fallback=True)
 
113
    return translation
 
114
 
 
115
 
 
116
def add_fallback(fallback):
 
117
    """
 
118
    Add a fallback translations object.  Typically used by plugins.
 
119
 
 
120
    :param fallback: gettext.GNUTranslations object
 
121
    """
 
122
    install()
 
123
    _translations.add_fallback(fallback)
82
124
 
83
125
 
84
126
def uninstall():
 
127
    """Disables gettext translations."""
85
128
    global _translations
86
129
    _translations = None
87
130
 
88
131
 
89
 
def _get_locale_dir():
 
132
def _get_locale_dir(base):
 
133
    """Returns directory to find .mo translations file in, either local or system
 
134
 
 
135
    :param base: plugins can specify their own local directory
 
136
    """
90
137
    if hasattr(sys, 'frozen'):
91
 
        base = os.path.dirname(
 
138
        if base is None:
 
139
            base = os.path.dirname(
92
140
                unicode(sys.executable, sys.getfilesystemencoding()))
93
141
        return os.path.join(base, u'locale')
94
142
    else:
95
 
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
 
143
        if base is None:
 
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):
98
147
            return 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')
134
183
        if lang:
135
184
            os.environ['LANGUAGE'] = lang
136
185
            return lang
141
190
        if lang:
142
191
            return lang
143
192
    return None
 
193
 
 
194
 
 
195
def load_plugin_translations(domain):
 
196
    """Load the translations for a specific plugin.
 
197
 
 
198
    :param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
 
199
    """
 
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)
 
205
    return translation