Exemplo n.º 1
0
    def translate(
        self,
        user,
        new_target,
        new_state,
        change_action=None,
        propagate: bool = True,
        author=None,
    ):
        """
        Store new translation of a unit.

        Propagation is currently disabled on import.
        """
        # Fetch current copy from database and lock it for update
        self.old_unit = Unit.objects.select_for_update().get(pk=self.pk)

        # Handle simple string units
        if isinstance(new_target, str):
            new_target = [new_target]

        # Apply autofixes
        if not self.translation.is_template:
            new_target, self.fixups = fix_target(new_target, self)

        # Update unit and save it
        self.target = join_plural(new_target)
        not_empty = bool(max(new_target))

        # Newlines fixup
        if "dos-eol" in self.all_flags:
            self.target = NEWLINES.sub("\r\n", self.target)

        if not_empty:
            self.state = new_state
        else:
            self.state = STATE_EMPTY
        self.original_state = self.state
        saved = self.save_backend(user,
                                  change_action=change_action,
                                  propagate=propagate,
                                  author=author)

        # Enforced checks can revert the state to needs editing (fuzzy)
        if (self.state >= STATE_TRANSLATED
                and self.translation.component.enforced_checks
                and self.all_checks_names
                & set(self.translation.component.enforced_checks)):
            self.state = self.original_state = STATE_FUZZY
            self.save(run_checks=False,
                      same_content=True,
                      update_fields=["state"])

        if (propagate and user and self.target != self.old_unit.target
                and self.state >= STATE_TRANSLATED
                and self.translation.component.is_glossary):
            transaction.on_commit(
                lambda: handle_unit_translation_change.delay(self.id, user.id))

        return saved
Exemplo n.º 2
0
def perform_translation(unit, form, request):
    """
    Handles translation and stores it to a backend.
    """
    # Remember old checks
    oldchecks = set(unit.active_checks().values_list("check", flat=True))

    # Run AutoFixes on user input
    new_target, fixups = fix_target(form.cleaned_data["target"], unit)

    # Save
    saved = unit.translate(request, new_target, form.cleaned_data["fuzzy"])

    # Warn about applied fixups
    if len(fixups) > 0:
        messages.info(
            request, _("Following fixups were applied to translation: %s") % ", ".join([unicode(f) for f in fixups])
        )

    # Get new set of checks
    newchecks = set(unit.active_checks().values_list("check", flat=True))

    # Did we introduce any new failures?
    if saved and newchecks > oldchecks:
        # Show message to user
        messages.error(
            request,
            _("Some checks have failed on your translation: {0}").format(
                ", ".join([unicode(CHECKS[check].name) for check in newchecks])
            ),
        )
        # Stay on same entry
        return False

    return True
Exemplo n.º 3
0
def perform_translation(unit, form, request):
    '''
    Handles translation and stores it to a backend.
    '''
    # Remember old checks
    oldchecks = set(unit.active_checks().values_list('check', flat=True))

    # Run AutoFixes on user input
    new_target, fixups = fix_target(form.cleaned_data['target'], unit)

    # Save
    saved = unit.translate(request, new_target, form.cleaned_data['fuzzy'])

    # Warn about applied fixups
    if len(fixups) > 0:
        messages.info(
            request,
            _('Following fixups were applied to translation: %s') %
            ', '.join([unicode(f) for f in fixups]))

    # Get new set of checks
    newchecks = set(unit.active_checks().values_list('check', flat=True))

    # Did we introduce any new failures?
    if saved and newchecks > oldchecks:
        # Show message to user
        messages.error(
            request,
            _('Some checks have failed on your translation: {0}').format(
                ', '.join([unicode(CHECKS[check].name)
                           for check in newchecks])))
        # Stay on same entry
        return False

    return True
Exemplo n.º 4
0
def perform_translation(unit, form, request):
    """Handle translation and stores it to a backend."""
    # Remember old checks
    oldchecks = set(
        unit.active_checks().values_list('check', flat=True)
    )

    # Run AutoFixes on user input
    if not unit.translation.is_template:
        new_target, fixups = fix_target(form.cleaned_data['target'], unit)
    else:
        new_target = form.cleaned_data['target']
        fixups = []

    # Save
    saved = unit.translate(
        request.user,
        new_target,
        form.cleaned_data['state']
    )

    # Should we skip to next entry
    if not saved:
        revert_rate_limit('translate', request)
        return True

    # Warn about applied fixups
    if fixups:
        messages.info(
            request,
            _('Following fixups were applied to translation: %s') %
            ', '.join([force_text(f) for f in fixups])
        )

    # Get new set of checks
    newchecks = set(
        unit.active_checks().values_list('check', flat=True)
    )

    # Did we introduce any new failures?
    if saved and newchecks > oldchecks:
        # Show message to user
        messages.error(
            request,
            _(
                'The translation has been saved, however there '
                'are some newly failing checks: {0}'
            ).format(
                ', '.join(
                    [force_text(CHECKS[check].name) for check in newchecks]
                )
            )
        )
        # Stay on same entry
        return False

    return True
