Python polib.POEntry() Examples

The following are 16 code examples of polib.POEntry(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module polib , or try the search function .
Example #1
Source File: dummy.py    From i18n-tools with Apache License 2.0 7 votes vote down vote up
def convert_msg(self, msg):
        """
        Takes one POEntry object and converts it (adds a dummy translation to it)
        msg is an instance of polib.POEntry
        """
        source = msg.msgid
        if not source:
            # don't translate empty string
            return

        plural = msg.msgid_plural
        if plural:
            # translate singular and plural
            foreign_single = self.convert(source)
            foreign_plural = self.convert(plural)
            plural = {
                '0': self.final_newline(source, foreign_single),
                '1': self.final_newline(plural, foreign_plural),
            }
            msg.msgstr_plural = plural
        else:
            foreign = self.convert(source)
            msg.msgstr = self.final_newline(source, foreign) 
Example #2
Source File: test_translated.py    From libbytesize with GNU Lesser General Public License v2.1 6 votes vote down vote up
def pofile_from_entry(*args, **kwargs):
    poobj = polib.POFile()
    poobj.metadata["Content-Type"] = "text/plain; charset=UTF-8"
    poobj.append(polib.POEntry(*args, **kwargs))
    return pofile(poobj) 
Example #3
Source File: __init__.py    From libbytesize with GNU Lesser General Public License v2.1 6 votes vote down vote up
def testString(poentry):
    """Run all tests against the given translatable string.

       :param polib.POEntry poentry: The PO file entry to test
       :returns: whether the tests succeeded or not
       :rtype: bool
    """
    success = True
    for test in _tests:
        try:
            test(poentry)
        except Exception as e: # pylint: disable=broad-except
            success = False
            print("%s failed on %s: %s" % (test.__name__, poentry.msgid, str(e)))

    return success 
Example #4
Source File: translate.py    From odoo13-x64 with GNU General Public License v3.0 6 votes vote down vote up
def add_entry(self, modules, tnrs, source, trad, comments=None):
        entry = polib.POEntry(
            msgid=source,
            msgstr=trad,
        )
        plural = len(modules) > 1 and 's' or ''
        entry.comment = "module%s: %s" % (plural, ', '.join(modules))
        if comments:
            entry.comment += "\n" + "\n".join(comments)

        code = False
        for typy, name, res_id in tnrs:
            if typy == 'code':
                code = True
                res_id = 0
            if isinstance(res_id, int) or res_id.isdigit():
                # second term of occurrence must be a digit
                # occurrence line at 0 are discarded when rendered to string
                entry.occurrences.append((u"%s:%s" % (typy, name), str(res_id)))
            else:
                entry.occurrences.append((u"%s:%s:%s" % (typy, name, res_id), ''))
        if code:
            entry.flags.append("python-format")
        self.po.append(entry) 
Example #5
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_ok(self):
        # no markup
        test_markup(POEntry(msgid="test string"))

        # internal markup
        test_markup(POEntry(msgid="<b>test</b> string")) 
Example #6
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_unnecessary_markup(self):
        self.assertRaises(AssertionError, test_markup, POEntry(msgid="<b>test string</b>")) 
Example #7
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_ok(self):
        # Perfectly fine string
        test_comment(POEntry(msgid="Hello, I am a test string"))

        # single-character string with a comment
        test_comment(POEntry(msgid="c", comment="TRANSLATORS: 'c' to continue")) 
Example #8
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_no_comment(self):
        self.assertRaises(AssertionError, test_comment, POEntry(msgid="c"))

# Test the top-level functions
# fake tests for testing with 
Example #9
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_failure(self):
        self.assertFalse(testString(POEntry())) 
Example #10
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_success(self):
        with tempfile.NamedTemporaryFile(suffix=".pot") as potfile:
            poobj = POFile()
            poobj.append(POEntry(msgstr="test string"))
            poobj.save(potfile.name)

            self.assertTrue(testPOT(potfile.name)) 
Example #11
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_some_failure(self):
        with tempfile.NamedTemporaryFile(suffix=".pot") as potfile:
            poobj = POFile()
            poobj.append(POEntry(msgstr="test string"))
            poobj.append(POEntry(msgstr="pest string"))
            poobj.save(potfile.name)

            self.assertFalse(testPOT(potfile.name)) 
Example #12
Source File: test_translatable.py    From libbytesize with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_all_failure(self):
        with tempfile.NamedTemporaryFile(suffix=".pot") as potfile:
            poobj = POFile()
            poobj.append(POEntry(msgstr="pest string"))
            poobj.append(POEntry(msgstr="past string"))
            poobj.save(potfile.name)

            self.assertFalse(testPOT(potfile.name)) 
Example #13
Source File: translate_messages.py    From django-autotranslate with MIT License 5 votes vote down vote up
def update_translations(self, entries, translated_strings):
        """Update translations in entries.

        The order and number of translations should match to get_strings_to_translate() result.

        :param entries: list of entries to translate
        :type entries: collections.Iterable[polib.POEntry] | polib.POFile
        :param translated_strings: list of translations
        :type translated_strings: collections.Iterable[six.text_type]
        """
        translations = iter(translated_strings)
        for entry in entries:
            if not self.need_translate(entry):
                continue

            if entry.msgid_plural:
                # fill the first plural form with the entry.msgid translation
                translation = next(translations)
                translation = fix_translation(entry.msgid, translation)
                entry.msgstr_plural[0] = translation

                # fill the rest of plural forms with the entry.msgid_plural translation
                translation = next(translations)
                translation = fix_translation(entry.msgid_plural, translation)
                for k, v in entry.msgstr_plural.items():
                    if k != 0:
                        entry.msgstr_plural[k] = translation
            else:
                translation = next(translations)
                translation = fix_translation(entry.msgid, translation)
                entry.msgstr = translation

            # Set the 'fuzzy' flag on translation
            if self.set_fuzzy and 'fuzzy' not in entry.flags:
                entry.flags.append('fuzzy') 
Example #14
Source File: test_extract.py    From i18n-tools with Apache License 2.0 5 votes vote down vote up
def test_is_keystring(self):
        """
        Verifies is_keystring predicate
        """
        entry1 = polib.POEntry()
        entry2 = polib.POEntry()
        entry1.msgid = "_.lms.admin.warning.keystring"
        entry2.msgid = "This is not a keystring"
        self.assertTrue(extract.is_key_string(entry1.msgid))
        self.assertFalse(extract.is_key_string(entry2.msgid)) 
Example #15
Source File: test_dummy.py    From i18n-tools with Apache License 2.0 5 votes vote down vote up
def test_singular(self):
        entry = POEntry()
        entry.msgid = "A lovely day for a cup of tea."
        expected = u"À lövélý däý för ä çüp öf téä. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢т#"
        self.converter.convert_msg(entry)
        self.assertUnicodeEquals(entry.msgstr, expected) 
Example #16
Source File: test_dummy.py    From i18n-tools with Apache License 2.0 5 votes vote down vote up
def test_plural(self):
        entry = POEntry()
        entry.msgid = "A lovely day for a cup of tea."
        entry.msgid_plural = "A lovely day for some cups of tea."
        expected_s = u"À lövélý däý för ä çüp öf téä. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢т#"
        expected_p = u"À lövélý däý för sömé çüps öf téä. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тєт#"
        self.converter.convert_msg(entry)
        result = entry.msgstr_plural
        self.assertUnicodeEquals(result['0'], expected_s)
        self.assertUnicodeEquals(result['1'], expected_p)