Exemplo n.º 1
0
def google_translate(request):
    """Get translation from Google machine translation service."""
    try:
        text = request.GET["text"]
        locale_code = request.GET["locale"]
    except MultiValueDictKeyError as e:
        return JsonResponse(
            {
                "status": False,
                "message": "Bad Request: {error}".format(error=e)
            },
            status=400,
        )

    # Validate if locale exists in the database to avoid any potential XSS attacks.
    if not Locale.objects.filter(google_translate_code=locale_code).exists():
        return JsonResponse(
            {
                "status": False,
                "message": "Not Found: {error}".format(error=locale_code),
            },
            status=404,
        )

    data = get_google_translate_data(text, locale_code)

    if not data["status"]:
        return JsonResponse(data, status=400)

    return JsonResponse(data)
Exemplo n.º 2
0
def get_translations(entity, locale):
    """
    Get pretranslations for the entity-locale pair

    :arg Entity entity: the Entity object
    :arg Locale locale: the Locale object

    :returns: a list of tuple with:
        - a pretranslation of the entity
        - plural form
        - user - tm_user/gt_user
    """
    tm_user = User.objects.get(email="*****@*****.**")
    gt_user = User.objects.get(email="*****@*****.**")

    strings = []
    plural_forms = range(0, locale.nplurals or 1)

    # Try to get matches from translation_memory
    tm_response = get_translation_memory_data(
        text=entity.string,
        locale=locale,
    )

    tm_response = [t for t in tm_response if int(t["quality"]) == 100]

    if tm_response:
        if entity.string_plural == "":
            strings = [(tm_response[0]["target"], None, tm_user)]
        else:
            for plural_form in plural_forms:
                strings.append(
                    (tm_response[0]["target"], plural_form, tm_user))

    # Else fetch from google translate
    elif locale.google_translate_code:
        gt_response = get_google_translate_data(
            text=entity.string,
            locale_code=locale.google_translate_code,
        )

        if gt_response["status"]:
            if entity.string_plural == "":
                strings = [(gt_response["translation"], None, gt_user)]
            else:
                for plural_form in plural_forms:
                    strings.append(
                        (gt_response["translation"], plural_form, gt_user))
    return strings
Exemplo n.º 3
0
def google_translate(request):
    """Get translation from Google machine translation service."""
    try:
        text = request.GET["text"]
        locale_code = request.GET["locale"]

        if not locale_code:
            raise ValueError("Locale code is empty")

    except (MultiValueDictKeyError, ValueError) as e:
        return JsonResponse(
            {
                "status": False,
                "message": f"Bad Request: {e}"
            },
            status=400,
        )

    data = get_google_translate_data(text, locale_code)

    if not data["status"]:
        return JsonResponse(data, status=400)

    return JsonResponse(data)