Exemplo n.º 5
0
def perform_translation(unit, form, request):
    """Handle translation and stores it to a backend."""
    # Remember old checks
    oldchecks = set(
        unit.active_checks().values_list('check', flat=True)
    )

    # Run AutoFixes on user input
    if not unit.translation.is_template:
        new_target, fixups = fix_target(form.cleaned_data['target'], unit)
    else:
        new_target = form.cleaned_data['target']
        fixups = []

    # Save
    saved = unit.translate(
        request,
        new_target,
        form.cleaned_data['state']
    )

    # Should we skip to next entry
    if not saved:
        revert_rate_limit('translate', request)
        return True

    # Warn about applied fixups
    if fixups:
        messages.info(
            request,
            _('Following fixups were applied to translation: %s') %
            ', '.join([force_text(f) for f in fixups])
        )

    # Get new set of checks
    newchecks = set(
        unit.active_checks().values_list('check', flat=True)
    )

    # Did we introduce any new failures?
    if saved and newchecks > oldchecks:
        # Show message to user
        messages.error(
            request,
            _(
                'The translation has been saved, however there '
                'are some newly failing checks: {0}'
            ).format(
                ', '.join(
                    [force_text(CHECKS[check].name) for check in newchecks]
                )
            )
        )
        # Stay on same entry
        return False

    return True
Exemplo n.º 6
0
def perform_translation(unit, form, request):
    '''
    Handles translation and stores it to a backend.
    '''
    # Remember old checks
    oldchecks = set(
        unit.active_checks().values_list('check', flat=True)
    )

    # Run AutoFixes on user input
    if not unit.translation.is_template():
        new_target, fixups = fix_target(form.cleaned_data['target'], unit)
    else:
        new_target = form.cleaned_data['target']
        fixups = []

    # Save
    saved = unit.translate(
        request,
        new_target,
        form.cleaned_data['fuzzy']
    )

    # Warn about applied fixups
    if len(fixups) > 0:
        messages.info(
            request,
            _('Following fixups were applied to translation: %s') %
            ', '.join([unicode(f) for f in fixups])
        )

    # Get new set of checks
    newchecks = set(
        unit.active_checks().values_list('check', flat=True)
    )

    # Did we introduce any new failures?
    if saved and newchecks > oldchecks:
        # Show message to user
        messages.error(
            request,
            _(
                'Some checks have failed on your translation: {0}'
            ).format(
                ', '.join(
                    [unicode(CHECKS[check].name) for check in newchecks]
                )
            )
        )
        # Stay on same entry
        return False

    return True
Exemplo n.º 7
0
def perform_translation(unit, form, request):
    """Handle translation and stores it to a backend."""
    # Remember old checks
    oldchecks = unit.all_checks_names

    # Run AutoFixes on user input
    if not unit.translation.is_template:
        new_target, fixups = fix_target(form.cleaned_data["target"], unit)
    else:
        new_target = form.cleaned_data["target"]
        fixups = []

    # Save
    saved = unit.translate(request.user, new_target, form.cleaned_data["state"])

    # Should we skip to next entry
    if not saved:
        revert_rate_limit("translate", request)
        return True

    # Warn about applied fixups
    if fixups:
        messages.info(
            request,
            _("Following fixups were applied to translation: %s")
            % ", ".join(force_str(f) for f in fixups),
        )

    # Get new set of checks
    newchecks = unit.all_checks_names

    # Did we introduce any new failures?
    if (
        saved
        and form.cleaned_data["state"] >= STATE_TRANSLATED
        and newchecks > oldchecks
    ):
        # Show message to user
        messages.error(
            request,
            _(
                "The translation has been saved, however there "
                "are some newly failing checks: {0}"
            ).format(", ".join(force_str(CHECKS[check].name) for check in newchecks)),
        )
        # Stay on same entry
        return False

    return True
Exemplo n.º 8
0
 def test_fix_target(self):
     unit = MockUnit(source="Foo…")
     fixed, fixups = fix_target(["Bar..."], unit)
     self.assertEqual(fixed, ["Bar…"])
     self.assertEqual(len(fixups), 1)
     self.assertEqual(force_text(fixups[0]), "Trailing ellipsis")
Exemplo n.º 9
0
 def test_fix_target(self):
     unit = MockUnit(source='Foo…')
     fixed, fixups = fix_target(['Bar...'], unit)
     self.assertEqual(fixed, ['Bar…'])
     self.assertEqual(len(fixups), 1)
     self.assertEqual(force_text(fixups[0]), 'Trailing ellipsis')
Exemplo n.º 10
0
 def test_fix_target(self):
     unit = MockUnit(source='Foo…')
     fixed, fixups = fix_target(['Bar...'], unit)
     self.assertEqual(fixed, ['Bar…'])
     self.assertEqual(len(fixups), 1)
     self.assertEqual(force_text(fixups[0]), 'Trailing ellipsis')
Exemplo n.º 11
0
 def test_fix_target(self):
     unit = Unit(source=u'Foo…')
     fixed, fixups = fix_target(['Bar...'], unit)
     self.assertEqual(fixed, [u'Bar…'])
     self.assertEqual(len(fixups), 1)
     self.assertEqual(unicode(fixups[0]), u'Trailing ellipsis')
Exemplo n.º 12
0
 def test_fix_target(self):
     unit = MockUnit(source="Foo…")
     fixed, fixups = fix_target(["Bar..."], unit)
     self.assertEqual(fixed, ["Bar…"])
     self.assertEqual(len(fixups), 1)
     self.assertEqual(str(fixups[0]), "Trailing ellipsis")
Exemplo n.º 13
0
 def test_fix_target(self):
     unit = Unit(source=u'Foo…')
     fixed, fixups = fix_target(['Bar...'], unit)
     self.assertEqual(fixed, [u'Bar…'])
     self.assertEqual(len(fixups), 1)
     self.assertEqual(unicode(fixups[0]), u'Trailing ellipsis')