Example #1
0
def test_tp_tool_update(po_directory, tp0, templates):
    new_lang = LanguageDBFactory()
    tp0_tool = TPTool(tp0.project)
    new_tp = tp0.project.translationproject_set.create(
        language=new_lang)

    # this will clone stores/directories as new_tp is empty
    tp0_tool.update_from_tp(tp0, new_tp)
    _test_tp_match(tp0, new_tp)
    tp0_tool.update_from_tp(tp0, new_tp)

    tp0.stores.first().delete()
    tp0.stores.first().units.first().delete()
    unit = tp0.stores.first().units.first()
    unit.source = multistring(["NEW TARGET", "NEW TARGETS"])
    unit.target = "NEW TARGET"
    unit.context = "something-else"
    unit.save()
    newunit = unit.__class__()
    newunit.source = multistring(["OTHER NEW TARGET", "OTHER NEW TARGETS"])
    newunit.target = "OTHER NEW TARGET"
    newunit.context = "something-else-again"
    unit.store.addunit(newunit)

    tp0_tool.update_from_tp(tp0, new_tp)
    _test_tp_match(tp0, new_tp)

    # doing another update does nothing
    tp0_tool.update_from_tp(tp0, new_tp)
    _test_tp_match(tp0, new_tp)
Example #2
0
 def gettarget(self):
     """Returns the unescaped msgstr"""
     if isinstance(self.msgstr, dict):
         multi = multistring(map(unquotefrompo, self.msgstr.values()), self._encoding)
     else:
         multi = multistring(unquotefrompo(self.msgstr), self._encoding)
     return multi
Example #3
0
    def settarget(self, text, lang='xx', append=False):
        self._rich_target = None
        if self.target == text:
            return
        if not self.hasplural():
            super(PoXliffUnit, self).settarget(text, lang, append)
            return
        if not isinstance(text, multistring):
            text = multistring(text)
        source = self.source
        sourcel = len(source.strings)
        targetl = len(text.strings)
        if sourcel < targetl:
            sources = source.strings + [source.strings[-1]] * (targetl - sourcel)
            targets = text.strings
            id = self.getid()
            self.source = multistring(sources)
            self.setid(id)
        elif targetl < sourcel:
            targets = text.strings + [""] * (sourcel - targetl)
        else:
            targets = text.strings

        for i in range(len(self.units)):
            self.units[i].target = targets[i]
Example #4
0
File: utils.py Project: arky/pootle
def test_unit_lifecycle_update_state_reviewed_by(store0, system, member2):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.target_f = multistring("Bar")
    unit.state = FUZZY
    unit.save(user=system)
    # force the unit to be refrozen
    unit = unit.__class__.objects.get(id=unit.id)
    unit.state = TRANSLATED
    unit.save(reviewed_by=member2)
    sub_state_update = lifecycle.get(Unit)(unit).sub_state_update()
    assert isinstance(sub_state_update, Submission)
    assert sub_state_update.unit == unit
    assert sub_state_update.translation_project == store0.translation_project
    assert sub_state_update.revision == unit.revision
    assert (
        sub_state_update.submitter
        == unit.change.reviewed_by
        == member2)
    assert sub_state_update.type == SubmissionTypes.SYSTEM
    assert sub_state_update.field == SubmissionFields.STATE
    assert sub_state_update.new_value == unit.state
    assert sub_state_update.old_value == unit._frozen.state
    assert not sub_state_update.pk
Example #5
0
    def test_plurals(self):
        store = self.StoreClass()
        store.parse(JSON_I18NEXT)

        # Remove plurals
        store.units[2].target = 'Ahoj'
        store.units[3].target = 'Nazdar'
        out = BytesIO()
        store.serialize(out)

        assert out.getvalue() == JSON_I18NEXT_PLURAL

        # Bring back plurals
        store.units[2].target = multistring([
            "the singular",
            "the plural",
        ])
        store.units[3].target = multistring([
            "the plural form 0",
            "the plural form 1",
            "the plural form 2",
            "the plural form 3",
            "the plural form 4",
            "the plural form 5"
        ])
        out = BytesIO()
        store.serialize(out)

        assert out.getvalue() == JSON_I18NEXT
Example #6
0
 def to_python(self, value):
     if not value:
         return multistring("", encoding="UTF-8")
     elif isinstance(value, multistring):
         return value
     elif isinstance(value, basestring):
         return multistring(value.split(SEPERATOR), encoding="UTF-8")
     elif isinstance(value, dict):
         return multistring([val for key, val in sorted(value.items())], encoding="UTF-8")
     else:
         return multistring(value, encoding="UTF-8")
