~sambuddhabasu1/mailman/fix_mailman_run_error

« back to all changes in this revision

Viewing changes to Mailman/i18n.py

  • Committer: bwarsaw
  • Date: 2007-03-21 14:11:53 UTC
  • Revision ID: vcs-imports@canonical.com-20070321141153-chq79cj8kdnvku1p
Test suite repair.  All tests are now passing again.

- In i18n.py, change this method so that everything it returns will be
  guaranteed to be a unicode.  Mailman 2.2 will be unicode-safe, meaning all
  strings internally will be unicodes.  The translation service is one
  boundary point were strings come from the outside, so ensure that they are
  unicodes and convert if necessary.  This may break some things, but it's
  better to fix those situations than to continue to return 8-bit strings from
  _().

- In Mailman/testing/base.py, craft a fake module called Mailman.MTA.stub and
  stick no-op functions on stub.create() and stub.remove().  We really don't
  need the MTA modules for testing purposes (yet at least), and if you're
  using the default configuration, you'll get tons of cruft on stdout when the
  Manual MTA tries to add and remove mailing lists.

  Set up the test configuration environment to use this stub MTA module.

- In test_handlers.py, remove an extraneous str().

- Convert ToDigest.py, Hold.py and Acknowledge.py to __i18n_templates__.  (I'm
  pretty darn close to just making everything use $-strings by default.)

- In CookHeaders.py, there's no need to unicode()-ify the subject since that
  should already be a unicode when passed from _().

- In MailList.py, we can use the str.capitalize() method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
 
74
74
def _(s):
75
75
    if s == '':
76
 
        return s
 
76
        return u''
77
77
    assert s
78
78
    # Do translation of the given string into the current language, and do
79
79
    # Ping-string interpolation into the resulting string.
97
97
    d = frame.f_globals.copy()
98
98
    d.update(frame.f_locals)
99
99
    use_templates = d.get('__i18n_templates__', False)
100
 
    # Translating the string returns an encoded 8-bit string.  Rather than
101
 
    # turn that into a Unicode, we turn any Unicodes in the dictionary values
102
 
    # into encoded 8-bit strings.  XXX: Returning a Unicode here broke too
103
 
    # much other stuff and _() has many tentacles.  Eventually I think we want
104
 
    # to use Unicode everywhere.
105
 
    tns = _translation.gettext(s)
106
 
    charset = _translation.charset()
107
 
    if not charset:
108
 
        charset = 'us-ascii'
 
100
    # Mailman must be unicode safe internally (i.e. all strings inside Mailman
 
101
    # must be unicodes).  The translation service is one boundary to the
 
102
    # outside world, so to honor this constraint, make sure that all strings
 
103
    # to come out of _() are unicodes, even if the translated string or
 
104
    # dictionary values are 8-bit strings.
 
105
    tns = _translation.ugettext(s)
 
106
    charset = _translation.charset() or 'us-ascii'
109
107
    for k, v in d.items():
110
 
        if isinstance(v, unicode):
111
 
            d[k] = v.encode(charset, 'replace')
 
108
        if isinstance(v, str):
 
109
            d[k] = unicode(v, charset, 'replace')
112
110
    # Are we using $-strings or %-strings?
113
111
    if use_templates:
114
 
        return Template(tns).safe_substitute(attrdict(d))
115
 
    if type(tns) == str:
116
 
        tns = unicode(tns, charset)
117
 
    return SafeDict(d, charset=charset).interpolate(tns)
 
112
        translated_string = Template(tns).safe_substitute(attrdict(d))
 
113
    else:
 
114
        translated_string = SafeDict(d, charset=charset).interpolate(tns)
 
115
    if isinstance(translated_string, str):
 
116
        translated_string = unicode(translated_string, charset)
 
117
    return translated_string
118
118
 
119
119
 
120
120