/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 bzrlib/i18n.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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