Skip to content

Instantly share code, notes, and snippets.

@deeplook
Created March 8, 2022 10:30
Show Gist options
  • Save deeplook/32131749e69b4333da07de8e47eede50 to your computer and use it in GitHub Desktop.
Save deeplook/32131749e69b4333da07de8e47eede50 to your computer and use it in GitHub Desktop.
Snippet for using AWS Translate online service.
"""
Snippet for using AWS Translate online service.
See https://aws.amazon.com/translate/ for more information.
"""
import os
import boto3 # pip install boto3
# Load your AWS params from environment or add them here:
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "******")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "******")
AWS_REGION_NAME = os.getenv("AWS_REGION_NAME", "us-east-1")
def aws_translate(text, source_lang="auto", target_langs=[]):
"""Translate text from a source language to one ore more target languages.
Supported source and target languages need to be given as ISO 639-2 codes.
The source one can also be "auto".
Example:
>>> text = "The quick brown fox jumped over the lazy old dog."
>>> target_langs = "de fr es it ro bg uk ru ar ja ko zh".split()
>>> aws_translate(text, target_langs=target_langs)
en: The quick brown fox jumped over the lazy old dog.
de: Der schnelle braune Fuchs sprang über den faulen alten Hund.
fr: Le renard brun rapide a sauté par-dessus le vieux chien paresseux.
es: El rápido zorro marrón saltó sobre el perro viejo y perezoso.
it: La veloce volpe marrone saltò sopra il vecchio cane pigro.
ro: Vulpea brună rapidă a sărit peste câinele bătrân leneș.
bg: Бързата кафява лисица прескочи мързеливото старо куче.
uk: Швидка коричнева лисиця перестрибнула через ледачого старого пса.
ru: Быстрая коричневая лиса перепрыгнула через ленивую старую собаку.
ar: قفز الثعلب البني السريع فوق الكلب القديم الكسول.
ja: 素早い茶色のキツネは怠惰な老犬を飛び越えた。
ko: 빠른 갈색 여우가 게으른 늙은 개 위로 뛰어 올랐다.
zh: 那只快速的棕色狐狸跳过那只懒惰的老狗
"""
client = boto3.client("translate",
region_name=AWS_REGION_NAME,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
)
target_langs = [target_langs] if type(target_langs) == str else target_langs
for i, target_lang in enumerate(target_langs):
try:
res = client.translate_text(
Text=text,
SourceLanguageCode=source_lang,
TargetLanguageCode=target_lang,
)
except client.exceptions.UnsupportedLanguagePairException:
print(f'{source_lang}/{target_lang}: UnsupportedLanguagePairException')
continue
if i == 0:
lang_code = res["SourceLanguageCode"]
print(f'{lang_code}: {text}')
lang_code = res["TargetLanguageCode"]
trans = res["TranslatedText"]
print(f"{lang_code}: {trans}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment