~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-30 18:28:17 UTC
  • mfrom: (5967.10.2 test-cat)
  • Revision ID: pqm@pqm.ubuntu.com-20110630182817-83a5q9r9rxfkdn8r
(mbp) don't use subprocesses for testing cat (Martin Pool)

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
 
 
27
25
import gettext as _gettext
28
26
import os
29
27
import sys
30
28
 
31
 
 
32
29
_translations = None
33
30
 
34
31
 
37
34
    
38
35
    :returns: translated message as unicode.
39
36
    """
40
 
    install()
41
37
    return _translations.ugettext(message)
42
38
 
43
39
 
50
46
 
51
47
    :returns: translated message as unicode.
52
48
    """
53
 
    install()
54
49
    return _translations.ungettext(singular, plural, number)
55
50
 
56
51
 
64
59
 
65
60
    :returns: concatenated translated message as unicode.
66
61
    """
67
 
    install()
68
62
    paragraphs = message.split(u'\n\n')
69
63
    ugettext = _translations.ugettext
70
64
    # Be careful not to translate the empty string -- it holds the
72
66
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
73
67
 
74
68
 
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
69
def installed():
83
 
    """Returns whether translations are in use or not."""
84
70
    return _translations is not None
85
71
 
86
72
 
87
73
def install(lang=None):
88
 
    """Enables gettext translations in bzr."""
89
74
    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
 
    """
104
75
    if lang is None:
105
76
        lang = _get_current_locale()
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,
 
77
    _translations = _gettext.translation(
 
78
            'bzr',
 
79
            localedir=_get_locale_dir(),
 
80
            languages=lang.split(':'),
114
81
            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)
126
82
 
127
83
 
128
84
def uninstall():
129
 
    """Disables gettext translations."""
130
85
    global _translations
131
86
    _translations = None
132
87
 
133
88
 
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
 
    """
139
 
    fs_enc = sys.getfilesystemencoding()
140
 
    if getattr(sys, 'frozen', False):
141
 
        if base is None:
142
 
            base = os.path.dirname(unicode(sys.executable, fs_enc))
 
89
def _get_locale_dir():
 
90
    if hasattr(sys, 'frozen'):
 
91
        base = os.path.dirname(
 
92
                unicode(sys.executable, sys.getfilesystemencoding()))
143
93
        return os.path.join(base, u'locale')
144
94
    else:
145
 
        if base is None:
146
 
            base = os.path.dirname(unicode(__file__, fs_enc))
 
95
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
147
96
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
148
97
        if os.path.exists(dirpath):
149
98
            return dirpath
150
 
    return os.path.join(unicode(sys.prefix, fs_enc), u"share", u"locale")
 
99
        else:
 
100
            return '/usr/share/locale'
151
101
 
152
102
 
153
103
def _check_win32_locale():
180
130
def _get_current_locale():
181
131
    if not os.environ.get('LANGUAGE'):
182
132
        from bzrlib import config
183
 
        lang = config.GlobalStack().get('language')
 
133
        lang = config.GlobalConfig().get_user_option('language')
184
134
        if lang:
185
135
            os.environ['LANGUAGE'] = lang
186
136
            return lang
191
141
        if lang:
192
142
            return lang
193
143
    return None
194
 
 
195
 
 
196
 
def load_plugin_translations(domain):
197
 
    """Load the translations for a specific plugin.
198
 
 
199
 
    :param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
200
 
    """
201
 
    locale_base = os.path.dirname(
202
 
        unicode(__file__, sys.getfilesystemencoding()))
203
 
    translation = install_translations(domain=domain,
204
 
        locale_base=locale_base)
205
 
    add_fallback(translation)
206
 
    return translation