API to pull the list of supported languages in AWS Translate

764 views Asked by At

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.

1

There are 1 answers

0
KnowledgeGainer On

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'))