English Text is Always Getting Translated Despite Not Matching from Parameter in Request URL

Mostafa Abdelmaksoud 20 Reputation points
2025-10-01T11:48:53.92+00:00

Hello, I following the Quickstart: Azure AI Translator REST APIs tutorial to test out the translation service, however I am seeing some strange behaviour regarding English text. When the text I send off to be translated is in English and the from parameter in my request url does not match the English Language code, the returned text is always translated

        string route = "/translate?api-version=3.0&from=fr&to=sq";
        string textToTranslate = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

Response:

[{"translations":[{"text":"Lorem Ipsum është thjesht tekst fik i industrisë së shtypit dhe shtypit. Lorem Ipsum ka qenë teksti standard i industrisë që nga vitet 1500, kur një printer i panjohur mori një galeri të shkronjave dhe e përgojoi atë për të bërë një libër ekzemplar të tipit. Ajo ka mbijetuar jo vetëm pesë shekuj, por edhe kërcimin në shtypshkrografinë elektronike, duke mbetur thelbësisht e pandryshuar. Ai u popullarizua në vitet 1960 me lëshimin e fletëve Letraset që përmbajnë pasazhe Lorem Ipsum, dhe kohët e fundit me softuerin e botimit desktop si Aldus PageMaker duke përfshirë versionet e Lorem Ipsum.","to":"sq"}]}]

However, in any other scenario where the text to be translated does not match the from parameter in the request url, the returned text is not translated. Instead, the returned text is just the exact same text that is sent in the request

In this Request, I am sending dutch text to be translated while the from parameter indicates the French language code. Notice how the returned text is the exact same

        string route = "/translate?api-version=3.0&from=fr&to=sq";
        string textToTranslate = "Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.";

Response

[{"translations":[{"text":"Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.","to":"sq"}]}]


Why is the English text always getting translated? I would not want the text to get translated if it doesnt match the source language. What can I do to make sure that happens with English?

Azure AI Translator
Azure AI Translator
An Azure service to easily conduct machine translation with a simple REST API call.
{count} votes

Answer accepted by question author
  1. Manas Mohanty 11,850 Reputation points Microsoft External Staff Moderator
    2025-10-02T18:22:27.2833333+00:00

    Hi Mostafa Abdelmaksoud

    To enforce that only French input is accepted and show an error if the input is in any other language, you can use the Azure Translator Detect API before attempting translation. Here's how to modify your code.

    import requests, uuid, json
    
    # Add your key and endpoint
    key = "<key>"
    endpoint = "https://api.cognitive.microsofttranslator.com/" #text translation endpoint
    location = "northeurope"
    
    # Input text
    
    input_text = "Hello, friend! What did you do today?"
    
    xt = "Bonjour Monsier Manas"
    
    # Step 1: Detect language
    detect_url = endpoint + "/detect"
    detect_headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': location,
        'Content-type': 'application/json',
        'X-ClientTraceId': str(uuid.uuid4())
    }
    detect_body = [{'text': input_text}]
    detect_response = requests.post(detect_url, params={'api-version': '3.0'}, headers=detect_headers, json=detect_body)
    detected_language = detect_response.json()[0]['language']
    
    # Step 2: Validate and translate if French
    if detected_language == 'fr':
        translate_url = endpoint + "/translate"
        translate_params = {
            'api-version': '3.0',
            'from': 'fr',
            'to': ['sq']
        }
        translate_headers = detect_headers  # Same headers
        translate_body = [{'text': input_text}]
        translate_response = requests.post(translate_url, params=translate_params, headers=translate_headers, json=translate_body)
        print(json.dumps(translate_response.json(), sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))
    else:
        print(f"Error: Only French input is accepted. Detected language was '{detected_language}'ptu.")
    
    
    

    #output

    Error: Only French input is accepted. Detected language was 'en'.
    
    
    

    Reference used -

    https://docs.azure.cn/en-us/ai-services/translator/text-translation/how-to/use-rest-api?tabs=python#translate-text

    https://free.blessedness.top/en-us/azure/ai-services/translator/text-translation/reference/v3/detect

    Hope it helps address the issue.

    Thank you

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.