Exemplo n.º 1
0
def mt_services(request):
    """Generate list of installed machine translation services in JSON."""
    # Machine translation
    machine_services = list(MACHINE_TRANSLATION_SERVICES.keys())

    return JsonResponse(
        data=machine_services,
        safe=False,
    )
Exemplo n.º 2
0
def ping():
    return {
        "version": weblate.utils.version.GIT_VERSION,
        "vcs": sorted(VCS_REGISTRY.keys()),
        "formats": sorted(FILE_FORMATS.keys()),
        "mt_services": sorted(MACHINE_TRANSLATION_SERVICES.keys()),
        "encoding": [sys.getfilesystemencoding(), sys.getdefaultencoding()],
        "uid": os.getuid(),
    }
Exemplo n.º 3
0
Arquivo: js.py Projeto: dekoza/weblate
def mt_services(request):
    """Generate list of installed machine translation services in JSON."""
    # Machine translation
    machine_services = list(MACHINE_TRANSLATION_SERVICES.keys())

    return JsonResponse(
        data=machine_services,
        safe=False,
    )
Exemplo n.º 4
0
    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"])
Exemplo n.º 5
0
    def __init__(self, obj, user, *args, **kwargs):
        """Generate choices for other component in same project."""
        other_components = obj.component.project.component_set.exclude(
            id=obj.component.id
        )
        choices = [(s.id, force_text(s)) for s in other_components]

        # Add components from other owned projects
        owned_components = Component.objects.filter(
            project__in=user.owned_projects,
        ).exclude(
            project=obj.component.project
        ).distinct()
        for component in owned_components:
            choices.append(
                (component.id, force_text(component))
            )

        super(AutoForm, self).__init__(*args, **kwargs)

        self.fields['component'].choices = \
            [('', _('All components in current project'))] + choices
        self.fields['engines'].choices = [
            (key, mt.name) for key, mt in MACHINE_TRANSLATION_SERVICES.items()
        ]
        if 'weblate' in MACHINE_TRANSLATION_SERVICES.keys():
            self.fields['engines'].initial = 'weblate'

        use_types = {
            'all', 'nottranslated', 'todo', 'fuzzy', 'check:inconsistent',
        }

        self.fields['type'].choices = [
            x for x in self.fields['type'].choices if x[0] in use_types
        ]

        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            Field('type'),
            InlineRadios('auto_source', id='select_auto_source'),
            Div(
                'component',
                css_id='auto_source_others'
            ),
            Div(
                'engines',
                'threshold',
                css_id='auto_source_mt'
            ),
        )
Exemplo n.º 6
0
    def __init__(self, obj, user, *args, **kwargs):
        """Generate choices for other component in same project."""
        other_components = obj.component.project.component_set.exclude(
            id=obj.component.id
        )
        choices = [(s.id, force_text(s)) for s in other_components]

        # Add components from other owned projects
        owned_components = Component.objects.filter(
            project__in=user.owned_projects,
        ).exclude(
            project=obj.component.project
        ).distinct()
        for component in owned_components:
            choices.append(
                (component.id, force_text(component))
            )

        super(AutoForm, self).__init__(*args, **kwargs)

        self.fields['component'].choices = \
            [('', _('All components in current project'))] + choices
        self.fields['engines'].choices = [
            (key, mt.name) for key, mt in MACHINE_TRANSLATION_SERVICES.items()
        ]
        if 'weblate' in MACHINE_TRANSLATION_SERVICES.keys():
            self.fields['engines'].initial = 'weblate'

        use_types = {
            'all', 'nottranslated', 'todo', 'fuzzy', 'check:inconsistent',
        }

        self.fields['type'].choices = [
            x for x in self.fields['type'].choices if x[0] in use_types
        ]

        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            Field('type'),
            InlineRadios('auto_source', id='select_auto_source'),
            Div(
                'component',
                css_id='auto_source_others'
            ),
            Div(
                'engines',
                'threshold',
                css_id='auto_source_mt'
            ),
        )
Exemplo n.º 7
0
    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!')

        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
        else:
            source = ''

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

        if options['inconsistent']:
            filter_type = 'check:inconsistent'
        elif options['overwrite']:
            filter_type = 'all'
        else:
            filter_type = 'todo'
        # Create fake request object
        request = HttpRequest()
        request.user = user
        auto = AutoTranslate(user, translation, filter_type, request)
        if options['mt']:
            auto.process_mt(options['mt'], options['threshold'])
        else:
            auto.process_others(source, check_acl=False)
        self.stdout.write('Updated {0} units'.format(auto.updated))
Exemplo n.º 8
0
    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!')

        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
        else:
            source = ''

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

        if options['inconsistent']:
            filter_type = 'check:inconsistent'
        elif options['overwrite']:
            filter_type = 'all'
        else:
            filter_type = 'todo'
        # Create fake request object
        request = HttpRequest()
        request.user = user
        auto = AutoTranslate(user, translation, filter_type, request)
        if options['mt']:
            auto.process_mt(options['mt'], options['threshold'])
        else:
            auto.process_others(source, check_acl=False)
        self.stdout.write('Updated {0} units'.format(auto.updated))
