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.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
29 |
_translations = None |
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.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
37 |
return _translations.ugettext(message) |
5875.2.7
by INADA Naoki
Change order of functions. |
38 |
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
39 |
|
5971.1.24
by Jonathan Riddell
fix translations for plural forms |
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
|
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
46 |
|
47 |
:returns: translated message as unicode.
|
|
48 |
"""
|
|
5971.1.77
by Jonathan Riddell
merge in trunk |
49 |
return _translations.ungettext(singular, plural, number) |
5875.2.7
by INADA Naoki
Change order of functions. |
50 |
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
51 |
|
5875.2.7
by INADA Naoki
Change order of functions. |
52 |
def N_(msg): |
53 |
"""Mark message for translation but don't translate it right away."""
|
|
54 |
return msg |
|
55 |
||
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
56 |
|
5875.2.7
by INADA Naoki
Change order of functions. |
57 |
def gettext_per_paragraph(message): |
58 |
"""Translate message per paragraph.
|
|
59 |
||
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
60 |
:returns: concatenated translated message as unicode.
|
61 |
"""
|
|
5875.2.7
by INADA Naoki
Change order of functions. |
62 |
paragraphs = message.split(u'\n\n') |
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
63 |
ugettext = _translations.ugettext |
5875.2.7
by INADA Naoki
Change order of functions. |
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 |
||
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
69 |
def installed(): |
70 |
return _translations is not None |
|
71 |
||
72 |
||
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
73 |
def install(lang=None): |
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
74 |
global _translations |
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
75 |
if lang is None: |
76 |
lang = _get_current_locale() |
|
6025.1.1
by Jelmer Vernooij
Fix i18n use when no environment variables are set. |
77 |
if lang is not None: |
78 |
languages = lang.split(':') |
|
79 |
else: |
|
80 |
languages = None |
|
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
81 |
_translations = _gettext.translation( |
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
82 |
'bzr', |
83 |
localedir=_get_locale_dir(), |
|
6025.1.1
by Jelmer Vernooij
Fix i18n use when no environment variables are set. |
84 |
languages=languages, |
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
85 |
fallback=True) |
86 |
||
5875.2.7
by INADA Naoki
Change order of functions. |
87 |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
88 |
def uninstall(): |
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
89 |
global _translations |
90 |
_translations = None |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
91 |
|
92 |
||
93 |
def _get_locale_dir(): |
|
94 |
if hasattr(sys, 'frozen'): |
|
95 |
base = os.path.dirname( |
|
96 |
unicode(sys.executable, sys.getfilesystemencoding())) |
|
97 |
return os.path.join(base, u'locale') |
|
98 |
else: |
|
99 |
base = os.path.dirname(unicode(__file__, sys.getfilesystemencoding())) |
|
5875.2.4
by INADA Naoki
Search '/usr/share/locale' when bzrlib/locale does not exist. |
100 |
dirpath = os.path.realpath(os.path.join(base, u'locale')) |
101 |
if os.path.exists(dirpath): |
|
102 |
return dirpath |
|
103 |
else: |
|
104 |
return '/usr/share/locale' |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
105 |
|
106 |
||
107 |
def _check_win32_locale(): |
|
108 |
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'): |
|
109 |
if os.environ.get(i): |
|
110 |
break
|
|
111 |
else: |
|
112 |
lang = None |
|
113 |
import locale |
|
114 |
try: |
|
115 |
import ctypes |
|
116 |
except ImportError: |
|
117 |
# use only user's default locale
|
|
118 |
lang = locale.getdefaultlocale()[0] |
|
119 |
else: |
|
120 |
# using ctypes to determine all locales
|
|
121 |
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID() |
|
122 |
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID() |
|
123 |
if lcid_user != lcid_system: |
|
124 |
lcid = [lcid_user, lcid_system] |
|
125 |
else: |
|
126 |
lcid = [lcid_user] |
|
127 |
lang = [locale.windows_locale.get(i) for i in lcid] |
|
128 |
lang = ':'.join([i for i in lang if i]) |
|
129 |
# set lang code for gettext
|
|
130 |
if lang: |
|
131 |
os.environ['LANGUAGE'] = lang |
|
132 |
||
133 |
||
134 |
def _get_current_locale(): |
|
135 |
if not os.environ.get('LANGUAGE'): |
|
136 |
from bzrlib import config |
|
137 |
lang = config.GlobalConfig().get_user_option('language') |
|
138 |
if lang: |
|
139 |
os.environ['LANGUAGE'] = lang |
|
140 |
return lang |
|
141 |
if sys.platform == 'win32': |
|
142 |
_check_win32_locale() |
|
143 |
for i in ('LANGUAGE','LC_ALL','LC_MESSAGES','LANG'): |
|
144 |
lang = os.environ.get(i) |
|
145 |
if lang: |
|
146 |
return lang |
|
147 |
return None |