/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
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
#
5875.2.11 by Vincent Ladeuil
Slightly different wrapping of the GPL header broke test_source :-/
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.
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
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
5875.2.11 by Vincent Ladeuil
Slightly different wrapping of the GPL header broke test_source :-/
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
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
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
29
_translation = _gettext.NullTranslations()
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
30
31
5875.2.7 by INADA Naoki
Change order of functions.
32
def gettext(message):
33
    """Translate message. 
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
34
    
35
    :returns: translated message as unicode.
36
    """
5875.2.7 by INADA Naoki
Change order of functions.
37
    return _translation.ugettext(message)
38
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
39
5875.2.7 by INADA Naoki
Change order of functions.
40
def ngettext(s, p, n):
41
    """Translate message based on `n`.
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
42
43
    :returns: translated message as unicode.
44
    """
5875.2.7 by INADA Naoki
Change order of functions.
45
    return _translation.ungettext(s, p, n)
46
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
47
5875.2.7 by INADA Naoki
Change order of functions.
48
def N_(msg):
49
    """Mark message for translation but don't translate it right away."""
50
    return msg
51
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
52
5875.2.7 by INADA Naoki
Change order of functions.
53
def gettext_per_paragraph(message):
54
    """Translate message per paragraph.
55
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
56
    :returns: concatenated translated message as unicode.
57
    """
5875.2.7 by INADA Naoki
Change order of functions.
58
    paragraphs = message.split(u'\n\n')
59
    ugettext = _translation.ugettext
60
    # Be careful not to translate the empty string -- it holds the
61
    # meta data of the .po file.
62
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
63
64
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
65
def install(lang=None):
5875.2.9 by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing.
66
    global _translation
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
67
    if lang is None:
68
        lang = _get_current_locale()
69
    _translation = _gettext.translation(
70
            'bzr',
71
            localedir=_get_locale_dir(),
72
            languages=lang.split(':'),
73
            fallback=True)
74
5875.2.7 by INADA Naoki
Change order of functions.
75
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
76
def uninstall():
77
    global _translation
78
    _translation = _null_translation
79
80
81
def _get_locale_dir():
82
    if hasattr(sys, 'frozen'):
83
        base = os.path.dirname(
84
                unicode(sys.executable, sys.getfilesystemencoding()))
85
        return os.path.join(base, u'locale')
86
    else:
87
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
5875.2.4 by INADA Naoki
Search '/usr/share/locale' when bzrlib/locale does not exist.
88
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
89
        if os.path.exists(dirpath):
90
            return dirpath
91
        else:
92
            return '/usr/share/locale'
5875.2.1 by INADA Naoki
Add bzrlib.i18n module.
93
94
95
def _check_win32_locale():
96
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
97
        if os.environ.get(i):
98
            break
99
    else:
100
        lang = None
101
        import locale
102
        try:
103
            import ctypes
104
        except ImportError:
105
            # use only user's default locale
106
            lang = locale.getdefaultlocale()[0]
107
        else:
108
            # using ctypes to determine all locales
109
            lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
110
            lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
111
            if lcid_user != lcid_system:
112
                lcid = [lcid_user, lcid_system]
113
            else:
114
                lcid = [lcid_user]
115
            lang = [locale.windows_locale.get(i) for i in lcid]
116
            lang = ':'.join([i for i in lang if i])
117
        # set lang code for gettext
118
        if lang:
119
            os.environ['LANGUAGE'] = lang
120
121
122
def _get_current_locale():
123
    if not os.environ.get('LANGUAGE'):
124
        from bzrlib import config
125
        lang = config.GlobalConfig().get_user_option('language')
126
        if lang:
127
            os.environ['LANGUAGE'] = lang
128
            return lang
129
    if sys.platform == 'win32':
130
        _check_win32_locale()
131
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
132
        lang = os.environ.get(i)
133
        if lang:
134
            return lang
135
    return None