Rishabh Tiwari
Rishabh Tiwari

Reputation: 15

API to pull the list of supported languages in AWS Translate

I am currently working on a project where I need to translate Customer comments into English from the source language on AWS. It is easy to do so using AWS Translate but before I call translate API to translate text into English, I want to check whether the source language is supported by AWS or not? One solution is to put all the language codes supported by AWS Translate into a list and then check source language against this list. This is easy but it is going to be messy and I want to make it more dynamic. So, I am thinking to code like this

import boto3

def translateUserComment(source_language):

    translate = boto3.client(service_name='translate', region_name='region', use_ssl=True)
    languages_supported = tanslate.<SomeMethod>()
    if source_language in languages_supported:
        result = translate.translate_text(Text="Hello, World", 
            SourceLanguageCode=source_language, TargetLanguageCode="en")
        print('TranslatedText: ' + result.get('TranslatedText'))
        print('SourceLanguageCode: ' + result.get('SourceLanguageCode'))
        print('TargetLanguageCode: ' + result.get('TargetLanguageCode'))
    else:
        print("The source language is not supported by AWS Translate")

Problem is that I am not able to find out any API call to get the list of languages/language codes supported by AWS Translate for place. Before I posted this question,

  1. I have tried to search for similar questions on stackoverflow
  2. I have gone through the AWS Translate Developer guide but still no luck

Any suggestion/ redirection to the right approach is highly appreciated.

Upvotes: 1

Views: 878

Answers (2)

Rub
Rub

Reputation: 2708

https://docs.aws.amazon.com/translate/latest/APIReference/API_ListLanguages.html

I use it (inside a function) like

    try:
        log.info("Fetching languages from AWS Translate")
        client = translator_client_aws
        response = client.list_languages(DisplayLanguageCode="en")
        return response["Languages"]
    except (BotoCoreError, ClientError) as error:
        msg = f"An error occurred: {error}"
        log.error(msg)
        raise

Which returns something like


{
│   'Languages': [
│   │   {
│   │   │   'LanguageName': 'Afrikaans',
│   │   │   'LanguageCode': 'af'
│   │   },
│   │   {
│   │   │   'LanguageName': 'Albanian',
│   │   │   'LanguageCode': 'sq'
│   │   },
│   │   {
│   │   │   'LanguageName': 'Amharic',
│   │   │   'LanguageCode': 'am'
│   │   },
│   │   {'LanguageName': 'Arabic', 'LanguageCode': 'ar'},
│   │   {
│   │   │   'LanguageName': 'Armenian',
│   │   │   'LanguageCode': 'hy'
│   │   },
...

As you can see in https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html

it says Amazon Translate supports text translation between the languages listed in the following table.

In other words, they use target_languages as source_languages as well.

I tested it with the examples below to make sure it works.

Note: translate_text_aws is my own wrapper (not public), but you get the idea.

t="Tengo jugo de manzana."
translate_text_aws(text=t,source="es-mx",target="es") # To Spanish (Spain)

# 'Tengo zumo de manzana.'

t="Tengo zumo de pera."
translate_text_aws(text=t,source="es",target="es-mx") # To Spanish (Mexico)

# 'Tengo jugo de pera.'

Upvotes: 0

KnowledgeGainer
KnowledgeGainer

Reputation: 1097

Currently there is no API for this service, although this code would work, in this code a class is created Translate_lang with all the language codes and country wise from here-> https://docs.aws.amazon.com/translate/latest/dg/what-is.html , you can call this class into your program and use it by creating an instance of the class:

translate_lang_check.py

class Translate_lang:
    def __init__(self):
        self.t_lang = {'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am',
                       'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az',
                       'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg',
                       'Catalan': 'ca', 'Chinese (Simplified)': 'zh',
                       'Chinese (Traditional)': 'zh-TW', 'Croatian': 'hr',
                       'Czech': 'cs', 'Danish': 'da ', 'Dari': 'fa-AF',
                       'Dutch': 'nl ', 'English': 'en', 'Estonian': 'et',
                       'Farsi (Persian)': 'fa', 'Filipino Tagalog': 'tl',
                       'Finnish': 'fi', 'French': 'fr', 'French (Canada)': 'fr-CA',
                       'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu',
                       'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hebrew': 'he ', 'Hindi': 'hi',
                       'Hungarian': 'hu', 'Icelandic': 'is', 'Indonesian': 'id ', 'Italian': 'it',
                       'Japanese': 'ja', 'Kannada': 'kn', 'Kazakh': 'kk', 'Korean': 'ko',
                       'Latvian': 'lv', 'Lithuanian': 'lt', 'Macedonian': 'mk', 'Malay': 'ms',
                       'Malayalam': 'ml', 'Maltese': 'mt', 'Mongolian': 'mn', 'Norwegian': 'no',
                       'Persian': 'fa', 'Pashto': 'ps', 'Polish': 'pl', 'Portuguese': 'pt',
                       'Romanian': 'ro', 'Russian': 'ru', 'Serbian': 'sr', 'Sinhala': 'si',
                       'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es',
                       'Spanish (Mexico)': 'es-MX', 'Swahili': 'sw', 'Swedish': 'sv',
                       'Tagalog': 'tl', 'Tamil': 'ta', 'Telugu': 'te', 'Thai': 'th',
                       'Turkish': 'tr', 'Ukrainian': 'uk', 'Urdu': 'ur', 'Uzbek': 'uz',
                       'Vietnamese': 'vi', 'Welsh': 'cy'}

    def check_lang_in_translate(self, given_country):
        if given_country in self.t_lang:
            return self.t_lang[given_country]
        else:
            return None

    def check_lang_code_in_translate(self, given_lang):
        if given_lang in list(self.t_lang.values()):
            return True
        else:
            return False

You can call and check your lang codes using the methods of this class:

from translate_lang_check import Translate_lang
tl = Translate_lang()
print(tl.check_lang_code_in_translate('en'))

Upvotes: 3

Related Questions