def reconstruct_source_file(self): ret, headers = "", self.header.split("\n") for header in headers: if header[:1] in [",", ":"]: ret += "#%s\n" % header else: ret += "# %s\n" % header if not isinstance(ret, polib.text_type): ret = ret.decode(self.encoding) ret = [ret] entries = [self.metadata_as_entry()] + [e for e in self if not e.obsolete] for entry in entries: if not entry.translated(): ret.append(entry.__unicode__(self.wrapwidth)) else: ret.append(unicode_with_source_msgstr(entry, self.wrapwidth)) for entry in self.obsolete_entries(): if not entry.translated(): ret.append(entry.__unicode__(self.wrapwidth)) else: ret.append(unicode_with_source_msgstr(entry, self.wrapwidth)) ret = polib.u("\n").join(ret) return ret
def test_pofile_and_mofile1(self): """ Test bad usage of pofile/mofile. """ data = u('''# test for pofile/mofile with string buffer msgid "" msgstr "" "Project-Id-Version: django\n" msgid "foo" msgstr "bar" ''') po = polib.pofile(data) self.assertTrue(isinstance(po, polib.POFile)) self.assertEqual(po.encoding, 'utf-8') self.assertEqual(po[0].msgstr, u("bar"))
def source_text(entry): if not entry.tcomment: return None source_text = None for line in entry.tcomment.splitlines(): if line.strip() == polib.u(";;"): break if source_text is not None: source_text.append(line) if line.strip() == polib.u(";; Source text ;;"): source_text = [] if source_text and source_text[0].startswith("msgid"): source_text = None return source_text
def test_ordered_metadata(self): pofile = polib.pofile('tests/test_fuzzy_header.po') f = open('tests/test_fuzzy_header.po') lines = f.readlines()[2:] f.close() mdata = [('Project-Id-Version', u('PACKAGE VERSION')), ('Report-Msgid-Bugs-To', u('')), ('POT-Creation-Date', u('2010-02-08 16:57+0100')), ('PO-Revision-Date', u('YEAR-MO-DA HO:MI+ZONE')), ('Last-Translator', u('FULL NAME <EMAIL@ADDRESS>')), ('Language-Team', u('LANGUAGE <*****@*****.**>')), ('MIME-Version', u('1.0')), ('Content-Type', u('text/plain; charset=UTF-8')), ('Content-Transfer-Encoding', u('8bit'))] self.assertEqual(pofile.ordered_metadata(), mdata)
def test_ordered_metadata(self): pofile = polib.pofile('tests/test_fuzzy_header.po') f = open('tests/test_fuzzy_header.po') lines = f.readlines()[2:] f.close() mdata = [ ('Project-Id-Version', u('PACKAGE VERSION')), ('Report-Msgid-Bugs-To', u('')), ('POT-Creation-Date', u('2010-02-08 16:57+0100')), ('PO-Revision-Date', u('YEAR-MO-DA HO:MI+ZONE')), ('Last-Translator', u('FULL NAME <EMAIL@ADDRESS>')), ('Language-Team', u('LANGUAGE <*****@*****.**>')), ('MIME-Version', u('1.0')), ('Content-Type', u('text/plain; charset=UTF-8')), ('Content-Transfer-Encoding', u('8bit')) ] self.assertEqual(pofile.ordered_metadata(), mdata)
def test_no_header(self): mo = polib.mofile('tests/test_no_header.mo') expected = u('''msgid "" msgstr "" msgid "bar" msgstr "rab" msgid "foo" msgstr "oof" ''') self.assertEqual(mo.__unicode__(), expected)
def test_sort(self): a1 = polib.POEntry(msgid='a1', occurrences=[('b.py', 1), ('b.py', 3)]) a2 = polib.POEntry(msgid='a2') a3 = polib.POEntry(msgid='a1', occurrences=[('b.py', 1), ('b.py', 3)], obsolete=True) b1 = polib.POEntry(msgid='b1', occurrences=[('b.py', 1), ('b.py', 3)]) b2 = polib.POEntry(msgid='b2', occurrences=[('d.py', 3), ('b.py', 1)]) c1 = polib.POEntry(msgid='c1', occurrences=[('a.py', 1), ('b.py', 1)]) c2 = polib.POEntry(msgid='c2', occurrences=[('a.py', 1), ('a.py', 3)]) pofile = polib.POFile() pofile.append(b1) pofile.append(a3) pofile.append(a2) pofile.append(a1) pofile.append(b2) pofile.append(c1) pofile.append(c2) pofile.sort() expected = u('''# msgid "" msgstr "" msgid "a2" msgstr "" #: a.py:1 a.py:3 msgid "c2" msgstr "" #: a.py:1 b.py:1 msgid "c1" msgstr "" #: b.py:1 b.py:3 msgid "a1" msgstr "" #: b.py:1 b.py:3 msgid "b1" msgstr "" #: d.py:3 b.py:1 msgid "b2" msgstr "" #~ msgid "a1" #~ msgstr "" ''') self.assertEqual(pofile.__unicode__(), expected)
def test_ufeff_data_pofile(self): """ Test that an ufeff prefixed pofile returns a POFile instance. """ data = u('''\ufeff# test for pofile/mofile with ufeff msgid "" msgstr "" "Project-Id-Version: django\n" msgid "foo" msgstr "bar" ''') po = polib.pofile(data) self.assertTrue(isinstance(po, polib.POFile))
def test_msgctxt(self): #import pdb; pdb.set_trace() mo = polib.mofile('tests/test_msgctxt.mo') expected = u('''msgid "" msgstr "Content-Type: text/plain; charset=UTF-8\u005cn" msgctxt "Some message context" msgid "some string" msgstr "une cha\u00eene" msgctxt "Some other message context" msgid "singular" msgid_plural "plural" msgstr[0] "singulier" msgstr[1] "pluriel" ''') self.assertEqual(mo.__unicode__(), expected)
def __unicode__(self, wrapwidth=78): """ Returns the unicode representation of the entry. """ ret = [] val = self.dpo_comment prefix = "# " if val: ret.append("# ;; Source text ;;") for comment in val.split("\n"): if wrapwidth > 0 and len(comment) + len(prefix) > wrapwidth: ret += polib.wrap( comment, wrapwidth, initial_indent=prefix, subsequent_indent=prefix, break_long_words=False ) else: ret.append("%s%s" % (prefix, comment)) ret.append("# ;;") ret.append(polib.POEntry.__unicode__(self)) ret = polib.u("\n").join(ret) return ret
def unicode_with_source_msgstr(self, wrapwidth=78): """ Returns the unicode representation of the entry. """ if self.obsolete: delflag = "#~ " else: delflag = "" ret = [] # write the msgctxt if any if self.msgctxt is not None: ret += self._str_field("msgctxt", delflag, "", self.msgctxt, wrapwidth) # write the msgid ret += self._str_field("msgid", delflag, "", self.msgid, wrapwidth) # write the msgid_plural if any if self.msgid_plural: ret += self._str_field("msgid_plural", delflag, "", self.msgid_plural, wrapwidth) source = source_text(self) if source is not None: ret += source else: if self.msgstr_plural: # write blank msgstr_plurals if any msgstrs = self.msgstr_plural keys = list(msgstrs) keys.sort() for index in keys: msgstr = msgstrs[index] plural_index = "[%s]" % index ret += self._str_field("msgstr", delflag, plural_index, "", wrapwidth) else: # otherwise write a blank msgstr ret += self._str_field("msgstr", delflag, "", "", wrapwidth) ret.append("") ret = polib.u("\n").join(ret) return ret
def test_find5(self): pofile = polib.pofile('tests/test_msgctxt.po') entry1 = pofile.find('some string') entry2 = pofile.find('some string', msgctxt='Some message context') self.assertEqual(entry1.msgstr, u('une cha\u00eene sans contexte')) self.assertEqual(entry2.msgstr, u('une cha\u00eene avec contexte'))
def test_find4(self): pofile = polib.pofile('tests/test_utf8.po') entry1 = pofile.find('test context', msgctxt='@context1') entry2 = pofile.find('test context', msgctxt='@context2') self.assertEqual(entry1.msgstr, u('test context 1')) self.assertEqual(entry2.msgstr, u('test context 2'))
def test_find3(self): pofile = polib.pofile('tests/test_pofile_helpers.po') entry = pofile.find('package', include_obsolete_entries=True) self.assertEqual(entry.msgstr, u('pacote'))
def test_find1(self): pofile = polib.pofile('tests/test_pofile_helpers.po') entry = pofile.find('and') self.assertEqual(entry.msgstr, u('y'))