Google Translate API outputs HTML entities

前端 未结 4 2049
小蘑菇
小蘑菇 2021-01-18 00:52

ENGLISH: Sale ID prefix is a required field

FRENCH: Vente préfixe d'ID est un champ obligatoire

Is there a way to have goog

相关标签:
4条回答
  • 2021-01-18 01:04

    If you specify format Text, content inside HTML tags will be translated as well. Assume your input is:

    This is a <a href="https://example.com/path">link</a>

    then example and path will be translated as well, which breaks the link.

    To avoid this and fix your problem, stick with format HTML and unescape the text you received back from google translate. In php you might use html_entity_decode.

    0 讨论(0)
  • 2021-01-18 01:05

    If you use google translate client lib, you should pass format_ in translate method, not format, it is format_ below are google translate python api:

    0 讨论(0)
  • 2021-01-18 01:12

    According to the Google Translate documentation, you can choose which format you will provide the text which is to be translated (see format in query parameters). The format defaults to HTML if not specfied.

    You should set this query parameter to text to indicate that you are sending plain-text as Google will likely return the translated text in the same format as it is received.

    So your PHP code could become:

    $baseUrl = "https://www.googleapis.com/language/translate/v2";
    $params ="?key=$api_key&q=$value&source=en&target=$language_key&format=text";
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $baseUrl + $params );
    
    0 讨论(0)
  • 2021-01-18 01:13

    For anyone working in java, there is a format method in Translate.TranslateOption

    So right now you might have something translate call like such:

    YourTranslateObject.translate(yourTextToBeTranslated,Translate.TranslateOption.targetLanguage(yourTargetLanguageCode))

    all you need to do is add a third parameter:

    YourTranslateObject.translate(yourTextToBeTranslated,Translate.TranslateOption.targetLanguage(yourTargetLanguageCode), Translate.TranslateOption.format("text"))

    since HTML is default, this will switch it to text.

    0 讨论(0)
提交回复
热议问题