2 # -*- coding: utf-8 -*-
4 #Copyright 2010-2011 Mika Bostrom
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
27 encoder_to_utf
= codecs
.lookup('utf-8')
28 encoder_to_sys
= codecs
.lookup(Configuration
.LOCALE_ENCODING
)
30 # I'm saving a few cycles with this one
31 not_needed1
, not_needed2
, not_needed3
= False, False, False
32 if Configuration
.LOCALE_ENCODING
== 'UTF8':
33 not_needed1
, not_needed2
, not_needed3
= True, True, True
36 if not_needed1
: return s
39 #(_out, _len) = encoder_to_utf.encode(s)
40 _out
= unicode(s
, Configuration
.LOCALE_ENCODING
).encode('utf-8')
42 except UnicodeDecodeError:
43 sys
.stderr
.write(_('Could not convert: "%s"') % (s
+"\n"))
45 except UnicodeEncodeError:
46 sys
.stderr
.write(_('Could not encode: "%s"') % (s
+"\n"))
48 except TypeError: # TypeError is raised when we give unicode() an already encoded string
52 if not_needed2
: return s
55 (_out
, _len
) = encoder_to_utf
.encode(unicode(s
))
57 except UnicodeDecodeError:
58 sys
.stderr
.write(_('Could not convert: "%s"') % (s
+"\n"))
60 except UnicodeEncodeError:
61 sys
.stderr
.write(_('Could not encode: "%s"') % (s
+"\n"))
65 if not_needed3
: return s
68 # we usually don't want to use 'replace' but this is only for displaying
69 # in the gui so it doesn't matter if names are missing an accent or two
70 (_out
, _len
) = encoder_to_sys
.encode(s
, 'replace')
72 except UnicodeDecodeError:
73 sys
.stderr
.write(_('Could not convert: "%s"') % (s
+"\n"))
75 except UnicodeEncodeError:
76 sys
.stderr
.write(_('Could not encode: "%s"') % (s
+"\n"))