~mailman-coders/mailman/2.1

« back to all changes in this revision

Viewing changes to Mailman/i18n.py

Fixed l10n to use correct charset for command-line scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2000-2010 by the Free Software Foundation, Inc.
 
1
# Copyright (C) 2000-2016 by the Free Software Foundation, Inc.
2
2
#
3
3
# This program is free software; you can redistribute it and/or
4
4
# modify it under the terms of the GNU General Public License
17
17
 
18
18
import sys
19
19
import time
 
20
import locale
20
21
import gettext
21
22
from types import StringType, UnicodeType
22
23
 
25
26
 
26
27
_translation = None
27
28
 
 
29
 
 
30
def _get_ctype_charset():
 
31
    old = locale.setlocale(locale.LC_CTYPE, '')
 
32
    charset = locale.nl_langinfo(locale.CODESET)
 
33
    locale.setlocale(locale.LC_CTYPE, old)
 
34
    return charset
 
35
 
 
36
_ctype_charset = _get_ctype_charset()
 
37
 
28
38
 
29
39
 
30
40
def set_language(language=None):
54
64
 
55
65
 
56
66
 
57
 
def _(s):
 
67
def _(s, frame=1):
58
68
    if s == '':
59
69
        return s
60
70
    assert s
70
80
    # original string is 1) locals dictionary, 2) globals dictionary.
71
81
    #
72
82
    # First, get the frame of the caller
73
 
    frame = sys._getframe(1)
 
83
    frame = sys._getframe(frame)
74
84
    # A `safe' dictionary is used so we won't get an exception if there's a
75
85
    # missing key in the dictionary.
76
86
    dict = SafeDict(frame.f_globals.copy())
95
105
 
96
106
 
97
107
 
 
108
def tolocale(s):
 
109
    global _ctype_charset
 
110
    if isinstance(s, UnicodeType):
 
111
        return s
 
112
    source = _translation.charset ()
 
113
    if not source:
 
114
        return s
 
115
    return unicode(s, source, 'replace').encode(_ctype_charset, 'replace')
 
116
 
 
117
def C_(s):
 
118
    return tolocale(_(s, 2))
 
119
    
 
120
 
98
121
def ctime(date):
99
122
    # Don't make these module globals since we have to do runtime translation
100
123
    # of the strings anyway.