Exemplo n.º 9
0
    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!")

        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
        else:
            source = ""

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

        if options["inconsistent"]:
            filter_type = "check:inconsistent"
        elif options["overwrite"]:
            filter_type = "all"
        else:
            filter_type = "todo"
        auto = AutoTranslate(user, translation, filter_type, "translate")
        if options["mt"]:
            auto.process_mt(options["mt"], options["threshold"])
        else:
            auto.process_others(source)
        self.stdout.write("Updated {0} units".format(auto.updated))
Exemplo n.º 10
0
def translate(request, project, component, lang):
    """Generic entry point for translating, suggesting and searching."""
    obj, project, unit_set = parse_params(request, project, component, lang)

    # Search results
    search_result = search(obj, unit_set, request)

    # Handle redirects
    if isinstance(search_result, HttpResponse):
        return search_result

    # Get numer of results
    num_results = len(search_result["ids"])

    # Search offset
    offset = search_result["offset"]

    # Checksum unit access
    checksum_form = ChecksumForm(unit_set, request.GET or request.POST)
    if checksum_form.is_valid():
        unit = checksum_form.cleaned_data["unit"]
        try:
            offset = search_result["ids"].index(unit.id) + 1
        except ValueError:
            messages.warning(request, _("No string matched your search!"))
            return redirect(obj)
    else:
        # Check boundaries
        if not 0 < offset <= num_results:
            messages.info(request, _("The translation has come to an end."))
            # Delete search
            del request.session[search_result["key"]]
            return redirect(obj)

        # Grab actual unit
        try:
            unit = unit_set.get(pk=search_result["ids"][offset - 1])
        except Unit.DoesNotExist:
            # Can happen when using SID for other translation
            messages.error(request, _("Invalid search string!"))
            return redirect(obj)

    # Check locks
    locked = unit.translation.component.locked

    # Some URLs we will most likely use
    base_unit_url = "{}?{}&offset=".format(obj.get_translate_url(),
                                           search_result["url"])
    this_unit_url = base_unit_url + str(offset)
    next_unit_url = base_unit_url + str(offset + 1)

    response = None

    # Any form submitted?
    if "skip" in request.POST:
        return redirect(next_unit_url)
    if request.method == "POST" and "merge" not in request.POST:
        if (not locked and "accept" not in request.POST
                and "accept_edit" not in request.POST
                and "delete" not in request.POST and "spam" not in request.POST
                and "upvote" not in request.POST
                and "downvote" not in request.POST):
            # Handle translation
            response = handle_translate(request, unit, this_unit_url,
                                        next_unit_url)
        elif not locked or "delete" in request.POST or "spam" in request.POST:
            # Handle accepting/deleting suggestions
            response = handle_suggestions(request, unit, this_unit_url,
                                          next_unit_url)

    # Handle translation merging
    elif "merge" in request.POST and not locked:
        response = handle_merge(unit, request, next_unit_url)

    # Handle reverting
    elif "revert" in request.GET and not locked:
        response = handle_revert(unit, request, this_unit_url)

    # Pass possible redirect further
    if response is not None:
        return response

    # Show secondary languages for signed in users
    if request.user.is_authenticated:
        secondary = unit.get_secondary_units(request.user)
    else:
        secondary = None

    # Spam protection
    antispam = AntispamForm()

    # Prepare form
    form = TranslationForm(request.user, unit)
    sort = get_sort_name(request)

    return render(
        request,
        "translate.html",
        {
            "this_unit_url":
            this_unit_url,
            "first_unit_url":
            base_unit_url + "1",
            "last_unit_url":
            base_unit_url + str(num_results),
            "next_unit_url":
            next_unit_url,
            "prev_unit_url":
            base_unit_url + str(offset - 1),
            "object":
            obj,
            "project":
            project,
            "unit":
            unit,
            "nearby":
            unit.nearby(request.user.profile.nearby_strings),
            "nearby_keys":
            unit.nearby_keys(request.user.profile.nearby_strings),
            "others":
            get_other_units(unit),
            "search_url":
            search_result["url"],
            "search_items":
            search_result["items"],
            "search_query":
            search_result["query"],
            "offset":
            offset,
            "sort_name":
            sort["name"],
            "sort_query":
            sort["query"],
            "filter_name":
            search_result["name"],
            "filter_count":
            num_results,
            "filter_pos":
            offset,
            "form":
            form,
            "antispam":
            antispam,
            "comment_form":
            CommentForm(
                project,
                initial={
                    "scope": "global" if unit.is_source else "translation"
                },
            ),
            "context_form":
            ContextForm(instance=unit.source_unit, user=request.user),
            "search_form":
            search_result["form"].reset_offset(),
            "secondary":
            secondary,
            "locked":
            locked,
            "glossary":
            Term.objects.get_terms(unit),
            "addterm_form":
            TermForm(project),
            "last_changes":
            unit.change_set.prefetch().order()[:10],
            "screenshots": (unit.source_unit.screenshots.all()
                            | unit.screenshots.all()).order,
            "last_changes_url":
            urlencode(unit.translation.get_reverse_url_kwargs()),
            "display_checks":
            list(get_display_checks(unit)),
            "machinery_services":
            json.dumps(list(MACHINE_TRANSLATION_SERVICES.keys())),
        },
    )