/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: Jelmer Vernooij
  • Date: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2007 Lukáš Lalinský <lalinsky@gmail.com>
 
4
# Copyright (C) 2007,2009 Alexander Belchenko <bialix@ukr.net>
 
5
# Copyright (C) 2011 Canonical Ltd
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
20
 
 
21
# This module is copied from Bazaar Explorer and modified for brz.
 
22
 
 
23
"""i18n and l10n support for Bazaar."""
 
24
 
 
25
from __future__ import absolute_import
 
26
 
 
27
import gettext as _gettext
 
28
import os
 
29
import sys
 
30
 
 
31
 
 
32
_translations = None
 
33
 
 
34
 
 
35
def gettext(message):
 
36
    """Translate message.
 
37
 
 
38
    :returns: translated message as unicode.
 
39
    """
 
40
    install()
 
41
    try:
 
42
        return _translations.ugettext(message)
 
43
    except AttributeError:
 
44
        return _translations.gettext(message)
 
45
 
 
46
 
 
47
def ngettext(singular, plural, number):
 
48
    """Translate message with plural forms based on `number`.
 
49
 
 
50
    :param singular: English language message in singular form
 
51
    :param plural: English language message in plural form
 
52
    :param number: the number this message should be translated for
 
53
 
 
54
    :returns: translated message as unicode.
 
55
    """
 
56
    install()
 
57
    try:
 
58
        return _translations.ungettext(singular, plural, number)
 
59
    except AttributeError:
 
60
        return _translations.ngettext(singular, plural, number)
 
61
 
 
62
 
 
63
def N_(msg):
 
64
    """Mark message for translation but don't translate it right away."""
 
65
    return msg
 
66
 
 
67
 
 
68
def gettext_per_paragraph(message):
 
69
    """Translate message per paragraph.
 
70
 
 
71
    :returns: concatenated translated message as unicode.
 
72
    """
 
73
    install()
 
74
    paragraphs = message.split(u'\n\n')
 
75
    # Be careful not to translate the empty string -- it holds the
 
76
    # meta data of the .po file.
 
77
    return u'\n\n'.join(gettext(p) if p else u'' for p in paragraphs)
 
78
 
 
79
 
 
80
def disable_i18n():
 
81
    """Do not allow i18n to be enabled.  Useful for third party users
 
82
    of breezy."""
 
83
    global _translations
 
84
    _translations = _gettext.NullTranslations()
 
85
 
 
86
 
 
87
def installed():
 
88
    """Returns whether translations are in use or not."""
 
89
    return _translations is not None
 
90
 
 
91
 
 
92
def install(lang=None):
 
93
    """Enables gettext translations in brz."""
 
94
    global _translations
 
95
    if installed():
 
96
        return
 
97
    _translations = install_translations(lang)
 
98
 
 
99
 
 
100
def install_translations(lang=None, domain='brz', locale_base=None):
 
101
    """Create a gettext translation object.
 
102
 
 
103
    :param lang: language to install.
 
104
    :param domain: translation domain to install.
 
105
    :param locale_base: plugins can specify their own directory.
 
106
 
 
107
    :returns: a gettext translations object to use
 
108
    """
 
109
    if lang is None:
 
110
        lang = _get_current_locale()
 
111
    if lang is not None:
 
112
        languages = lang.split(':')
 
113
    else:
 
114
        languages = None
 
115
    translation = _gettext.translation(
 
116
        domain,
 
117
        localedir=_get_locale_dir(locale_base),
 
118
        languages=languages,
 
119
        fallback=True)
 
120
    return translation
 
121
 
 
122
 
 
123
def add_fallback(fallback):
 
124
    """
 
125
    Add a fallback translations object.  Typically used by plugins.
 
126
 
 
127
    :param fallback: gettext.GNUTranslations object
 
128
    """
 
129
    install()
 
130
    _translations.add_fallback(fallback)
 
131
 
 
132
 
 
133
def uninstall():
 
134
    """Disables gettext translations."""
 
135
    global _translations
 
136
    _translations = None
 
137
 
 
138
 
 
139
def _get_locale_dir(base):
 
140
    """Returns directory to find .mo translations file in, either local or system
 
141
 
 
142
    :param base: plugins can specify their own local directory
 
143
    """
 
144
    if getattr(sys, 'frozen', False):
 
145
        if base is None:
 
146
            base = os.path.dirname(sys.executable)
 
147
        return os.path.join(base, u'locale')
 
148
    else:
 
149
        if base is None:
 
150
            base = os.path.dirname(__file__)
 
151
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
 
152
        if os.path.exists(dirpath):
 
153
            return dirpath
 
154
    return os.path.join(sys.prefix, u"share", u"locale")
 
155
 
 
156
 
 
157
def _check_win32_locale():
 
158
    for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
 
159
        if os.environ.get(i):
 
160
            break
 
161
    else:
 
162
        lang = None
 
163
        import locale
 
164
        try:
 
165
            import ctypes
 
166
        except ImportError:
 
167
            # use only user's default locale
 
168
            lang = locale.getdefaultlocale()[0]
 
169
        else:
 
170
            # using ctypes to determine all locales
 
171
            lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
 
172
            lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
 
173
            if lcid_user != lcid_system:
 
174
                lcid = [lcid_user, lcid_system]
 
175
            else:
 
176
                lcid = [lcid_user]
 
177
            lang = [locale.windows_locale.get(i) for i in lcid]
 
178
            lang = ':'.join([i for i in lang if i])
 
179
        # set lang code for gettext
 
180
        if lang:
 
181
            os.environ['LANGUAGE'] = lang
 
182
 
 
183
 
 
184
def _get_current_locale():
 
185
    if not os.environ.get('LANGUAGE'):
 
186
        from . import config
 
187
        lang = config.GlobalStack().get('language')
 
188
        if lang:
 
189
            os.environ['LANGUAGE'] = lang
 
190
            return lang
 
191
    if sys.platform == 'win32':
 
192
        _check_win32_locale()
 
193
    for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
 
194
        lang = os.environ.get(i)
 
195
        if lang:
 
196
            return lang
 
197
    return None
 
198
 
 
199
 
 
200
def load_plugin_translations(domain):
 
201
    """Load the translations for a specific plugin.
 
202
 
 
203
    :param domain: Gettext domain name (usually 'brz-PLUGINNAME')
 
204
    """
 
205
    locale_base = os.path.dirname(__file__)
 
206
    translation = install_translations(domain=domain,
 
207
                                       locale_base=locale_base)
 
208
    add_fallback(translation)
 
209
    return translation