Example #7
0
    def to_pypo(unit):
        pounit = pypo.pounit(
            source=multistring(unit.msgid) if unit.hasplural() else (unit.msgid[0] if len(unit.msgid) else "")
        )
        pounit.target = multistring(unit.msgstr) if unit.hasplural() else unit.msgstr[0]

        if unit.comments:
            ps = poparser.ParseState(cStringIO.StringIO(unit.comments.encode("utf8")), pypo.pounit, encoding="utf-8")
            poparser.parse_comments(ps, pounit)

        return pounit
Example #8
0
    def test_prevmsgid_parse(self):
        """checks that prevmsgid (i.e. #|) is parsed and saved correctly"""
        posource = r'''msgid ""
msgstr ""
"PO-Revision-Date: 2006-02-09 23:33+0200\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8-bit\n"

#, fuzzy
#| msgid "trea"
msgid "tree"
msgstr "boom"

#| msgid "trea"
#| msgid_plural "treas"
msgid "tree"
msgid_plural "trees"
msgstr[0] "boom"
msgstr[1] "bome"

#| msgctxt "context 1"
#| msgid "tast"
msgctxt "context 1a"
msgid "test"
msgstr "toets"

#| msgctxt "context 2"
#| msgid "tast"
#| msgid_plural "tasts"
msgctxt "context 2a"
msgid "test"
msgid_plural "tests"
msgstr[0] "toet"
msgstr[1] "toetse"
'''

        pofile = self.poparse(posource)

        assert pofile.units[1].prev_msgctxt == []
        assert pofile.units[1].prev_source == multistring([u"trea"])

        assert pofile.units[2].prev_msgctxt == []
        assert pofile.units[2].prev_source == multistring([u"trea", u"treas"])

        assert pofile.units[3].prev_msgctxt == [u'"context 1"']
        assert pofile.units[3].prev_source == multistring([u"tast"])

        assert pofile.units[4].prev_msgctxt == [u'"context 2"']
        assert pofile.units[4].prev_source == multistring([u"tast", u"tasts"])

        assert str(pofile) == posource
Example #9
0
def to_python(value):
    """Reconstruct a multistring from the database string representation."""
    if not value:
        return multistring("", encoding="UTF-8")
    elif isinstance(value, multistring):
        return value
    elif isinstance(value, basestring):
        return parse_multistring(value)
    elif isinstance(value, dict):
        return multistring([val for __, val in sorted(value.items())],
                           encoding="UTF-8")
    else:
        return multistring(value, encoding="UTF-8")
Example #10
0
 def parse(self, input):
     """parses the given file or file source string"""
     if hasattr(input, 'name'):
         self.filename = input.name
     elif not getattr(self, 'filename', ''):
         self.filename = ''
     if hasattr(input, "read"):
         mosrc = input.read()
         input.close()
         input = mosrc
     little, = struct.unpack("<L", input[:4])
     big, = struct.unpack(">L", input[:4])
     if little == MO_MAGIC_NUMBER:
         endian = "<"
     elif big == MO_MAGIC_NUMBER:
         endian = ">"
     else:
         raise ValueError("This is not an MO file")
     magic, version_maj, version_min, lenkeys, startkey, \
         startvalue, sizehash, offsethash = struct.unpack("%sLHHiiiii" % endian,
                                                          input[:(7 * 4)])
     if version_maj >= 1:
         raise base.ParseError("""Unable to process version %d.%d MO files""" % (version_maj, version_min))
     for i in range(lenkeys):
         nextkey = startkey + (i * 2 * 4)
         nextvalue = startvalue + (i * 2 * 4)
         klength, koffset = struct.unpack("%sii" % endian,
                                          input[nextkey:nextkey + (2 * 4)])
         vlength, voffset = struct.unpack("%sii" % endian,
                                          input[nextvalue:nextvalue + (2 * 4)])
         source = input[koffset:koffset + klength]
         context = None
         if b"\x04" in source:
             context, source = source.split(b"\x04")
         # Still need to handle KDE comments
         if source == "":
             charset = re.search(b"charset=([^\\s]+)",
                                 input[voffset:voffset + vlength])
             if charset:
                 self.encoding = charset.group(1)
         source = multistring([s.decode(self.encoding)
                               for s in source.split(b"\0")])
         target = multistring([s.decode(self.encoding)
                               for s in input[voffset:voffset + vlength].split(b"\0")])
         newunit = mounit(source)
         newunit.target = target
         if context is not None:
             newunit.msgctxt.append(context)
         self.addunit(newunit)
