ENGLISH:
Sale ID prefix is a required field
FRENCH:
Vente préfixe d'ID est un champ obligatoire
有没有办法让谷歌翻译不输出html实体,而是输出实际的字符(')
代码:(见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;
}
?>
答案 0 :(得分:10)
根据Google翻译文档,您可以选择提供待翻译文本的格式(请参阅query parameters中的format
)。如果没有指定,格式默认为HTML
。
您应该将此查询参数设置为text
,以表明您要发送纯文本,因为Google可能会返回与收到的格式相同的翻译文本。
所以你的PHP代码可能变成:
$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 );
答案 1 :(得分:2)
对于使用Java的任何人,Translate.TranslateOption中都有一个format方法
所以现在您可能会有类似这样的翻译呼叫:
YourTranslateObject.translate(yourTextToBeTranslated,Translate.TranslateOption.targetLanguage(yourTargetLanguageCode))
您需要做的就是添加第三个参数:
YourTranslateObject.translate(yourTextToBeTranslated,Translate.TranslateOption.targetLanguage(yourTargetLanguageCode), Translate.TranslateOption.format("text"))
由于HTML是默认设置,因此会将其切换为文本。
答案 2 :(得分:0)
答案 3 :(得分:0)
如果您指定格式Text
,HTML标记内的内容也将被翻译。假设您输入的是:
This is a <a href="https://example.com/path">link</a>
然后example
和path
也将被翻译,从而断开链接。
为避免这种情况并解决您的问题,请坚持使用HTML
格式并取消转义您从Google翻译收到的文字。在php中,您可以使用html_entity_decode。