def test_ugettext_lazy(self): """ugettext_lazy should return translated strings, if they are in the locale""" self.setup_l10n_model() poentry = polib.POEntry(msgid='test string', msgstr='LOL DID YOU REALLY THINK I WAS NUB?') self.setup_mofile_with_entry(poentry) lol = translation.ugettext_lazy("test string") assert (unicode(lol) == u'LOL DID YOU REALLY THINK I WAS NUB?')
def test_nulled_translations_get_translated_from_po_file(self): """If a property which is a translation is accessed but it is None, the translation from the po file should be used""" i = self.setup_l10n_model() assert (i.title == i.title_en == u'Ik ben de groot moeftie van cambodja') poentry = polib.POEntry(msgid=u'Ik ben de groot moeftie van cambodja', msgstr=u'Ik ben een zwerver') self.setup_mofile_with_entry(poentry, 'de') translation.activate('de') assert (i.title_de is None) assert (i.title == u'Ik ben een zwerver')
def test_translations_that_are_not_null_should_not_come_from_the_po_file( self): """If a translation is not null, that value should be returned.""" i = self.setup_l10n_model() i.title_de = u'Ik ben een konijntje' i.save() assert (i.title == i.title_en == u'Ik ben de groot moeftie van cambodja') poentry = polib.POEntry(msgid=u'Ik ben de groot moeftie van cambodja', msgstr=u'Ik ben een zwerver') self.setup_mofile_with_entry(poentry, 'de') translation.activate('de') assert (i.title_de is not None) assert (not (i.title == u'Ik ben een zwerver')) assert (i.title == u'Ik ben een konijntje')
def test_translated_fields_handle_correctly_under_to_xml(self): """A field that is translated should show the correct value when converted to xml""" i = self.setup_l10n_model() poentry = polib.POEntry(msgid=u'Ik ben de groot moeftie van cambodja', msgstr=u'Ik ben een zwerver') self.setup_mofile_with_entry(poentry, 'de') xml_representation = tree.xml(i) # todo: # DefaultFieldDescriptor is now serialized as a charfield type. # add the different serializable types to the xslt and the test should be able # to verify the correct workings then. # contains_default_field_descriptor = re.search(r'DefaultFieldDescriptor', xml_representation) # self.assertTrue(contains_default_field_descriptor) contains_moeftie = re.search(r'Ik ben de groot moeftie van cambodja', xml_representation) contains_konijntje = contains_konijntje = re.search( r'Ik ben een konijntje', xml_representation) self.assertTrue(contains_moeftie) self.assertFalse(contains_konijntje) translation.activate('de') i.title = u'Ik ben een duits konijntje' xml_representation = tree.xml(i) contains_konijntje = re.search(r'Ik ben een duits konijntje', xml_representation) self.assertTrue(contains_konijntje) i.title_de = u'Ik ben geen konijntje' i.save() xml_representation = tree.xml(i) contains_konijntje = re.search(r'Ik ben geen konijntje', xml_representation) self.assertTrue(contains_konijntje)
def poify(self, model): """turn a django model into a po file.""" if not hasattr(model, 'localized_fields'): return None # create po stream with header po_stream = polibext.PoStream(StringIO.StringIO(self.po_header)).parse() for (name, field) in easymode.tree.introspection.get_default_field_descriptors(model): occurrence = u"%s.%s.%s" % (model._meta.app_label, model.__class__.__name__, name) value = field.value_to_string(model) # only add empty strings if value != "": entry = polib.POEntry(msgid=value, occurrences=[(occurrence, model.pk)]) # make sure no duplicate entries in the po_stream existing_entry = po_stream.find(entry.msgid) if existing_entry is None: po_stream.append(entry) else: # no really, existing_entry.merge does not merge the occurrences. existing_entry.occurrences += entry.occurrences return po_stream