~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/i18n.py

  • Committer: Vincent Ladeuil
  • Date: 2011-06-27 15:42:09 UTC
  • mfrom: (5993 +trunk)
  • mto: (5993.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5994.
  • Revision ID: v.ladeuil+lp@free.fr-20110627154209-azubuhbuxsz109hq
Merge trunk

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