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 |
|
|
6681.2.4
by Jelmer Vernooij
More renames. |
21 |
# This module is copied from Bazaar Explorer and modified for brz.
|
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
22 |
|
|
6379.6.7
by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear. |
23 |
"""i18n and l10n support for Bazaar."""
|
24 |
||
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
25 |
import gettext as _gettext |
26 |
import os |
|
27 |
import sys |
|
28 |
||
|
6162.4.7
by Jonathan Riddell
two empty lines between top level items |
29 |
|
|
6133.3.10
by Jonathan Riddell
default _translations back to None so we can tell if it gets installed |
30 |
_translations = None |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
31 |
|
32 |
||
|
5875.2.7
by INADA Naoki
Change order of functions. |
33 |
def gettext(message): |
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
34 |
"""Translate message. |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
35 |
|
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
36 |
:returns: translated message as unicode.
|
37 |
"""
|
|
|
6131.2.1
by Jonathan Riddell
install translations whenever they are used |
38 |
install() |
|
6621.2.26
by Martin
Misc set of changes to get started with selftest on Python 3 |
39 |
try: |
40 |
return _translations.ugettext(message) |
|
41 |
except AttributeError: |
|
42 |
return _translations.gettext(message) |
|
|
5875.2.7
by INADA Naoki
Change order of functions. |
43 |
|
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
44 |
|
|
5971.1.24
by Jonathan Riddell
fix translations for plural forms |
45 |
def ngettext(singular, plural, number): |
46 |
"""Translate message with plural forms based on `number`. |
|
47 |
||
48 |
:param singular: English language message in singular form
|
|
49 |
:param plural: English language message in plural form
|
|
50 |
: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. |
51 |
|
52 |
:returns: translated message as unicode.
|
|
53 |
"""
|
|
|
6131.2.1
by Jonathan Riddell
install translations whenever they are used |
54 |
install() |
|
6621.2.26
by Martin
Misc set of changes to get started with selftest on Python 3 |
55 |
try: |
56 |
return _translations.ungettext(singular, plural, number) |
|
57 |
except AttributeError: |
|
58 |
return _translations.ngettext(singular, plural, number) |
|
|
5875.2.7
by INADA Naoki
Change order of functions. |
59 |
|
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
60 |
|
|
5875.2.7
by INADA Naoki
Change order of functions. |
61 |
def N_(msg): |
62 |
"""Mark message for translation but don't translate it right away.""" |
|
63 |
return msg |
|
64 |
||
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
65 |
|
|
5875.2.7
by INADA Naoki
Change order of functions. |
66 |
def gettext_per_paragraph(message): |
67 |
"""Translate message per paragraph. |
|
68 |
||
|
5875.2.9
by Vincent Ladeuil
Cleanup some PEP8 issues and move test code in test module, test_multiline still failing. |
69 |
:returns: concatenated translated message as unicode.
|
70 |
"""
|
|
|
6133.3.14
by Jonathan Riddell
install() translations for per_paragraph too |
71 |
install() |
|
5875.2.7
by INADA Naoki
Change order of functions. |
72 |
paragraphs = message.split(u'\n\n') |
73 |
# Be careful not to translate the empty string -- it holds the
|
|
74 |
# meta data of the .po file.
|
|
|
6621.2.26
by Martin
Misc set of changes to get started with selftest on Python 3 |
75 |
return u'\n\n'.join(gettext(p) if p else u'' for p in paragraphs) |
|
5875.2.7
by INADA Naoki
Change order of functions. |
76 |
|
77 |
||
|
6131.2.3
by Jonathan Riddell
rename to disable_i18n() to follow convention |
78 |
def disable_i18n(): |
|
6131.2.2
by Jonathan Riddell
add disableI18n() function so bzrlib users can chose not to use i18n |
79 |
"""Do not allow i18n to be enabled. Useful for third party users |
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
80 |
of breezy."""
|
|
6133.3.13
by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__ |
81 |
global _translations |
|
6133.3.10
by Jonathan Riddell
default _translations back to None so we can tell if it gets installed |
82 |
_translations = _gettext.NullTranslations() |
|
6131.2.2
by Jonathan Riddell
add disableI18n() function so bzrlib users can chose not to use i18n |
83 |
|
|
6133.3.13
by Jonathan Riddell
vila sorts it out, fix disable_i18n() and use it in tests.__init__ |
84 |
|
|
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
85 |
def installed(): |
|
6133.3.10
by Jonathan Riddell
default _translations back to None so we can tell if it gets installed |
86 |
"""Returns whether translations are in use or not.""" |
87 |
return _translations is not None |
|
|
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
88 |
|
89 |
||
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
90 |
def install(lang=None): |
|
6681.2.4
by Jelmer Vernooij
More renames. |
91 |
"""Enables gettext translations in brz.""" |
|
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
92 |
global _translations |
|
6112.1.1
by Jonathan Riddell
check for installed i18n before doing install |
93 |
if installed(): |
94 |
return
|
|
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
95 |
_translations = install_translations(lang) |
96 |
||
|
6162.4.7
by Jonathan Riddell
two empty lines between top level items |
97 |
|
|
6681.2.4
by Jelmer Vernooij
More renames. |
98 |
def install_translations(lang=None, domain='brz', locale_base=None): |
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
99 |
"""Create a gettext translation object. |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
100 |
|
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
101 |
:param lang: language to install.
|
102 |
:param domain: translation domain to install.
|
|
103 |
:param locale_base: plugins can specify their own directory.
|
|
104 |
||
105 |
:returns: a gettext translations object to use
|
|
106 |
"""
|
|
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
107 |
if lang is None: |
108 |
lang = _get_current_locale() |
|
|
6025.1.1
by Jelmer Vernooij
Fix i18n use when no environment variables are set. |
109 |
if lang is not None: |
110 |
languages = lang.split(':') |
|
111 |
else: |
|
112 |
languages = None |
|
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
113 |
translation = _gettext.translation( |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
114 |
domain, |
115 |
localedir=_get_locale_dir(locale_base), |
|
116 |
languages=languages, |
|
117 |
fallback=True) |
|
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
118 |
return translation |
|
5875.2.7
by INADA Naoki
Change order of functions. |
119 |
|
|
6162.4.7
by Jonathan Riddell
two empty lines between top level items |
120 |
|
|
6162.4.2
by Jonathan Riddell
add add_fallback() method to i18n |
121 |
def add_fallback(fallback): |
122 |
""" |
|
123 |
Add a fallback translations object. Typically used by plugins.
|
|
124 |
||
125 |
:param fallback: gettext.GNUTranslations object
|
|
126 |
"""
|
|
127 |
install() |
|
128 |
_translations.add_fallback(fallback) |
|
129 |
||
|
6162.4.7
by Jonathan Riddell
two empty lines between top level items |
130 |
|
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
131 |
def uninstall(): |
|
6133.3.10
by Jonathan Riddell
default _translations back to None so we can tell if it gets installed |
132 |
"""Disables gettext translations.""" |
|
5875.3.25
by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation. |
133 |
global _translations |
|
6133.3.10
by Jonathan Riddell
default _translations back to None so we can tell if it gets installed |
134 |
_translations = None |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
135 |
|
136 |
||
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
137 |
def _get_locale_dir(base): |
138 |
"""Returns directory to find .mo translations file in, either local or system |
|
139 |
||
140 |
:param base: plugins can specify their own local directory
|
|
141 |
"""
|
|
|
6437.56.1
by Martin Packman
Fall back to sys.prefix not /usr when looking for .mo files |
142 |
if getattr(sys, 'frozen', False): |
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
143 |
if base is None: |
|
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
144 |
base = os.path.dirname(sys.executable) |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
145 |
return os.path.join(base, u'locale') |
146 |
else: |
|
|
6162.4.1
by Jonathan Riddell
alter code to make it more usable by plugins, add install_translations() |
147 |
if base is None: |
|
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
148 |
base = os.path.dirname(__file__) |
|
5875.2.4
by INADA Naoki
Search '/usr/share/locale' when bzrlib/locale does not exist. |
149 |
dirpath = os.path.realpath(os.path.join(base, u'locale')) |
150 |
if os.path.exists(dirpath): |
|
151 |
return dirpath |
|
|
7479.2.1
by Jelmer Vernooij
Drop python2 support. |
152 |
return os.path.join(sys.prefix, u"share", u"locale") |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
153 |
|
154 |
||
155 |
def _check_win32_locale(): |
|
|
6809.1.1
by Martin
Apply 2to3 ws_comma fixer |
156 |
for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
157 |
if os.environ.get(i): |
158 |
break
|
|
159 |
else: |
|
160 |
lang = None |
|
161 |
import locale |
|
162 |
try: |
|
163 |
import ctypes |
|
164 |
except ImportError: |
|
165 |
# use only user's default locale
|
|
166 |
lang = locale.getdefaultlocale()[0] |
|
167 |
else: |
|
168 |
# using ctypes to determine all locales
|
|
169 |
lcid_user = ctypes.windll.kernel32.GetUserDefaultLCID() |
|
170 |
lcid_system = ctypes.windll.kernel32.GetSystemDefaultLCID() |
|
171 |
if lcid_user != lcid_system: |
|
172 |
lcid = [lcid_user, lcid_system] |
|
173 |
else: |
|
174 |
lcid = [lcid_user] |
|
175 |
lang = [locale.windows_locale.get(i) for i in lcid] |
|
176 |
lang = ':'.join([i for i in lang if i]) |
|
177 |
# set lang code for gettext
|
|
178 |
if lang: |
|
179 |
os.environ['LANGUAGE'] = lang |
|
180 |
||
181 |
||
182 |
def _get_current_locale(): |
|
183 |
if not os.environ.get('LANGUAGE'): |
|
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
184 |
from . import config |
|
6056.2.3
by Vincent Ladeuil
Migrate language. |
185 |
lang = config.GlobalStack().get('language') |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
186 |
if lang: |
187 |
os.environ['LANGUAGE'] = lang |
|
188 |
return lang |
|
189 |
if sys.platform == 'win32': |
|
190 |
_check_win32_locale() |
|
|
6809.1.1
by Martin
Apply 2to3 ws_comma fixer |
191 |
for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
|
5875.2.1
by INADA Naoki
Add bzrlib.i18n module. |
192 |
lang = os.environ.get(i) |
193 |
if lang: |
|
194 |
return lang |
|
195 |
return None |
|
|
6189.1.1
by Jelmer Vernooij
Add a load_plugin_translations method. |
196 |
|
197 |
||
198 |
def load_plugin_translations(domain): |
|
199 |
"""Load the translations for a specific plugin. |
|
200 |
||
|
6681.2.4
by Jelmer Vernooij
More renames. |
201 |
:param domain: Gettext domain name (usually 'brz-PLUGINNAME')
|
|
6189.1.1
by Jelmer Vernooij
Add a load_plugin_translations method. |
202 |
"""
|
|
7058.4.10
by Jelmer Vernooij
Fix i18n stuff. |
203 |
locale_base = os.path.dirname(__file__) |
|
6189.1.1
by Jelmer Vernooij
Add a load_plugin_translations method. |
204 |
translation = install_translations(domain=domain, |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
205 |
locale_base=locale_base) |
|
6189.1.1
by Jelmer Vernooij
Add a load_plugin_translations method. |
206 |
add_fallback(translation) |
207 |
return translation |