Google翻译API输出HTML实体 [英] Google Translate API outputs HTML entities

查看:229
本文介绍了Google翻译API输出HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

英语: Sale ID prefix is a required field

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

有没有办法让Google翻译不输出html实体,而是输出实际字符(')

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

代码:(请参阅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;
}
?>

推荐答案

根据Google翻译文档,您可以选择要提供要翻译的文本的格式(请参见查询参数中).如果未指定,则格式默认为HTML.

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.

您应该将此查询参数设置为text,以表明您正在发送纯文本,因为Google可能会以与接收到的格式相同的格式返回翻译后的文本.

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.

因此您的PHP代码可能会变成:

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 );

这篇关于Google翻译API输出HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