コード例 #1
0
 def test_autotranslate(self):
     other = self.do_base()
     auto_translate(
         None,
         other.translation_set.get(language_code="cs").pk,
         "translate",
         "todo",
         "others",
         self.component.pk,
         [],
         99,
     )
     unit = self.get_unit()
     self.assertEqual(unit.all_checks_names, set())
コード例 #2
0
ファイル: edit.py プロジェクト: lacroixdavid1/weblate
def auto_translation(request, project, component, lang):
    translation = get_translation(request, project, component, lang)
    project = translation.component.project
    if not request.user.has_perm("translation.auto", project):
        raise PermissionDenied()

    autoform = AutoForm(translation.component, request.POST)

    if translation.component.locked or not autoform.is_valid():
        messages.error(request, _("Failed to process form!"))
        show_form_errors(request, autoform)
        return redirect(translation)

    args = (
        request.user.id,
        translation.id,
        autoform.cleaned_data["mode"],
        autoform.cleaned_data["filter_type"],
        autoform.cleaned_data["auto_source"],
        autoform.cleaned_data["component"],
        autoform.cleaned_data["engines"],
        autoform.cleaned_data["threshold"],
    )

    if settings.CELERY_TASK_ALWAYS_EAGER:
        messages.success(request, auto_translate(*args))
    else:
        task = auto_translate.delay(*args)
        messages.success(request, _("Automatic translation in progress"),
                         f"task:{task.id}")

    return redirect(translation)
コード例 #3
0
 def autotranslate(self, request, **kwargs):
     translation = self.get_object()
     if not request.user.has_perm("translation.auto", translation):
         self.permission_denied(request, message="Can not auto translate")
     autoform = AutoForm(translation.component, request.data)
     if translation.component.locked or not autoform.is_valid():
         return Response(
             data={
                 "result": "Unsuccessful",
                 "detail": "Failed to process autotranslation data!",
             },
             status=status.HTTP_400_BAD_REQUEST,
         )
     args = (
         request.user.id,
         translation.id,
         autoform.cleaned_data["mode"],
         autoform.cleaned_data["filter_type"],
         autoform.cleaned_data["auto_source"],
         autoform.cleaned_data["component"],
         autoform.cleaned_data["engines"],
         autoform.cleaned_data["threshold"],
     )
     return Response(
         data={
             "result": "Success",
             "details": auto_translate(*args)
         },
         status=status.HTTP_200_OK,
     )
コード例 #4
0
ファイル: auto_translate.py プロジェクト: renatofb/weblate
    def handle(self, *args, **options):
        # Get translation object
        translation = self.get_translation(**options)

        # Get user
        try:
            user = User.objects.get(username=options["user"])
        except User.DoesNotExist:
            raise CommandError("User does not exist!")

        source = None
        if options["source"]:
            parts = options["source"].split("/")
            if len(parts) != 2:
                raise CommandError("Invalid source component specified!")
            try:
                component = Component.objects.get(project__slug=parts[0],
                                                  slug=parts[1])
            except Component.DoesNotExist:
                raise CommandError("No matching source component found!")
            source = component.id

        if options["mt"]:
            for translator in options["mt"]:
                if translator not in MACHINE_TRANSLATION_SERVICES.keys():
                    raise CommandError(
                        f"Machine translation {translator} is not available")

        if options["mode"] not in ("translate", "fuzzy", "suggest"):
            raise CommandError("Invalid translation mode specified!")

        if options["inconsistent"]:
            filter_type = "check:inconsistent"
        elif options["overwrite"]:
            filter_type = "all"
        else:
            filter_type = "todo"

        result = auto_translate(
            user.id,
            translation.id,
            options["mode"],
            filter_type,
            "mt" if options["mt"] else "others",
            source,
            options["mt"],
            options["threshold"],
            translation=translation,
        )
        self.stdout.write(result["message"])
コード例 #5
0
ファイル: views.py プロジェクト: tsabi/weblate
 def autotranslate(self, request, **kwargs):
     translation = self.get_object()
     if not request.user.has_perm("translation.auto", translation):
         self.permission_denied(request, "Can not auto translate")
     autoform = AutoForm(translation.component, request.data)
     if translation.component.locked or not autoform.is_valid():
         raise ParseError("Failed to process autotranslation data!", "invalid")
     args = (
         request.user.id,
         translation.id,
         autoform.cleaned_data["mode"],
         autoform.cleaned_data["filter_type"],
         autoform.cleaned_data["auto_source"],
         autoform.cleaned_data["component"],
         autoform.cleaned_data["engines"],
         autoform.cleaned_data["threshold"],
     )
     return Response(
         data={"details": auto_translate(*args)},
         status=HTTP_200_OK,
     )