Esempio n. 1
0
 def translate(self, lang, encoding = None, do_Q=False):
     if lang == 'en':
         return self.data
     res = self.get(lang)
     if isinstance(res, unicode):
         if encoding is None:
             import registry
             try:
                 encoding = registry.team(lang).charset
             except KeyError:
                 encoding = 'utf-8'
             if encoding is None:
                 encoding = 'iso-8859-1'
         try:
             res = res.encode(encoding)
         except (KeyError, UnicodeError):
             encoding="utf-8"
             res = res.encode('utf-8')
     if do_Q and encoding:
         # Do quoted printable encoding.
         from email.Charset import Charset
         charset = Charset(encoding)
         lines = res.split('\n')
         for i in range(len(lines)):
             l = lines[i]
             try:
                 l.decode("us-ascii")
             except UnicodeError:
                 pass
             else:
                 continue
             f,b = l.split(": ",1) # RFC822 field and body
             lines[i] = f+": "+charset.header_encode(b)
         res = "\n".join(lines)
     return res
Esempio n. 2
0
def _encode_address_string(text, charset):
    """Split the email into parts and use header encoding on the name
    part if needed. We do this because the actual addresses need to be
    ASCII with no encoding for most SMTP servers, but the non-address
    parts should be encoded appropriately."""
    header = Header()
    name, addr = parseaddr(text)
    try:
        name.decode('us-ascii')
    except UnicodeDecodeError:
        if charset:
            charset = Charset(charset)
            name = charset.header_encode(name)
    # We again replace rather than raise an error or pass an 8bit string
    header.append(formataddr((name, addr)), errors='replace')
    return header
Esempio n. 3
0
def _encode_address_string(text, charset):
    """Split the email into parts and use header encoding on the name
    part if needed. We do this because the actual addresses need to be
    ASCII with no encoding for most SMTP servers, but the non-address
    parts should be encoded appropriately."""
    header = Header()
    name, addr = parseaddr(text)
    try:
        name.decode('us-ascii')
    except UnicodeDecodeError:
        if charset:
            charset = Charset(charset)
            name = charset.header_encode(name)
    # We again replace rather than raise an error or pass an 8bit string
    header.append(formataddr((name, addr)), errors='replace')
    return header