/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/i18n.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# along with this program; if not, write to the Free Software
19
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 
21
 
# This module is copied from Bazaar Explorer and modified for bzr.
 
21
# This module is copied from Bazaar Explorer and modified for brz.
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
33
31
 
34
32
 
35
33
def gettext(message):
36
 
    """Translate message. 
37
 
    
 
34
    """Translate message.
 
35
 
38
36
    :returns: translated message as unicode.
39
37
    """
40
38
    install()
41
 
    return _translations.ugettext(message)
 
39
    try:
 
40
        return _translations.ugettext(message)
 
41
    except AttributeError:
 
42
        return _translations.gettext(message)
42
43
 
43
44
 
44
45
def ngettext(singular, plural, number):
51
52
    :returns: translated message as unicode.
52
53
    """
53
54
    install()
54
 
    return _translations.ungettext(singular, plural, number)
 
55
    try:
 
56
        return _translations.ungettext(singular, plural, number)
 
57
    except AttributeError:
 
58
        return _translations.ngettext(singular, plural, number)
55
59
 
56
60
 
57
61
def N_(msg):
66
70
    """
67
71
    install()
68
72
    paragraphs = message.split(u'\n\n')
69
 
    ugettext = _translations.ugettext
70
73
    # Be careful not to translate the empty string -- it holds the
71
74
    # meta data of the .po file.
72
 
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
 
75
    return u'\n\n'.join(gettext(p) if p else u'' for p in paragraphs)
73
76
 
74
77
 
75
78
def disable_i18n():
76
79
    """Do not allow i18n to be enabled.  Useful for third party users
77
 
    of bzrlib."""
 
80
    of breezy."""
78
81
    global _translations
79
82
    _translations = _gettext.NullTranslations()
80
83
 
85
88
 
86
89
 
87
90
def install(lang=None):
88
 
    """Enables gettext translations in bzr."""
 
91
    """Enables gettext translations in brz."""
89
92
    global _translations
90
93
    if installed():
91
94
        return
92
95
    _translations = install_translations(lang)
93
96
 
94
97
 
95
 
def install_translations(lang=None, domain='bzr', locale_base=None):
 
98
def install_translations(lang=None, domain='brz', locale_base=None):
96
99
    """Create a gettext translation object.
97
 
    
 
100
 
98
101
    :param lang: language to install.
99
102
    :param domain: translation domain to install.
100
103
    :param locale_base: plugins can specify their own directory.
108
111
    else:
109
112
        languages = None
110
113
    translation = _gettext.translation(
111
 
            domain,
112
 
            localedir=_get_locale_dir(locale_base),
113
 
            languages=languages,
114
 
            fallback=True)
 
114
        domain,
 
115
        localedir=_get_locale_dir(locale_base),
 
116
        languages=languages,
 
117
        fallback=True)
115
118
    return translation
116
119
 
117
120
 
136
139
 
137
140
    :param base: plugins can specify their own local directory
138
141
    """
139
 
    fs_enc = sys.getfilesystemencoding()
140
142
    if getattr(sys, 'frozen', False):
141
143
        if base is None:
142
 
            base = os.path.dirname(unicode(sys.executable, fs_enc))
 
144
            base = os.path.dirname(sys.executable)
143
145
        return os.path.join(base, u'locale')
144
146
    else:
145
147
        if base is None:
146
 
            base = os.path.dirname(unicode(__file__, fs_enc))
 
148
            base = os.path.dirname(__file__)
147
149
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
148
150
        if os.path.exists(dirpath):
149
151
            return dirpath
150
 
    return os.path.join(unicode(sys.prefix, fs_enc), u"share", u"locale")
 
152
    return os.path.join(sys.prefix, u"share", u"locale")
151
153
 
152
154
 
153
155
def _check_win32_locale():
154
 
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
 
156
    for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
155
157
        if os.environ.get(i):
156
158
            break
157
159
    else:
179
181
 
180
182
def _get_current_locale():
181
183
    if not os.environ.get('LANGUAGE'):
182
 
        from bzrlib import config
 
184
        from . import config
183
185
        lang = config.GlobalStack().get('language')
184
186
        if lang:
185
187
            os.environ['LANGUAGE'] = lang
186
188
            return lang
187
189
    if sys.platform == 'win32':
188
190
        _check_win32_locale()
189
 
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
 
191
    for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
190
192
        lang = os.environ.get(i)
191
193
        if lang:
192
194
            return lang
196
198
def load_plugin_translations(domain):
197
199
    """Load the translations for a specific plugin.
198
200
 
199
 
    :param domain: Gettext domain name (usually 'bzr-PLUGINNAME')
 
201
    :param domain: Gettext domain name (usually 'brz-PLUGINNAME')
200
202
    """
201
 
    locale_base = os.path.dirname(
202
 
        unicode(__file__, sys.getfilesystemencoding()))
 
203
    locale_base = os.path.dirname(__file__)
203
204
    translation = install_translations(domain=domain,
204
 
        locale_base=locale_base)
 
205
                                       locale_base=locale_base)
205
206
    add_fallback(translation)
206
207
    return translation