Discovering Supported Languages

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

Sending a supported languages request

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

https://translation.googleapis.com/language/translate/v2/languages?parameters

One query parameter is required with each request:

  • API key: Use the key query parameter to identify your application.

All other query parameters are optional.

Getting a list of supported languages

Protocol

Here is an example to return a list of supported language codes:

https://translation.googleapis.com/language/translate/v2/languages?key=YOUR_API_KEY

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.

Go

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

public static void displaySupportedLanguages(PrintStream out, Optional<String> tgtLang) {
  Translate translate = createTranslateService();
  LanguageListOption target = LanguageListOption.targetLanguage(tgtLang.orElse("en"));
  List<Language> languages = translate.listSupportedLanguages(target);

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

Node.js

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

function listLanguages () {
  // Instantiates a client
  const translate = Translate();

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

      console.log('Languages:');
      languages.forEach((language) => console.log(language));

      return languages;
    });
}

PHP

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

use Google\Cloud\Translate\TranslateClient;

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

Python

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

    results = translate_client.get_languages()

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

Ruby

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

# 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.

https://translation.googleapis.com/language/translate/v2/languages?key=YOUR_API_KEY&target=zh-TW

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.

Go

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

public static void displaySupportedLanguages(PrintStream out, Optional<String> tgtLang) {
  Translate translate = createTranslateService();
  LanguageListOption target = LanguageListOption.targetLanguage(tgtLang.orElse("en"));
  List<Language> languages = translate.listSupportedLanguages(target);

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

Node.js

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

function listLanguagesWithTarget (target) {
  // The target language for language names, e.g.:
  // target = 'ru';

  // Instantiates a client
  const translate = Translate();

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

      console.log('Languages:');
      languages.forEach((language) => console.log(language));

      return languages;
    });
}

PHP

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

def list_languages_with_target(target):
    """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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

# 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

Send feedback about...

Google Cloud Translation API Documentation