Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    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"))
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
Archivo: tests.py Proyecto: H0bby/polib
    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"))
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
Archivo: tests.py Proyecto: H0bby/polib
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
Archivo: tests.py Proyecto: H0bby/polib
    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)
Ejemplo n.º 10
0
    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))
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
Archivo: tests.py Proyecto: xrile/fjord
    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)
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
Archivo: tests.py Proyecto: H0bby/polib
 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'))
Ejemplo n.º 17
0
Archivo: tests.py Proyecto: H0bby/polib
 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'))
Ejemplo n.º 18
0
Archivo: tests.py Proyecto: H0bby/polib
 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'))
Ejemplo n.º 19
0
Archivo: tests.py Proyecto: H0bby/polib
 def test_find1(self):
     pofile = polib.pofile('tests/test_pofile_helpers.po')
     entry = pofile.find('and')
     self.assertEqual(entry.msgstr, u('y'))
Ejemplo n.º 20
0
 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'))
Ejemplo n.º 21
0
 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'))
Ejemplo n.º 22
0
 def test_find1(self):
     pofile = polib.pofile('tests/test_pofile_helpers.po')
     entry = pofile.find('and')
     self.assertEqual(entry.msgstr, u('y'))