首页 > 解决方案 > 如何用罗马字母翻译成日文?

问题描述

我正在尝试用 PHP 翻译成日文,结果总是日文字符。但我需要罗马字母的结果(就像在https://translate.google.com上一样)

我可以使用 Google\Cloud\Translate\V3\TranslationServiceClient(高级)和 Google\Cloud\Translate\V2\TranslationServiceClient(基本)进行正常翻译

我还尝试了 REST API。

但它们都没有提供罗马(拉丁)字符。

有谁知道怎么做?

以下是一些示例代码,展示了我如何进行翻译:

require_once ('vendor/autoload.php');
use Google\Cloud\Translate\V3\TranslationServiceClient;

$translationClient = new TranslationServiceClient(['credentials' => $xxx]);
//$xxx contains my authentication JSON
$content = ['This is some text to tranlate'];
$targetLanguage = 'ja';

$response = $translationClient->translateText(
    $content,
    $targetLanguage,
    TranslationServiceClient::locationName('my Google cloud project ID', 'global')
);
foreach ($response->getTranslations() as $key => $translation) {
    $separator = $key === 2
        ? '!'
        : ', ';
    echo $translation->getTranslatedText() . $separator;
}

结果:これは翻訳するテキストです,

期望的结果:Kore wa hon'yaku suru tekisutodesu

$response 对象包含大量文本,但不是所需的翻译。

标签: google-translategoogle-cloud-translate

解决方案


推荐阅读