Discovering Supported Languages

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

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/language/translate/v2/languages

Getting a list of supported languages

Protocol

To get a list of all supported languages, make a GET request to the https://translation.googleapis.com/language/translate/v2/languages endpoint. 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/language/translate/v2/languages"

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

{
  "data": {
    "languages": [
      {
        "language": "en"
      },
      {
        "language": "fr"
      },
      ...
      {
        "language": "zh-CN"
      }
    ]
  }
}

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. The list is sorted alphabetically by language code.

C#

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

using Google.Cloud.Translation.V2;
using System;
using System.Collections.Generic;

public partial class TranslateSample
{
    public IList<Language> ListLanguageCodes()
    {
        TranslationClient client = TranslationClient.Create();
        IList<Language> response = client.ListLanguages();
        foreach (var language in response)
        {
            Console.WriteLine(language.Code);
        }
        return response;
    }
}

Go

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

func listSupportedLanguages(w io.Writer, targetLanguage string) error {
	ctx := context.Background()

	lang, err := language.Parse(targetLanguage)
	if err != nil {
		return err
	}

	client, err := translate.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	langs, err := client.SupportedLanguages(ctx, lang)
	if err != nil {
		return err
	}

	for _, lang := range langs {
		fmt.Fprintf(w, "%q: %s\n", lang.Tag, lang.Name)
	}

	return nil
}

Java

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

// TODO(developer): Uncomment these lines.
// import com.google.cloud.translate.*;
// Translate translate = TranslateOptions.getDefaultInstance().getService();

List<Language> languages = translate.listSupportedLanguages();

for (Language language : languages) {
  System.out.printf("Name: %s, Code: %s\n", language.getName(), language.getCode());
}

Node.js

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

// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');

// Creates a client
const translate = new Translate();

// Lists available translation language with their names in English (the default).
translate
  .getLanguages()
  .then(results => {
    const languages = results[0];

    console.log('Languages:');
    languages.forEach(language => console.log(language));
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

PHP

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

use Google\Cloud\Translate\TranslateClient;

$translate = new TranslateClient();
foreach ($translate->languages() as $code) {
    print("$code\n");
}

Python

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

"""Lists all available languages."""
translate_client = translate.Client()

results = translate_client.get_languages()

for language in results:
    print(u'{name} ({language})'.format(**language))

Ruby

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

# project_id = "Your Google Cloud project ID"

require "google/cloud/translate"

translate = Google::Cloud::Translate.new project: project_id
languages = translate.languages

puts "Supported language codes:"
languages.each do |language|
  puts language.code
end

Listing supported languages in target language

Protocol

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

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

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'target': 'zh-TW'
}" "https://translation.googleapis.com/language/translate/v2/languages"

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

{
  "data": {
    "languages": [
      {
        "language": "zh-CN",
        "name": "中文(簡體)"
      },
      {
        "language": "fr",
        "name": "法文"
      },
      ...
      {
        "language": "en",
        "name": "英文"
      }
    ]
  }
}

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, zh-TW.

C#

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

using Google.Cloud.Translation.V2;
using System;
using System.Collections.Generic;

public partial class TranslateSample
{
    public IList<Language> ListLanguageNames()
    {
        TranslationClient client = TranslationClient.Create();
        // List all the languages in English.
        IList<Language> languages = client.ListLanguages(target: "en");
        foreach (var language in languages)
        {
            Console.WriteLine($"{language.Code}\t{language.Name}");
        }
        return languages;
    }
}

Go

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

func listSupportedLanguages(w io.Writer, targetLanguage string) error {
	ctx := context.Background()

	lang, err := language.Parse(targetLanguage)
	if err != nil {
		return err
	}

	client, err := translate.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	langs, err := client.SupportedLanguages(ctx, lang)
	if err != nil {
		return err
	}

	for _, lang := range langs {
		fmt.Fprintf(w, "%q: %s\n", lang.Tag, lang.Name)
	}

	return nil
}

Java

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

// TODO(developer): Uncomment these lines.
// import com.google.cloud.translate.*;
// Translate translate = TranslateOptions.getDefaultInstance().getService();

List<Language> languages = translate.listSupportedLanguages(
        Translate.LanguageListOption.targetLanguage("es"));

for (Language language : languages) {
  System.out.printf("Name: %s, Code: %s\n", language.getName(), language.getCode());
}

Node.js

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

// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');

// Creates a client
const translate = new Translate();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const target = 'The target language for language names, e.g. ru';

// Lists available translation language with their names in a target language
translate
  .getLanguages(target)
  .then(results => {
    const languages = results[0];

    console.log('Languages:');
    languages.forEach(language => console.log(language));
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

PHP

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

use Google\Cloud\Translate\TranslateClient;

// $targetLanguage = 'en'; // Print the names of the languages in which language?

$translate = new TranslateClient();
$result = $translate->localizedLanguages([
    'target' => $targetLanguage,
]);
foreach ($result as $lang) {
    print("$lang[code]: $lang[name]\n");
}

Python

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

"""Lists all available languages and localizes them to the target language.

Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
translate_client = translate.Client()

results = translate_client.get_languages(target_language=target)

for language in results:
    print(u'{name} ({language})'.format(**language))

Ruby

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

# project_id = "Your Google Cloud project ID"

# To receive the names of the supported languages, provide the code
# for the language in which you wish to receive the names
# language_code = "en"

require "google/cloud/translate"

translate = Google::Cloud::Translate.new project: project_id
languages = translate.languages language_code

puts "Supported languages:"
languages.each do |language|
  puts "#{language.code} #{language.name}"
end

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Translation API
Need help? Visit our support page.