Discovering supported languages (v3beta1)

This document describes how to use the Cloud Translation API to list supported languages using Cloud Translation v3beta1.

Sending a supported languages request

You can discover the supported languages of this API by sending an HTTP request using a URL of the following format:

https://translation.googleapis.com/v3beta1/projects/project-id/locations/location/supportedLanguages

Getting a list of supported languages

curl command

To get a list of all supported languages, make a GET request to the https://translation.googleapis.com/v3beta1/projects/<var>project-id</var>/locations/<var>location</var>/supportedLanguages URL. The following shows an example of a GET request using curl. The example uses the access token for a service account set up for the project using the Google Cloud Platform Cloud SDK. For instructions on installing the Cloud SDK, setting up a project with a service account, and obtaining an access token, see the Quickstart.

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    "https://translation.googleapis.com/v3beta1/projects/project-id/locations/global/supportedLanguages"

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

{
  "languages": [
     "languageCode": "af",
      "supportSource": true,
      "supportTarget": true
    },
    {
      "languageCode": "am",
      "supportSource": true,
      "supportTarget": true
    },
    {
      "languageCode": "ar",
      "supportSource": true,
      "supportTarget": true
    },
    ....
    {
      "languageCode": "zu",
      "supportSource": true,
      "supportTarget": true
    }
   ]
}

The list is sorted alphabetically by language code. This query returns iso-639-1 language codes for supported languages. Some language codes also include a country code, like zh-CN or zh-TW. For example:

   {
      "languageCode": "zh-TW",
      "supportSource": true,
      "supportTarget": true
    },

Java

Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries . For more information, see the Translation Java API reference documentation .

static SupportedLanguages listSupportedLanguages(String projectId, String location) {
  try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {

    LocationName locationName =
        LocationName.newBuilder().setProject(projectId).setLocation(location).build();
    GetSupportedLanguagesRequest getSupportedLanguagesRequest =
        GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).build();

    // Call the API
    ApiFuture<SupportedLanguages> future =
        translationServiceClient
            .getSupportedLanguagesCallable()
            .futureCall(getSupportedLanguagesRequest);

    SupportedLanguages response = future.get();
    List<SupportedLanguage> languages = response.getLanguagesList();
    for (SupportedLanguage language : languages) {
      System.out.printf("Code: %s\n", language.getLanguageCode());
    }

    return response;

  } catch (Exception e) {
    throw new RuntimeException("Couldn't create client.", e);
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries . For more information, see the Translation Node.js API reference documentation .

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;

// Instantiates a client
const translationClient = new TranslationServiceClient();

async function listLanguages() {
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, location),
  };

  // Run request
  const [response] = await translationClient.getSupportedLanguages(request);

  console.log(`Supported languages:`);
  for (const language of response.languages) {
    console.log(`Language Code: ${language.languageCode}`);
  }
}

listLanguages();

Python

Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries . For more information, see the Translation Python API reference documentation .

from google.cloud import translate_v3beta1 as translate
client = translate.TranslationServiceClient()

# project_id = YOUR_PROJECT_ID
location = 'global'

parent = client.location_path(project_id, location)

response = client.get_supported_languages(parent)

print('Supported Languages:')
for language in response.languages:
    print('Language Code: {}'.format(language.language_code))

Listing supported languages with target language name

curl command

Here is another example that returns the list of supported languages with the returned language names written in the specified target language. The returned list is sorted alphabetically according to the target language.

To detect the language of some text, make a GET request and provide JSON that identifies the target language in the request body. The following shows an example of a GET request using curl.

curl -X GET \
     -H "Authorization: Bearer `gcloud auth application-default print-access-token`" \
     -H 'Content-Type: application/json'
     "https://translation.googleapis.com/v3beta1/projects/project-id/locations/global/supportedLanguages?display_language_code=sq"

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

{
  "languages": [
   {
      "languageCode": "af",
      "displayName": "Afrikanisht",
      "supportSource": true,
      "supportTarget": true
    },
    {
      "languageCode": "am",
      "displayName": "Amarikisht",
      "supportSource": true,
      "supportTarget": true
    },
    {
      "languageCode": "en",
      "displayName": "Anglisht",
      "supportSource": true,
      "supportTarget": true
    },
        ...
    {
      "languageCode": "zu",
      "displayName": "Zulu",
      "supportSource": true,
      "supportTarget": true
    }

  ]
}

In this case, the query returns the same language codes as above, along with name strings that give the names of the languages written in the target language, sq.

Java

Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries . For more information, see the Translation Java API reference documentation .

static SupportedLanguages listSupportedLanguagesWithTarget(String projectId, String location) {
  try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {

    LocationName locationName =
        LocationName.newBuilder().setProject(projectId).setLocation(location).build();
    GetSupportedLanguagesRequest getSupportedLanguagesRequest =
        GetSupportedLanguagesRequest.newBuilder()
            .setParent(locationName.toString())
            .setDisplayLanguageCode("en-US")
            .build();

    // Call the API
    ApiFuture<SupportedLanguages> future =
        translationServiceClient
            .getSupportedLanguagesCallable()
            .futureCall(getSupportedLanguagesRequest);

    SupportedLanguages response = future.get();
    List<SupportedLanguage> languages = response.getLanguagesList();

    for (SupportedLanguage language : languages) {
      System.out.printf(
          "Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode());
    }
    return response;

  } catch (Exception e) {
    throw new RuntimeException("Couldn't create client.", e);
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries . For more information, see the Translation Node.js API reference documentation .

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;

// Instantiates a client
const translationClient = new TranslationServiceClient();

async function listLanguages() {
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, location),
    displayLanguageCode: 'fr',
  };

  // Run request
  const [response] = await translationClient.getSupportedLanguages(request);

  console.log(`Supported languages:`);
  for (const language of response.languages) {
    console.log(`Language Code: ${language.languageCode}`);
    console.log(`Display Name: ${language.displayName}`);
  }
}

listLanguages();

Python

Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries . For more information, see the Translation Python API reference documentation .

from google.cloud import translate_v3beta1 as translate
client = translate.TranslationServiceClient()

# project_id = YOUR_PROJECT_ID
# display_language_code = 'is'
location = 'global'

parent = client.location_path(project_id, location)

response = client.get_supported_languages(
    parent=parent,
    display_language_code=display_language_code)

print('Supported Languages:')
for language in response.languages:
    print('Language Code: {}'.format(language.language_code))
    print('Display Name: {}\n'.format(language.display_name))

Оцените, насколько информация на этой странице была вам полезна:

Оставить отзыв о...

Текущей странице
Cloud Translation
Нужна помощь? Обратитесь в службу поддержки.