~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-11 07:49:59 UTC
  • mto: (6110.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6021.
  • Revision ID: v.ladeuil+lp@free.fr-20110711074959-igi048gv89bfc93d
Open trunk as 2.5dev1

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 bzr.
 
22
 
 
23
"""i18n and l10n support for Bazaar."""
 
24
 
 
25
import gettext as _gettext
 
26
import os
 
27
import sys
 
28
 
 
29
_translations = None
 
30
 
 
31
 
 
32
def gettext(message):
 
33
    """Translate message. 
 
34
    
 
35
    :returns: translated message as unicode.
 
36
    """
 
37
    return _translations.ugettext(message)
 
38
 
 
39
 
 
40
def ngettext(singular, plural, number):
 
41
    """Translate message with plural forms based on `number`.
 
42
 
 
43
    :param singular: English language message in singular form
 
44
    :param plural: English language message in plural form
 
45
    :param number: the number this message should be translated for
 
46
 
 
47
    :returns: translated message as unicode.
 
48
    """
 
49
    return _translations.ungettext(singular, plural, number)
 
50
 
 
51
 
 
52
def N_(msg):
 
53
    """Mark message for translation but don't translate it right away."""
 
54
    return msg
 
55
 
 
56
 
 
57
def gettext_per_paragraph(message):
 
58
    """Translate message per paragraph.
 
59
 
 
60
    :returns: concatenated translated message as unicode.
 
61
    """
 
62
    paragraphs = message.split(u'\n\n')
 
63
    ugettext = _translations.ugettext
 
64
    # Be careful not to translate the empty string -- it holds the
 
65
    # meta data of the .po file.
 
66
    return u'\n\n'.join(ugettext(p) if p else u'' for p in paragraphs)
 
67
 
 
68
 
 
69
def installed():
 
70
    return _translations is not None
 
71
 
 
72
 
 
73
def install(lang=None):
 
74
    global _translations
 
75
    if lang is None:
 
76
        lang = _get_current_locale()
 
77
    _translations = _gettext.translation(
 
78
            'bzr',
 
79
            localedir=_get_locale_dir(),
 
80
            languages=lang.split(':'),
 
81
            fallback=True)
 
82
 
 
83
 
 
84
def uninstall():
 
85
    global _translations
 
86
    _translations = None
 
87
 
 
88
 
 
89
def _get_locale_dir():
 
90
    if hasattr(sys, 'frozen'):
 
91
        base = os.path.dirname(
 
92
                unicode(sys.executable, sys.getfilesystemencoding()))
 
93
        return os.path.join(base, u'locale')
 
94
    else:
 
95
        base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
 
96
        dirpath = os.path.realpath(os.path.join(base, u'locale'))
 
97
        if os.path.exists(dirpath):
 
98
            return dirpath
 
99
        else:
 
100
            return '/usr/share/locale'
 
101
 
 
102
 
 
103
def _check_win32_locale():
 
104
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
 
105
        if os.environ.get(i):
 
106
            break
 
107
    else:
 
108
        lang = None
 
109
        import locale
 
110
        try:
 
111
            import ctypes
 
112
        except ImportError:
 
113
            # use only user's default locale
 
114
            lang = locale.getdefaultlocale()[0]
 
115
        else:
 
116
            # using ctypes to determine all locales
 
117
            lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID()
 
118
            lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID()
 
119
            if lcid_user != lcid_system:
 
120
                lcid = [lcid_user, lcid_system]
 
121
            else:
 
122
                lcid = [lcid_user]
 
123
            lang = [locale.windows_locale.get(i) for i in lcid]
 
124
            lang = ':'.join([i for i in lang if i])
 
125
        # set lang code for gettext
 
126
        if lang:
 
127
            os.environ['LANGUAGE'] = lang
 
128
 
 
129
 
 
130
def _get_current_locale():
 
131
    if not os.environ.get('LANGUAGE'):
 
132
        from bzrlib import config
 
133
        lang = config.GlobalConfig().get_user_option('language')
 
134
        if lang:
 
135
            os.environ['LANGUAGE'] = lang
 
136
            return lang
 
137
    if sys.platform == 'win32':
 
138
        _check_win32_locale()
 
139
    for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'):
 
140
        lang = os.environ.get(i)
 
141
        if lang:
 
142
            return lang
 
143
    return None