/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

Implement command help l10n.

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
 
8
# modify it under the terms of the GNU General Public License
 
9
# as published by the Free Software Foundation; either version 2
 
10
# of the License, or (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 bzr.
 
22
 
 
23
"""i18n and l10n support for Bazaar."""
 
24
 
 
25
import gettext as _gettext
 
26
import os
 
27
import sys
 
28
 
 
29
_translation = _null_translation = _gettext.NullTranslations()
 
30
_installed_language = None
 
31
 
 
32
 
 
33
def install(lang=None):
 
34
    global _translation, _installed_language
 
35
    if lang is None:
 
36
        lang = _get_current_locale()
 
37
    _translation = _gettext.translation(
 
38
            'bzr',
 
39
            localedir=_get_locale_dir(),
 
40
            languages=lang.split(':'),
 
41
            fallback=True)
 
42
 
 
43
def uninstall():
 
44
    global _translation
 
45
    _translation = _null_translation
 
46
 
 
47
 
 
48
def _get_locale_dir():
 
49
    if hasattr(sys, 'frozen'):
 
50
        base = os.path.dirname(
 
51
                unicode(sys.executable, sys.getfilesystemencoding()))
 
52
        return os.path.join(base, u'locale')
 
53
    else:
 
54
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
 
55
        return os.path.realpath(os.path.join(base, u'locale'))
 
56
 
 
57
 
 
58
def _check_win32_locale():
 
59
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
 
60
        if os.environ.get(i):
 
61
            break
 
62
    else:
 
63
        lang = None
 
64
        import locale
 
65
        try:
 
66
            import ctypes
 
67
        except ImportError:
 
68
            # use only user's default locale
 
69
            lang = locale.getdefaultlocale()[0]
 
70
        else:
 
71
            # using ctypes to determine all locales
 
72
            lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
 
73
            lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
 
74
            if lcid_user != lcid_system:
 
75
                lcid = [lcid_user, lcid_system]
 
76
            else:
 
77
                lcid = [lcid_user]
 
78
            lang = [locale.windows_locale.get(i) for i in lcid]
 
79
            lang = ':'.join([i for i in lang if i])
 
80
        # set lang code for gettext
 
81
        if lang:
 
82
            os.environ['LANGUAGE'] = lang
 
83
 
 
84
 
 
85
def _get_current_locale():
 
86
    if not os.environ.get('LANGUAGE'):
 
87
        from bzrlib import config
 
88
        lang = config.GlobalConfig().get_user_option('language')
 
89
        if lang:
 
90
            os.environ['LANGUAGE'] = lang
 
91
            return lang
 
92
    if sys.platform == 'win32':
 
93
        _check_win32_locale()
 
94
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
 
95
        lang = os.environ.get(i)
 
96
        if lang:
 
97
            return lang
 
98
    return None
 
99
 
 
100
# special zzz translation for debugging i18n stuff
 
101
class _ZzzTranslations(object):
 
102
 
 
103
    def zzz(self, s):
 
104
        return u'zz{{%s}}' % s
 
105
 
 
106
    def ugettext(self, s):
 
107
        return self.zzz(_null_translation.ugettext(s))
 
108
 
 
109
    def ungettext(self, s, p, n):
 
110
        return self.zzz(_null_translation.ungettext(s, p, n))
 
111
 
 
112
def install_zzz():
 
113
    global _translation
 
114
    _translation = _ZzzTranslations()
 
115
 
 
116
def gettext(message):
 
117
    """Translate message. 
 
118
    Returns translated message as unicode."""
 
119
    paragraphs = message.split(u'\n\n')
 
120
    ugettext = _translation.ugettext
 
121
    # Be careful not to translate the empty string -- it holds the
 
122
    # meta data of the .po file.
 
123
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
 
124
 
 
125
def ngettext(s, p, n):
 
126
    return gettext(s if n == 1 else p)
 
127