Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Translate API outputs HTML entities

ENGLISH: Sale ID prefix is a required field

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

Is there a way to have google translate NOT output the html entity and instead output the actual character (')

CODE: (SEE translateTo)

#!/usr/bin/php
<?php
$languages = array('english' => 'en', 'spanish' => 'es', 'indonesia' => 'id', 'french' => 'fr', 'italian' => 'it', 'dutch' => 'nl', 'portugues' => 'pt', 'arabic' => 'ar');

fwrite(STDOUT, "Please enter file: ");
$file = trim(fgets(STDIN));

//Run until user kills it
while(true)
{
    fwrite(STDOUT, "Please enter key: ");
    $key = trim(fgets(STDIN));

    fwrite(STDOUT, "Please enter english value: ");
    $value = trim(fgets(STDIN));

    foreach($languages as $folder=>$code)
    {
        $path = dirname(__FILE__).'/../../application/language/'.$folder.'/'.$file;
        $transaltedValue = translateTo($value, $code);

        $current_file_contents = file_get_contents($path); 

        //If we have already translated, update it
        if (preg_match("/['\"]{1}${key}['\"]{1}/",$current_file_contents))
        {
            $find_existing_translation = "/(\[['\"]{1})(${key}['\"]{1}[^=]+=[ ]*['\"]{1})([^'\"]+)(['\"]{1};)/";
            $new_file_contents = preg_replace($find_existing_translation, '${1}${2}'.$transaltedValue.'${4}', $current_file_contents);
            file_put_contents($path, $new_file_contents);
        }
        else //We haven't translated: Add
        {
            $pair = "\$lang['$key'] = '$transaltedValue';";
            file_put_contents($path, str_replace('?>', "$pair\n?>", $current_file_contents));
        }
    }


    fwrite(STDOUT, "Quit? (y/n): ");
    $quit = strtolower(trim(fgets(STDIN)));

    if ($quit == 'y' || $quit == 'yes')
    {
        exit(0);
    }
}

function translateTo($value, $language_key)
{
    if ($language_key == 'en')
    {
        return $value;
    }

    $api_key = 'MY_API_KEY';
    $value = urlencode($value);

    $url ="https://www.googleapis.com/language/translate/v2?key=$api_key&q=$value&source=en&target=$language_key";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $body = curl_exec($ch);
    curl_close($ch);

    $json = json_decode($body);

    return $json->data->translations[0]->translatedText;
}
?>
like image 881
Chris Muench Avatar asked Nov 10 '14 19:11

Chris Muench


Video Answer


2 Answers

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 );
like image 125
My Head Hurts Avatar answered Oct 14 '22 21:10

My Head Hurts


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.

like image 40
Hope this was helpful Avatar answered Oct 14 '22 21:10

Hope this was helpful