~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

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