1 |
bh |
2642 |
# Copyright (c) 2001, 2003, 2005 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
jan |
373 |
# Jan-Oliver Wagner <[email protected]> |
5 |
bh |
6 |
# |
6 |
|
|
# This program is free software under the GPL (>=v2) |
7 |
|
|
# Read the file COPYING coming with Thuban for details. |
8 |
jan |
373 |
|
9 |
bh |
671 |
import os |
10 |
|
|
|
11 |
bh |
1871 |
# Thuban Message Translation |
12 |
bh |
671 |
# |
13 |
bh |
1871 |
# This is somewhat tricky. On the one hand we want to use the wx |
14 |
|
|
# facilities because that way the standard wx messages are also |
15 |
|
|
# translated and we get automatic conversion of encodings so the we can |
16 |
|
|
# use e.g. an UTF po/mo file in a Latin 1 environment. OTOH, we do not |
17 |
|
|
# want to import the wxPython modules at all when running the test suite |
18 |
|
|
# because otherwise the test suite would require a working X server |
19 |
|
|
# connection. |
20 |
bh |
671 |
# |
21 |
bh |
1871 |
# Therefore this module only provides the hooks for installing the |
22 |
|
|
# correct translation function with a default translation function that |
23 |
|
|
# does nothing and simply returns the string it gets as argument. |
24 |
|
|
# |
25 |
|
|
# The front end to the installed translation function is _ (see it's |
26 |
|
|
# doc-string). |
27 |
|
|
# |
28 |
|
|
# The Thuban.UI module is responsible for installing the wx translation |
29 |
|
|
# function. It must take care to install the translation function as |
30 |
|
|
# early as possible, i.e. when Thuban/UI/__init__.py is executed so that |
31 |
|
|
# strings translated at module import time are translated (this also |
32 |
|
|
# means that a program built on top of Thuban and which uses Thuban.UI |
33 |
|
|
# should start by importing Thuban.UI before any other Thuban module. |
34 |
|
|
# |
35 |
|
|
# At the same time Thuban/UI/__init__.py should not import any wxPython |
36 |
|
|
# module unless it really has to install the translation function, i.e. |
37 |
|
|
# when no other translation function has already been installed. That |
38 |
|
|
# way the test suite can override the wx translation by installing its |
39 |
|
|
# own translation function before importing anything from Thuban.UI and |
40 |
|
|
# actually before importing anything but the Thuban module itself. |
41 |
|
|
|
42 |
|
|
# Thedirectory holding the translation files (actually they're in |
43 |
|
|
# language specific subdirectories of _message_dir) |
44 |
bh |
671 |
_message_dir = os.path.join(os.path.dirname(__file__), os.pardir, "Resources", |
45 |
|
|
"Locale") |
46 |
|
|
|
47 |
bh |
1871 |
def _(s): |
48 |
|
|
"""Return a localized version of the the string s |
49 |
bh |
671 |
|
50 |
bh |
1871 |
This is the function to use in the sources to translate strings and |
51 |
|
|
it simply delegates the translation to the installable translation |
52 |
|
|
function. It's done this way so that _ may be imported with 'from |
53 |
|
|
Thuban import _' even when the correct translation function hasn't |
54 |
|
|
been installed yet. |
55 |
|
|
""" |
56 |
|
|
return _translation_function(s) |
57 |
|
|
|
58 |
|
|
def gettext_identity(s): |
59 |
|
|
"""Default gettext implementation which returns the string as is""" |
60 |
|
|
return s |
61 |
|
|
|
62 |
|
|
_translation_function = gettext_identity |
63 |
|
|
|
64 |
|
|
def translation_function_installed(): |
65 |
|
|
"""Return whether a translation function has been installed.""" |
66 |
|
|
return _translation_function is not gettext_identity |
67 |
|
|
|
68 |
|
|
def install_translation_function(function): |
69 |
|
|
"""Install function as the translation function |
70 |
|
|
|
71 |
|
|
If a translation has already been installed that is not the default |
72 |
|
|
implementation (gettext_identity) do nothing. |
73 |
|
|
""" |
74 |
|
|
global _translation_function |
75 |
|
|
if not translation_function_installed(): |
76 |
|
|
_translation_function = function |
77 |
bh |
2642 |
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
# String representation in Thuban |
81 |
|
|
# |
82 |
|
|
# Thuban has an internal representation for textual data that all text |
83 |
|
|
# that comes into Thuban has to be converted into. Any text written by |
84 |
|
|
# Thuban has to be converted to whatever is needed by the output device. |
85 |
|
|
# |
86 |
|
|
# Currently, the internal representation is usually a byte-string in a |
87 |
|
|
# particuler encoding. For more details see the file |
88 |
|
|
# Doc/technotes/string_representation.txt. |
89 |
|
|
|
90 |
|
|
# The encoding to use for the internal string representation. Usually |
91 |
|
|
# it's a string with the encoding name to use when converting between |
92 |
|
|
# Python byte-strings and unicode objects. The special value "unicode" |
93 |
|
|
# means the use unicode objects as the internal representation. |
94 |
|
|
_internal_encoding = None |
95 |
|
|
|
96 |
|
|
def internal_from_unicode(unistr): |
97 |
|
|
"""Return Thuban's internal representation for the unicode object unistr""" |
98 |
|
|
if _internal_encoding != "unicode": |
99 |
|
|
# we use replace so that we don't get exceptions when the |
100 |
|
|
# conversion can't be done properly. |
101 |
|
|
return unistr.encode(_internal_encoding, "replace") |
102 |
|
|
else: |
103 |
|
|
return unistr |
104 |
|
|
|
105 |
|
|
def unicode_from_internal(s): |
106 |
|
|
"""Return the unicode object for the string s in internal representation""" |
107 |
|
|
if _internal_encoding != "unicode": |
108 |
|
|
return unicode(s, _internal_encoding) |
109 |
|
|
else: |
110 |
|
|
return s |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
def set_internal_encoding(encoding): |
114 |
|
|
"""Set the encoding to use for the internal string representation |
115 |
|
|
|
116 |
|
|
The parameter should be the name of an encoding known to Python so |
117 |
|
|
that it can be used with e.g. the encode method of unicode objects. |
118 |
|
|
As a special case it can be the string 'unicode' to indicate that |
119 |
|
|
the internal representation are unicode objects. |
120 |
|
|
""" |
121 |
|
|
global _internal_encoding |
122 |
|
|
_internal_encoding = encoding |
123 |
bernhard |
2672 |
|
124 |
|
|
# and now let us test, if we can go back and forth |
125 |
|
|
# it is better to complain now than to have runtime problems later |
126 |
|
|
unicode_from_internal(internal_from_unicode(u'')) |