Beispiel #1
0
    def update_from_unit(self, unit, pos, force):
        '''
        Updates Unit from ttkit unit.
        '''
        # Generate values
        location = ', '.join(unit.getlocations())
        if hasattr(unit, 'typecomments'):
            flags = ', '.join(unit.typecomments)
        else:
            flags = ''
        if hasattr(unit.target, 'strings'):
            target = join_plural(unit.target.strings)
        else:
            target = unit.target
        fuzzy = unit.isfuzzy()
        translated = unit.istranslated()
        comment = unit.getnotes()

        # Update checks on fuzzy update or on content change
        same_content = (target == self.target and fuzzy == self.fuzzy)

        # Check if we actually need to change anything
        if not force and location == self.location and flags == self.flags and same_content and fuzzy == self.fuzzy and translated == self.translated and comment == self.comment and pos == self.position:
            return

        # Store updated values
        self.position = pos
        self.location = location
        self.flags = flags
        self.target = target
        self.fuzzy = fuzzy
        self.translated = translated
        self.comment = comment
        self.save(force_insert = force, backend = True, same_content = same_content)
Beispiel #2
0
    def update_from_unit(self, translation, unit, pos):
        """
        Process translation toolkit unit and stores/updates database entry.
        """
        if hasattr(unit.source, "strings"):
            src = join_plural(unit.source.strings)
        else:
            src = unit.source
        ctx = unit.getcontext()
        checksum = msg_checksum(src, ctx)
        from weblate.trans.models import Unit

        dbunit = None
        try:
            dbunit = self.get(translation=translation, checksum=checksum)
            force = False
        except Unit.MultipleObjectsReturned:
            # Some inconsistency (possibly race condition), try to recover
            self.filter(translation=translation, checksum=checksum).delete()
        except Unit.DoesNotExist:
            pass

        if dbunit is None:
            dbunit = Unit(translation=translation, checksum=checksum, source=src, context=ctx)
            force = True

        dbunit.update_from_unit(unit, pos, force)
        return dbunit
Beispiel #3
0
    def update_unit(self, unit, request):
        '''
        Updates backend file and unit.
        '''
        store = self.get_store()
        src = unit.get_source_plurals()[0]
        need_save = False
        # Find all units with same source
        for pounit in store.findunits(src):
            # Does context match?
            if pounit.getcontext() == unit.context:
                # Is it plural?
                if hasattr(pounit.target, 'strings'):
                    potarget = join_plural(pounit.target.strings)
                else:
                    potarget = pounit.target
                # Is there any change
                if unit.target != potarget or unit.fuzzy != pounit.isfuzzy():
                    # Update fuzzy flag
                    pounit.markfuzzy(unit.fuzzy)
                    # Store translations
                    if unit.is_plural():
                        pounit.settarget(unit.get_target_plurals())
                    else:
                        pounit.settarget(unit.target)
                    # We need to update backend
                    need_save = True
                # We should have only one match
                break

        # Save backend if there was a change
        if need_save:
            author = self.get_author_name(request.usr)
            # Update po file header
            if hasattr(store, 'updateheader'):
                po_revision_date = datetime.now().strftime('%Y-%m-%d %H:%M') + poheader.tzstring()

                store.updateheader(
                    add = True,
                    last_translator = author,
                    plural_forms = self.language.get_plural_form(),
                    language = self.language.code,
                    PO_Revision_Date = po_revision_date,
                    x_generator = 'Weblate %s' % weblate.VERSION
                    )
            # commit possible previous changes (by other author)
            self.commit_pending(author)
            # save translation changes
            store.save()
            # commit Git repo if needed
            self.git_commit(author, sync = True)

        return need_save, pounit
Beispiel #4
0
    def update_from_unit(self, translation, unit, pos):
        '''
        Process translation toolkit unit and stores/updates database entry.
        '''
        if hasattr(unit.source, 'strings'):
            src = join_plural(unit.source.strings)
        else:
            src = unit.source
        ctx = unit.getcontext()
        checksum = msg_checksum(src, ctx)

        # Try getting existing unit
        from weblate.trans.models import Unit
        dbunit = None
        try:
            dbunit = self.get(
                translation = translation,
                checksum = checksum)
            force = False
        except Unit.MultipleObjectsReturned:
            # Some inconsistency (possibly race condition), try to recover
            self.filter(
                translation = translation,
                checksum = checksum).delete()
        except Unit.DoesNotExist:
            pass

        # Create unit if it does not exist
        if dbunit is None:
            dbunit = Unit(
                translation = translation,
                checksum = checksum,
                source = src,
                context = ctx)
            force = True

        # Update all details
        dbunit.update_from_unit(unit, pos, force)

        # Return result
        return dbunit, force