Example #11
0
 def parse(self, input):
     """parses the given file or file source string"""
     if hasattr(input, 'name'):
         self.filename = input.name
     elif not getattr(self, 'filename', ''):
         self.filename = ''
     if hasattr(input, "read"):
         mosrc = input.read()
         input.close()
         input = mosrc
     little, = struct.unpack("<L", input[:4])
     big, = struct.unpack(">L", input[:4])
     if little == MO_MAGIC_NUMBER:
         endian = "<"
     elif big == MO_MAGIC_NUMBER:
         endian = ">"
     else:
         raise ValueError("This is not an MO file")
     magic, version, lenkeys, startkey, \
     startvalue, sizehash, offsethash = struct.unpack("%sLiiiiii" % endian,
                                                      input[:(7 * 4)])
     if version > 1:
         raise ValueError("Unable to process MO files with versions > 1.  \
                          This is a %d version MO file" % version)
     for i in range(lenkeys):
         nextkey = startkey + (i * 2 * 4)
         nextvalue = startvalue + (i * 2 * 4)
         klength, koffset = struct.unpack("%sii" % endian,
                                          input[nextkey:nextkey + (2 * 4)])
         vlength, voffset = struct.unpack("%sii" % endian,
                                          input[nextvalue:nextvalue + (2 * 4)])
         source = input[koffset:koffset + klength]
         context = None
         if "\x04" in source:
             context, source = source.split("\x04")
         # Still need to handle KDE comments
         source = multistring(source.split("\0"), encoding=self._encoding)
         if source == "":
             charset = re.search("charset=([^\\s]+)",
                                 input[voffset:voffset + vlength])
             if charset:
                 self._encoding = po.encodingToUse(charset.group(1))
         target = multistring(input[voffset:voffset + vlength].split("\0"),
                              encoding=self._encoding)
         newunit = mounit(source)
         newunit.settarget(target)
         if context is not None:
             newunit.msgctxt.append(context)
         self.addunit(newunit)
Example #12
0
 def getsource(self):
     # TODO: support <byte>. See bug 528.
     sourcenode = self._getsourcenode()
     if self.hasplural():
         return multistring([sourcenode.text])
     else:
         return data.forceunicode(sourcenode.text)
Example #13
0
 def getsource(self):
     if not self.hasplural():
         return super(PoXliffUnit, self).getsource()
     else:
         strings = []
         strings.extend([unit.source for unit in self.units])
         return multistring(strings)
Example #14
0
    def addplural(self, source, target, filename, createifmissing=False):
        """This method should now be unnecessary, but is left for reference"""
        assert isinstance(source, multistring)
        if not isinstance(target, multistring):
            target = multistring(target)
        sourcel = len(source.strings)
        targetl = len(target.strings)
        if sourcel < targetl:
            sources = source.strings + [source.strings[-1]] * targetl - sourcel
            targets = target.strings
        else:
            sources = source.strings
            targets = target.strings
        self._messagenum += 1
        pluralnum = 0
        group = self.creategroup(filename, True, restype="x-gettext-plural")
        for (src, tgt) in zip(sources, targets):
            unit = self.UnitClass(src)
            unit.target = tgt
            unit.setid("%d[%d]" % (self._messagenum, pluralnum))
            pluralnum += 1
            group.append(unit.xmlelement)
            self.units.append(unit)

        if pluralnum < sourcel:
            for string in sources[pluralnum:]:
                unit = self.UnitClass(src)
                unit.xmlelement.set("translate", "no")
                unit.setid("%d[%d]" % (self._messagenum, pluralnum))
                pluralnum += 1
                group.append(unit.xmlelement)
                self.units.append(unit)

        return self.units[-pluralnum]
Example #15
0
def test_submit_unit_plural(client, unit_plural, request_users, settings):
    """Tests translation can be applied after suggestion is accepted."""
    settings.POOTLE_CAPTCHA_ENABLED = False
    user = request_users["user"]
    if user.username != "nobody":
        client.login(
            username=user.username,
            password=request_users["password"])

    url = '/xhr/units/%d/' % unit_plural.id
    original_target = unit_plural.target
    target = [
        "%s" % unit_plural.target.strings[0],
        "%s changed" % unit_plural.target.strings[1]
    ]
    response = client.post(
        url,
        {'target_f_0': target[0],
         'target_f_1': target[1],
         'is_fuzzy': "0"},
        HTTP_X_REQUESTED_WITH='XMLHttpRequest')

    unit = Unit.objects.get(id=unit_plural.id)
    if check_permission('translate', response.wsgi_request):
        assert response.status_code == 200
        assert unit.target == multistring(target)
    else:
        assert response.status_code == 403
        assert unit.target == original_target
        with pytest.raises(UnitChange.DoesNotExist):
            unit.change
Example #16
0
File: utils.py Project: arky/pootle
def test_unit_lifecycle_unmute_qc(store0, member):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.save()
    unit_lifecycle = lifecycle.get(Unit)(unit)

    qc = QualityCheck(
        unit=unit,
        name="foo-check",
        message="Check foo!",
        category="Footile")

    with pytest.raises(KeyError):
        unit_lifecycle.sub_unmute_qc()

    with pytest.raises(KeyError):
        unit_lifecycle.sub_unmute_qc(submitter=member)

    with pytest.raises(KeyError):
        unit_lifecycle.sub_unmute_qc(quality_check=qc)

    sub_unmute_qc = unit_lifecycle.sub_unmute_qc(
        quality_check=qc, submitter=member)

    assert sub_unmute_qc.unit == unit
    assert sub_unmute_qc.translation_project == store0.translation_project
    assert sub_unmute_qc.revision == unit.revision
    assert sub_unmute_qc.type == SubmissionTypes.WEB
    assert sub_unmute_qc.field == SubmissionFields.CHECK
    assert sub_unmute_qc.new_value == UNMUTED
    assert sub_unmute_qc.old_value == MUTED
    assert not sub_unmute_qc.pk
Example #17
0
 def test_plural_escape_message_with_newline(self):
     mString = multistring(['one message\nwith newline', 'other message\nwith newline'])
     xml = ('<plurals name="teststring">\n'
            '    <item quantity="one">one message\n\\nwith newline</item>'
            '<item quantity="other">other message\n\\nwith newline</item>'
            '</plurals>\n')
     self.__check_escape(mString, xml, 'en')
Example #18
0
def test_add_suggestion(client, request_users, settings):
    """Tests translation can be applied after suggestion is accepted."""
    settings.POOTLE_CAPTCHA_ENABLED = False
    user = request_users["user"]
    if user.username != "nobody":
        client.login(
            username=user.username,
            password=request_users["password"])

    unit = Unit.objects.all().first()
    url = '/xhr/units/%d/suggestions' % unit.id
    target = "%s TEST SUGGESTION" % unit.source
    response = client.post(
        url,
        {
            'target_f_0': target,
        },
        HTTP_X_REQUESTED_WITH='XMLHttpRequest'
    )

    assert response.status_code == 200
    changed = Unit.objects.get(id=unit.id)
    suggestion = changed.get_suggestions().order_by('id').last()
    assert suggestion.target == multistring(target)
    with pytest.raises(UnitChange.DoesNotExist):
        unit.change
Example #19
0
 def test_plural_parse_message_with_newline(self):
     mString = multistring(['one message\nwith newline', 'other message\nwith newline'])
     xml = ('<plurals name="Test String">\n\t'
            '<item quantity="one">one message\\nwith newline</item>\n\t'
            '<item quantity="other">other message\\nwith newline</item>\n\n'
            '</plurals>\n\n')
     self.__check_parse(mString, xml)
Example #20
0
 def handle_plurals(self, plurals):
     if len(plurals) == 1:
         return self.string_filter(plurals[0])
     else:
         return multistring(
             [self.string_filter(plural) for plural in plurals]
         )
Example #21
0
 def set_target(self, target):
     """Set translation unit target."""
     if 'target' in self.__dict__:
         del self.__dict__['target']
     if isinstance(target, list):
         target = multistring(target)
     self.unit.target = target
Example #22
0
 def gettarget(self):
     if self.hasplural():
         plurals = []
         nplural = 0
         plural = gpo.po_message_msgstr_plural(self._gpo_message, nplural)
         while plural:
             plurals.append(plural.decode(self.CPO_ENC))
             nplural += 1
             plural = gpo.po_message_msgstr_plural(self._gpo_message, nplural)
         if plurals:
             multi = multistring(plurals, encoding=self.CPO_ENC)
         else:
             multi = multistring(u"")
     else:
         multi = (gpo.po_message_msgstr(self._gpo_message) or "").decode(self.CPO_ENC)
     return multi
Example #23
0
 def test_plural_invalid_lang(self):
     mString = multistring(['one message', 'other message'])
     xml = ('<plurals name="Test String">\n\t'
            '<item quantity="one">one message</item>\n\t'
            '<item quantity="other">other message</item>\n'
            '</plurals>\n\n')
     self.__check_escape(mString, xml, 'invalid')
Example #24
0
        def clean_target_f(self):
            value = self.cleaned_data["target_f"]

            if self.instance.target.strings != multistring(value or [u""]):
                self.instance._target_updated = True
                self.updated_fields.append((SubmissionFields.TARGET, to_db(self.instance.target), to_db(value)))

            return value
Example #25
0
def _unit_post_merge_pounit(input_unit, input_store, output_store, template_store):
    """PO format specific plural string initializtion logic."""
    #FIXME: do we want to do that for poxliff also?
    if input_unit.hasplural() and len(input_unit.target) == 0:
        # untranslated plural unit; Let's ensure that we have the correct number of plural forms:
        nplurals, plural = output_store.getheaderplural()
        if nplurals and nplurals.isdigit() and nplurals != '2':
            input_unit.target = multistring([""]*int(nplurals))
Example #26
0
 def set_target(self, target):
     """Set translation unit target."""
     if isinstance(target, list):
         target = multistring(target)
     self.unit.target = target
     # Propagate to value so that is_translated works correctly
     if self.is_unit_key_value(self.unit):
         self.unit.value = self.unit.translation
Example #27
0
 def _get_source_vars(self, msgid, msgid_plural):
     multi = multistring(unquotefrompo(msgid), self._encoding)
     if self.hasplural():
         pluralform = unquotefrompo(msgid_plural)
         if isinstance(pluralform, str):
             pluralform = pluralform.decode(self._encoding)
         multi.strings.append(pluralform)
     return multi
Example #28
0
        def clean_target_f(self):
            value = self.cleaned_data['target_f']

            if self.instance.target.strings != multistring(value or [u'']):
                self.instance._target_updated = True
                self.updated_fields.append(("pootle_store.Unit.target", to_db(self.instance.target), to_db(value)))

            return value
Example #29
0
 def test_plural_reduction(self):
     """checks that reducing the number of plurals supplied works"""
     unit = self.UnitClass("Tree")
     unit.msgid_plural = ["Trees"]
     assert isinstance(unit.source, multistring)
     assert unit.source.strings == ["Tree", "Trees"]
     unit.target = multistring(["Boom", "Bome", "Baie Bome"])
     assert isinstance(unit.source, multistring)
     assert unit.target.strings == ["Boom", "Bome", "Baie Bome"]
     unit.target = multistring(["Boom", "Bome"])
     assert unit.target.strings == ["Boom", "Bome"]
     unit.target = "Boom"
     # FIXME: currently assigning the target to the same as the first string won't change anything
     # we need to verify that this is the desired behaviour...
     assert unit.target.strings[0] == "Boom"
     unit.target = "Een Boom"
     assert unit.target.strings == ["Een Boom"]
Example #30
0
 def gettarget(self, lang=None):
     if (self.xmlelement.tag == "plurals"):
         target = []
         for entry in self.xmlelement.iterchildren():
             target.append(self.get_xml_text_value(entry))
         return multistring(target)
     else:
         return self.get_xml_text_value(self.xmlelement)
Example #31
0
 def test_plural_invalid_lang(self):
     mString = multistring(["one message", "other message"])
     xml = (
         '<plurals name="teststring">\n'
         '    <item quantity="one">one message</item>'
         '<item quantity="other">other message</item>'
         "</plurals>\n"
     )
     self.__check_escape(mString, xml, "invalid")
Example #32
0
    def rich_to_multistring(cls, elem_list):
        """Convert a "rich" string tree to a ``multistring``:

        >>> from translate.storage.placeables.interfaces import X
        >>> rich = [StringElem(['foo', X(id='xxx', sub=[' ']), 'bar'])]
        >>> TranslationUnit.rich_to_multistring(rich)
        multistring('foo bar')
        """
        return multistring([str(elem) for elem in elem_list])
Example #33
0
def test_unit_lifecycle_create(store0):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.index = store0.max_index() + 1
    unit.save()
    source = unit.unit_source.get()
    assert source.created_by == get_system_user()
    assert source.created_with == SubmissionTypes.SYSTEM
Example #34
0
 def gettarget(self):
     if self.hasplural():
         strings = [unit.target for unit in self.units]
         if strings:
             return multistring(strings)
         else:
             return None
     else:
         return super(PoXliffUnit, self).gettarget()
Example #35
0
 def gettarget(self, lang=None):
     if self.hasplural():
         strings = [data.forceunicode(unit.target) for unit in self.units]
         if strings:
             return multistring(strings)
         else:
             return None
     else:
         return super().gettarget(lang)
Example #36
0
        def clean_target_f(self):
            value = self.cleaned_data['target_f']

            if self.instance.target != multistring(value or [u'']):
                self._updated_fields.append(
                    (SubmissionFields.TARGET, to_db(self.instance.target),
                     to_db(value)))

            return value
Example #37
0
    def test_new_plural(self):
        EXPECTED = b"""{
    "simple": "the singular",
    "simple_plural": "the plural",
    "complex_0": "the plural form 0",
    "complex_1": "the plural form 1",
    "complex_2": "the plural form 2",
    "complex_3": "the plural form 3",
    "complex_4": "the plural form 4",
    "complex_5": "the plural form 5"
}
"""
        store = self.StoreClass()

        unit = self.StoreClass.UnitClass(
            multistring(
                [
                    "the singular",
                    "the plural",
                ]
            ),
            "simple",
        )
        store.addunit(unit)

        unit = self.StoreClass.UnitClass(
            multistring(
                [
                    "the plural form 0",
                    "the plural form 1",
                    "the plural form 2",
                    "the plural form 3",
                    "the plural form 4",
                    "the plural form 5",
                ]
            ),
            "complex",
        )
        store.addunit(unit)

        out = BytesIO()
        store.serialize(out)

        assert out.getvalue() == EXPECTED
Example #38
0
 def gettarget(self):
     targetnode = self._gettargetnode()
     if targetnode is None:
         etree.SubElement(self.xmlelement, self.namespaced("translation"))
         return None
     if self.hasplural():
         numerus_nodes = targetnode.findall(self.namespaced("numerusform"))
         return multistring([node.text or u"" for node in numerus_nodes])
     else:
         return data.forceunicode(targetnode.text) or u""
Example #39
0
def test_unit_lifecycle_update_state(store0, member):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.target_f = multistring("Bar")
    unit.state = TRANSLATED
    unit.reviewed_by = member
    unit.save(user=member)
    sub_state_update = lifecycle.get(Unit)(unit).sub_state_update()
    assert isinstance(sub_state_update, Submission)
    assert sub_state_update.unit == unit
    assert sub_state_update.translation_project == store0.translation_project
    assert sub_state_update.revision == unit.revision
    assert (sub_state_update.submitter == unit.change.submitted_by == member)
    assert sub_state_update.type == SubmissionTypes.SYSTEM
    assert sub_state_update.field == SubmissionFields.STATE
    assert sub_state_update.new_value == unit.state
    assert sub_state_update.old_value == unit._frozen.state
    assert not sub_state_update.pk
Example #40
0
 def create_unit(self, key: str, source: Union[str, List[str]]):
     if isinstance(source, list):
         unit = self.construct_unit(source[0])
         source = multistring(source)
     else:
         unit = self.construct_unit(source)
     unit.setid(key)
     unit.source = self.create_unit_key(key, source)
     unit.target = source
     return unit
Example #41
0
 def convertstrings(self, input):
     if isinstance(input, multistring):
         strings = input.strings
     elif isinstance(input, list):
         strings = input
     else:
         return self.convertstring(input)
     for index, string in enumerate(strings):
         strings[index] = re.sub(r'%(\d)\$s', lambda x: "{%d}" % (int(x.group(1)) - 1), string)
     return multistring(strings)
Example #42
0
 def add_unit(self, unit):
     if unit.is_plural():
         output = self.storage.UnitClass(
             multistring(unit.get_source_plurals()))
         output.target = multistring(unit.get_target_plurals())
     else:
         output = self.storage.UnitClass(unit.source)
         output.target = unit.target
     output.context = unit.context
     for location in unit.location.split():
         if location:
             output.addlocation(location)
     output.addnote(unit.comment)
     if hasattr(output, 'settypecomment'):
         for flag in unit.flags.split(','):
             output.settypecomment(flag)
     if unit.fuzzy:
         output.markfuzzy(True)
     self.storage.addunit(output)
Example #43
0
 def update_unit_target(self, unit_data, updated_text):
     if unit_data.unit.hasplural():
         plurals = unit_data.unit.gettarget().strings
         if (unit_data.plural < len(plurals)):
             plurals[unit_data.plural] = updated_text
         else:
             plurals.append(updated_text)
         unit_data.unit.settarget(multistring(plurals))
     else:
         unit_data.unit.settarget(updated_text)
Example #44
0
    def _parse_dict(self, data, prev):
        # Does this look like a plural?
        if data and all(x in cldr_plural_categories for x in data.keys()):
            # Ensure we have correct plurals ordering.
            values = [data[item] for item in cldr_plural_categories if item in data]
            yield (prev, multistring(values))
            return

        # Handle normal dict
        yield from super()._parse_dict(data, prev)
Example #45
0
def test_unit_lifecycle_instance(store0):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.save()
    unit_lifecycle = lifecycle.get(Unit)(unit)
    assert isinstance(unit_lifecycle, UnitLifecycle)
    assert unit_lifecycle.original == unit._frozen
    assert unit_lifecycle.unit == unit
    assert unit_lifecycle.submission_model == unit.submission_set.model
Example #46
0
    def test_plurals(self):
        store = self.StoreClass()
        store.parse(JSON_GOI18N)

        assert len(store.units) == 2
        assert store.units[0].target == multistring(
            ["{{.count}} tag", "{{.count}} tags"])
        assert store.units[1].target == "Table"

        assert bytes(store).decode() == JSON_GOI18N.decode()
Example #47
0
def test_unit_lifecycle_create(store0):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.save()
    source = unit.unit_source.get()
    assert source.created_by == get_system_user()
    assert source.created_with == SubmissionTypes.SYSTEM
    with pytest.raises(UnitChange.DoesNotExist):
        unit.change
Example #48
0
 def source(self, source):
     self._rich_source = None
     source = data.forceunicode(source or u"")
     source = source or u""
     if isinstance(source, multistring):
         self._source = source
     elif isinstance(source, six.text_type):
         self._source = source
     else:  # If it is unicode, list or dict.
         self._source = multistring(source)
Example #49
0
def _unit_post_merge_pounit(input_unit, input_store, output_store,
        template_store):
    """PO format specific plural string initializtion logic."""
    #FIXME: do we want to do that for poxliff also?
    if input_unit.hasplural() and len(input_unit.target) == 0:
        # untranslated plural unit; Let's ensure that we have the correct
        # number of plural forms:
        nplurals, plural = output_store.getheaderplural()
        if nplurals and nplurals.isdigit() and nplurals != '2':
            input_unit.target = multistring([""] * int(nplurals))
Example #50
0
def test_unit_lifecycle_update_comment(store0, member):
    unit = store0.UnitClass()
    unit.store = store0
    unit.source_f = multistring("Foo")
    unit.target_f = multistring("Bar")
    unit.translator_comment = "SOME COMMENT"
    unit.save(user=member)
    sub_comment_update = lifecycle.get(Unit)(unit).sub_comment_update()
    assert isinstance(sub_comment_update, Submission)
    assert sub_comment_update.unit == unit
    assert sub_comment_update.translation_project == store0.translation_project
    assert sub_comment_update.revision == unit.revision
    assert sub_comment_update.submitter == member
    assert sub_comment_update.type == SubmissionTypes.SYSTEM
    assert sub_comment_update.field == SubmissionFields.COMMENT
    assert sub_comment_update.new_value == unit.translator_comment
    assert sub_comment_update.old_value == (unit._frozen.translator_comment
                                            or "")
    assert not sub_comment_update.pk
Example #51
0
 def to_python(self, value):
     if not value:
         return multistring("", encoding="UTF-8")
     elif isinstance(value, multistring):
         return value
     elif isinstance(value, basestring):
         strings = value.split(SEPERATOR)
         if strings[-1] == PLURAL_PLACEHOLDER:
             strings = strings[:-1]
             plural = True
         else:
             plural = len(strings) > 1
         ms = multistring(strings, encoding="UTF-8")
         ms.plural = plural
         return ms
     elif isinstance(value, dict):
         return multistring([val for key, val in sorted(value.items())],
                            encoding="UTF-8")
     else:
         return multistring(value, encoding="UTF-8")
Example #52
0
 def test_plural_parse_message_with_newline(self):
     mString = multistring(
         ["one message\nwith newline", "other message\nwith newline"]
     )
     xml = (
         '<plurals name="teststring">\n'
         '    <item quantity="one">one message\\nwith newline</item>\n'
         '    <item quantity="other">other message\\nwith newline</item>\n'
         "</plurals>\n"
     )
     self.__check_parse(mString, xml)
Example #53
0
    def test_plurals(self):
        phpsource = r"""<?php
return [
    'welcome' => 'Welcome to our application',
    'apples' => 'There is one apple|There are many apples',
];
"""
        phpfile = self.phpparse(phpsource)
        assert len(phpfile.units) == 2
        phpunit = phpfile.units[0]
        assert phpunit.name == "return[]->'welcome'"
        assert phpunit.source == "Welcome to our application"
        phpunit = phpfile.units[1]
        assert phpunit.name == "return[]->'apples'"
        assert phpunit.source == multistring(
            ["There is one apple", "There are many apples"]
        )
        assert bytes(phpfile).decode() == phpsource
        phpunit.source = multistring(["There is an apple", "There are many apples"])
        assert bytes(phpfile).decode() == phpsource.replace("one apple", "an apple")
Example #54
0
 def fix_errata(self, terms):
     replaced = False
     newstrings = []
     for string in self.target_strings:
         newstring = self.replace(string, terms)
         if string != newstring:
             string = newstring
             replaced = True
         newstrings.append(string)
     if replaced:
         self.settarget(multistring(newstrings))
Example #55
0
 def test_plural_parse_message_with_comments(self):
     mString = multistring(["one message", "other message"])
     xml = (
         '<plurals name="teststring">\n'
         "    <!-- comment of one string -->\n"
         '    <item quantity="one">one message</item>\n'
         "    <!-- comment of other string -->\n"
         '    <item quantity="other">other message</item>\n'
         "</plurals>\n"
     )
     self.__check_parse(mString, xml)
Example #56
0
def to_python(value):
    """Reconstruct a multistring from the database string representation."""
    if not value:
        return multistring("", encoding="UTF-8")
    elif isinstance(value, multistring):
        return value
    elif isinstance(value, basestring):
        strings = value.split(SEPARATOR)
        if strings[-1] == PLURAL_PLACEHOLDER:
            strings = strings[:-1]
            plural = True
        else:
            plural = len(strings) > 1
        ms = multistring(strings, encoding="UTF-8")
        ms.plural = plural
        return ms
    elif isinstance(value, dict):
        return multistring([val for key, val in sorted(value.items())], encoding="UTF-8")
    else:
        return multistring(value, encoding="UTF-8")
