Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BASH example of translation using the AWS CLI
af
sq
am
ar
az
bn
bs
bg
zh
zh-TW
hr
cs
da
fa-AF
nl
et
fi
fr
fr-CA
ka
de
el
ha
he
hi
hu
id
it
ja
ko
lv
ms
no
fa
ps
pl
pt
ro
ru
sr
sk
sl
so
es
es-MX
sw
sv
tl
ta
th
tr
uk
ur
vi
#!/bin/bash
# Bourne Again Shell example of translation services using the AWS Translate
# service. Requires the AWS CLI tool and JQ to be present in the path.
#
# Directions, edit "langlist.txt" to include the desired languages from the
# following site: https://docs.aws.amazon.com/translate/latest/dg/what-is.html
#
# Next: Edit the "--text" option to include your text to be translated and
# the JQ filter to format the output. In the example below we are translating
# a string for use in translating the description of an application for use in
# a FreeDesktop `.desktop` file:
# https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
#
# Output from the service will be appended one line at a time to the file
# "translation_list.txt"
#
# Note: JQ (https://stedolan.github.io/jq/manual/) has a number of built in
# string formatters to make processing easier. To use them one begins the
# encapsulates the JSON field to be used with what I call a "half-escaped" set
# of parenthesis: \( ).
#
# For example, given the following JSON object:
# {
# "name": "Alice",
# "job": "Owner @ Alice's Restaurant",
# "profile": "http://example.com/alice",
# "likes": "dogs & cats"
# }
#
# This would be the output of various JQ filters:
# |--------------------------------------------------------------------------|
# | Filter: '@text "Check out \(.name) at \(.profile)"' |
# | Output: "Check out dog at http://example.com/dog" |
# |--------------------------------------------------------------------------|
# | Filter: '@html "\(.name) spends their time as \(.job)"' |
# | Output: "Alice spends their time as Owner @ Alice's Restaurant" |
# |--------------------------------------------------------------------------|
# | Filter: '@uri "http://example.com/search?s=\(.likes)"' |
# | Output: "http://example.com/search?s=dogs%20%26%20cats" |
# |--------------------------------------------------------------------------|
#
# It should be noted that the formatters treat the quoted non-field text as a
# string literal. As such the formatters will only interpolate the fields
# from the JSON object.
for lang in `cat langlist.txt`; do \
aws translate translate-text --region us-east-1 \
--source-language-code "en" \
--target-language-code "${lang}" \
--text "Emulator for Texas Instruments calculators" | \
jq -r '@text "Comment[\(.TargetLanguageCode | sub("-"; "_"))]=\(.TranslatedText);" ' | \
tee -a translation_list.txt;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment