Localizer, Parse .po files with polib

Instead of itools.gettext
parent 8aabfd28
# -*- coding: UTF-8 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2000-2007 Juan David Ibáñez Palomar <jdavid@itaapy.com> # Copyright (C) 2000-2007 Juan David Ibáñez Palomar <jdavid@itaapy.com>
# Copyright (C) 2003 Roberto Quero, Eduardo Corrales # Copyright (C) 2003 Roberto Quero, Eduardo Corrales
# Copyright (C) 2004 Søren Roug # Copyright (C) 2004 Søren Roug
...@@ -28,11 +28,8 @@ from re import compile ...@@ -28,11 +28,8 @@ from re import compile
from time import gmtime, strftime, time from time import gmtime, strftime, time
from urllib import quote from urllib import quote
# Import from itools # Import from polib
from itools.datatypes import LanguageTag import polib
import itools.gettext
from itools.tmx import TMXFile, Sentence, TMXUnit, TMXNote
from itools.xliff import XLFFile, XLFNote
# Import from Zope # Import from Zope
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
...@@ -66,16 +63,21 @@ def md5text(str): ...@@ -66,16 +63,21 @@ def md5text(str):
return md5(str.encode('utf-8')).hexdigest() return md5(str.encode('utf-8')).hexdigest()
def to_unicode(x): def to_unicode(x, encoding=HTTPRequest.default_encoding):
"""In Zope the ISO-8859-1 encoding has an special status, normal strings """In Zope the ISO-8859-1 encoding has an special status, normal strings
are considered to be in this encoding by default. are considered to be in this encoding by default.
""" """
if isinstance(x, unicode): if isinstance(x, unicode):
return x return x
encoding = HTTPRequest.default_encoding
return unicode(x, encoding) return unicode(x, encoding)
def to_str(x):
"""Make sure we have an (utf-8 encoded) string"""
if isinstance(x, str):
return x
x.encode('utf-8')
def message_encode(message): def message_encode(message):
"""Encodes a message to an ASCII string. """Encodes a message to an ASCII string.
...@@ -620,18 +622,18 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem): ...@@ -620,18 +622,18 @@ class MessageCatalog(LanguageManager, ObjectManager, SimpleItem):
messages = self._messages messages = self._messages
# Load the data # Load the data
po = itools.gettext.POFile(string=data) po = polib.pofile(data)
for msgid in po.get_msgids(): encoding = to_str(po.encoding)
# TODO Keep the context if any for entry in po:
_context, msgid = msgid msgid = to_unicode(entry.msgid, encoding=encoding)
if msgid: if msgid:
msgstr = po.get_msgstr(msgid) or '' msgstr = to_unicode(entry.msgstr or '', encoding=encoding)
if not messages.has_key(msgid): if not messages.has_key(msgid):
messages[msgid] = PersistentMapping() messages[msgid] = PersistentMapping()
messages[msgid][lang] = msgstr messages[msgid][lang] = msgstr
# Set the encoding (the full header should be loaded XXX) # Set the encoding (the full header should be loaded XXX)
self.update_po_header(lang, charset=po.get_encoding()) self.update_po_header(lang, charset=encoding)
security.declareProtected('Manage messages', 'manage_import') security.declareProtected('Manage messages', 'manage_import')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment