Hello Google Team,
I am stuck in same language translation
Environment details
OS: Linux
PHP version: 7.2.34
Steps to reproduce
just pass the same language code in $source_language and $target_language ("en","en")
Code example
$text = "I Love google translation";
$tl = 'en';
$sl = 'en';
$project_id = 'MY_PROJECT_ID';
$glossary_id = 'MY_GLOSSARY_ID';
function v3_translate_text_with_glossary($text,$targetLanguage,$sourceLanguage,$projectId,$glossaryId)
{
$translationServiceClient = new TranslationServiceClient();
$request["content"] = $text;
try {
$formattedParent = $translationServiceClient->locationName($projectId, 'global');
$response = $translationServiceClient->detectLanguage($formattedParent, $request);
//echo "<pre>";print_r($response->getLanguageCode());die;
foreach ($response->getLanguages() as $language) {
printf('getDetectedLanguageCode text: %s' . PHP_EOL, $language->getLanguageCode());
}
}
finally {
$translationServiceClient->close();
}
$glossaryPath = $translationServiceClient->glossaryName(
$projectId,
'us-central1',
$glossaryId
);
$contents = [$text];
$formattedParent = $translationServiceClient->locationName(
$projectId,
'us-central1'
);
$glossaryConfig = new TranslateTextGlossaryConfig();
$glossaryConfig->setGlossary($glossaryPath);
// Optional. Can be "text/plain" or "text/html".
$mimeType = 'text/plain';
try {
$response = $translationServiceClient->translateText(
$contents,
$targetLanguage,
$formattedParent,
[
'sourceLanguageCode' => $sourceLanguage,
'glossaryConfig' => $glossaryConfig,
'mimeType' => $mimeType
]
);
// Display the translation for each input text provided
foreach ($response->getGlossaryTranslations() as $translation) {
printf('Translated text: %s' . PHP_EOL, $translation->getTranslatedText());
}
} finally {
$translationServiceClient->close();
}
}
v3_translate_text_with_glossary($text, $tl, $sl, $project_id, $glossary_id);
/////////////////////////////////////// ERROR RESPONCE /////////////////////////////
Error: Call to a member function getTag() on null: { "message": "Target language can't be equal to source language.", "code": 3, "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "field": "source_language_code", "description": "Source language: iw" }, { "field": "target_language_code", "description": "Target language: he" } ] } ] }
/////////////////////////////////// ERROR END ///////////////////////////////////////////////
How can I translate text in "en" to "en" OR "he" to "he" OR "he" to "iw"
Thanks
Ankit
what is the purpose of translating EN to EN?
As per error response it is not possible to that the target language equals the source language.
I am running a script for translation text and I have 4 languages installed ar, en, he, ru, and th
now I am detecting the language code of the text, suppose it gives me "iw"
and my website has "he", in this case, both language codes are the same because both language code point to Hebrew,
That's why I want the translation of same language to same language
Thanks
Ankit
If you translate iw (שלום) to he it will be still (שלום).
So there is no need to translate. That's why you get the error.