Example #57
0
    def setsource(self, source):
        self._rich_source = None
#        assert isinstance(source, unicode)
        source = data.forceunicode(source or u"")
        source = source or u""
        if isinstance(source, multistring):
            self._source = source
        elif isinstance(source, unicode):
            self._source = source
        else:
            #unicode, list, dict
            self._source = multistring(source)
Example #58
0
    def test_increase(self):
        """checks that if the target language has more plural form the generated properties file is correct """
        propsource = 'test_me=I can code!\ntest_me[one]=I can code single!'
        propfile = self.propparse(propsource, "gwt", None, "en",
                                  "ar")  # All plural forms
        assert len(propfile.units) == 1
        propunit = propfile.units[0]

        assert isinstance(propunit.target, multistring)
        assert propunit.target.strings == ['', '', '', '', '', '']
        assert b'test_me=I can code!\ntest_me[none]=\ntest_me[one]=I can code single!\n' + \
               b'test_me[two]=\ntest_me[few]=\ntest_me[many]=\n' == propfile.__bytes__()

        propunit.target = {
            'other': 'other',
            'one': 'one',
            'zero': 'zero',
            'few': 'few',
            'two': 'two',
            'many': 'many'
        }
        assert isinstance(propunit.target, multistring)
        assert propunit.target.strings == [
            'zero', 'one', 'two', 'few', 'many', 'other'
        ]
        assert b'test_me=other\ntest_me[none]=zero\ntest_me[one]=one\n' + \
               b'test_me[two]=two\ntest_me[few]=few\ntest_me[many]=many\n' == propfile.__bytes__()

        propunit.target = multistring(
            ['zero', 'one', 'two', 'few', 'many', 'other'])
        assert isinstance(propunit.target, multistring)
        assert propunit.target.strings == [
            'zero', 'one', 'two', 'few', 'many', 'other'
        ]
        assert b'test_me=other\ntest_me[none]=zero\ntest_me[one]=one\n' + \
               b'test_me[two]=two\ntest_me[few]=few\ntest_me[many]=many\n' == propfile.__bytes__()

        propunit.target = ['zero', 'one', 'two', 'few', 'many', 'other']
        assert isinstance(propunit.target, multistring)
        assert propunit.target.strings == [
            'zero', 'one', 'two', 'few', 'many', 'other'
        ]
        assert b'test_me=other\ntest_me[none]=zero\ntest_me[one]=one\n' + \
               b'test_me[two]=two\ntest_me[few]=few\ntest_me[many]=many\n' == propfile.__bytes__()

        propunit.source = ['zero', 'one', 'two', 'few', 'many', 'other']
        assert isinstance(propunit.target, multistring)
        assert propunit.target.strings == [
            'zero', 'one', 'two', 'few', 'many', 'other'
        ]
        assert b'test_me=other\ntest_me[none]=zero\ntest_me[one]=one\n' + \
               b'test_me[two]=two\ntest_me[few]=few\ntest_me[many]=many\n' == propfile.__bytes__()
Example #59
0
 def save_unit(self):
     user = self.request_user
     target = multistring(self.cleaned_data["target_f"] or [u''])
     if target != self.unit.target:
         self.unit.target = self.cleaned_data["target_f"]
     if self.unit.target:
         if self.cleaned_data["is_fuzzy"]:
             self.unit.state = FUZZY
         else:
             self.unit.state = TRANSLATED
     else:
         self.unit.state = UNTRANSLATED
     self.unit.save(user=user, changed_with=SubmissionTypes.WEB)
Example #60
0
    def test_rich_get(self):
        """Basic test for converting from multistrings to StringElem trees."""
        target_mstr = multistring([u'tėst', u'<b>string</b>'])
        unit = self.UnitClass(multistring([u'a', u'b']))
        unit.rich_parsers = general.parsers
        unit.target = target_mstr
        elems = unit.rich_target

        if unit.hasplural():
            assert len(elems) == 2
            assert len(elems[0].sub) == 1
            assert len(elems[1].sub) == 3

            assert unicode(elems[0]) == target_mstr.strings[0]
            assert unicode(elems[1]) == target_mstr.strings[1]

            assert unicode(elems[1].sub[0]) == u'<b>'
            assert unicode(elems[1].sub[1]) == u'string'
            assert unicode(elems[1].sub[2]) == u'</b>'
        else:
            assert len(elems[0].sub) == 1
            assert unicode(elems[0]) == target_mstr.strings[0]