class GettextPOImporterTestCase(unittest.TestCase): """Class test for gettext's .po file imports""" layer = LaunchpadZopelessLayer def setUp(self): # Add a new entry for testing purposes. It's a template one. self.translation_import_queue = getUtility(ITranslationImportQueue) template_path = 'po/testing.pot' by_maintainer = True personset = getUtility(IPersonSet) importer = personset.getByName('carlos') productset = getUtility(IProductSet) firefox = productset.getByName('firefox') productseries = firefox.getSeries('trunk') template_entry = self.translation_import_queue.addOrUpdateEntry( template_path, test_template, by_maintainer, importer, productseries=productseries) # Add another one, a translation file. pofile_path = 'po/es.po' translation_entry = self.translation_import_queue.addOrUpdateEntry( pofile_path, test_translation_file, by_maintainer, importer, productseries=productseries) transaction.commit() self.template_importer = GettextPOImporter() self.template_file = self.template_importer.parse(template_entry) self.translation_importer = GettextPOImporter() self.translation_file = self.translation_importer.parse( translation_entry) def testInterface(self): """Check whether the object follows the interface.""" self.failUnless( verifyObject(ITranslationFormatImporter, self.template_importer), "GettextPOImporter doesn't conform to ITranslationFormatImporter" "interface.") def testFormat(self): # GettextPOImporter reports that it handles the PO file format. format = self.template_importer.getFormat(test_template) self.failUnless( format == TranslationFileFormat.PO, 'GettextPOImporter format expected PO but got %s' % format.name)
def parse(self, translation_import_queue_entry): """See `ITranslationFormatImporter`.""" translation_file = GettextPOImporter.parse( self, translation_import_queue_entry) plural_prefix = u'_n: ' context_prefix = u'_: ' for message in translation_file.messages: msgid = message.msgid_singular if msgid.startswith(plural_prefix) and '\n' in msgid: # This is a KDE plural form singular, plural = msgid[len(plural_prefix):].split('\n') message.msgid_singular = singular message.msgid_plural = plural msgstrs = message._translations if len(msgstrs) > 0: message._translations = msgstrs[0].split('\n') self.internal_format = TranslationFileFormat.KDEPO elif msgid.startswith(context_prefix) and '\n' in msgid: # This is a KDE context message message.context, message.msgid_singular = ( msgid[len(context_prefix):].split('\n', 1)) self.internal_format = TranslationFileFormat.KDEPO else: # Other messages are left as they are parsed by # GettextPOImporter pass return translation_file
def parse(self, translation_import_queue_entry): """See `ITranslationFormatImporter`.""" translation_file = GettextPOImporter.parse( self, translation_import_queue_entry) plural_prefix = u'_n: ' context_prefix = u'_: ' for message in translation_file.messages: msgid = message.msgid_singular if msgid.startswith(plural_prefix) and '\n' in msgid: # This is a KDE plural form singular, plural = msgid[len(plural_prefix):].split('\n') message.msgid_singular = singular message.msgid_plural = plural msgstrs = message._translations if len(msgstrs) > 0: message._translations = msgstrs[0].split('\n') self.internal_format = TranslationFileFormat.KDEPO elif msgid.startswith(context_prefix) and '\n' in msgid: # This is a KDE context message message.context, message.msgid_singular = ( msgid[len(context_prefix):].split('\n', 1)) self.internal_format = TranslationFileFormat.KDEPO else: # Other messages are left as they are parsed by # GettextPOImporter pass return translation_file
class GettextPOImporterTestCase(unittest.TestCase): """Class test for gettext's .po file imports""" layer = LaunchpadZopelessLayer def setUp(self): # Add a new entry for testing purposes. It's a template one. self.translation_import_queue = getUtility(ITranslationImportQueue) template_path = 'po/testing.pot' by_maintainer = True personset = getUtility(IPersonSet) importer = personset.getByName('carlos') productset = getUtility(IProductSet) firefox = productset.getByName('firefox') productseries = firefox.getSeries('trunk') template_entry = self.translation_import_queue.addOrUpdateEntry( template_path, test_template, by_maintainer, importer, productseries=productseries) # Add another one, a translation file. pofile_path = 'po/es.po' translation_entry = self.translation_import_queue.addOrUpdateEntry( pofile_path, test_translation_file, by_maintainer, importer, productseries=productseries) transaction.commit() self.template_importer = GettextPOImporter() self.template_file = self.template_importer.parse(template_entry) self.translation_importer = GettextPOImporter() self.translation_file = self.translation_importer.parse( translation_entry) def testInterface(self): """Check whether the object follows the interface.""" self.failUnless( verifyObject(ITranslationFormatImporter, self.template_importer), "GettextPOImporter doesn't conform to ITranslationFormatImporter" "interface.") def testFormat(self): # GettextPOImporter reports that it handles the PO file format. format = self.template_importer.getFormat(test_template) self.failUnless( format == TranslationFileFormat.PO, 'GettextPOImporter format expected PO but got %s' % format.name)