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