Search code examples
phpgoogle-cloud-platformgoogle-translategoogle-translation-api

Google Cloud Translation API - different language codes but API states they're the same


I've been using Cloud Translation with this package for a months now and suddenly it stared throwing weird exception:

Google\ApiCore\ApiException: {
    "message": "Target language can't be equal to source language.",
    "code": 3,
    "status": "INVALID_ARGUMENT",
    "details": [
        {
            "@type": 0,
            "data": "type.googleapis.com\/google.rpc.BadRequest"
        },
        {
            "@type": 0,
            "data": [
                {
                    "field": "source_language_code",
                    "description": "Source language: pol"
                },
                {
                    "field": "target_language_code",
                    "description": "Target language: por"
                }
            ]
        }
    ]
}

As you can see, language codes are clearly different. That happens with any language pair.

Environment details

  • OS: Ubuntu 20.04 (LTS) x64
  • PHP version: 7.4.24
  • Package name and version: google/cloud-translate v1.10.1

Code example

$client = new TranslationServiceClient();
$projectId = self::key(true)->project_id;
$formattedParent = $client::locationName($projectId, 'us-central1');

$client->translateText(
    ['Test'],
    'en',
    $formattedParent,
    ['sourceLanguageCode' => 'pl']
)->getTranslations();

Anybody else ran into this error and knows a fix?


Solution

  • I changed all language codes from 2 letters to 3 letters - from pl, de, en to pol, ger, eng. Now it works.