何これ?
最近短いシェルスクリプトを書いたら、意外と使用頻度が高かったのでメモっておく。Amazon Translateをコマンドラインから使うだけのもの。
誰向け?
俺。その他、以下のような人
- 英語を頻繁に読み書きしないといけない人
-
aws
とかjq
コマンドが入っているややインフラな人 - 翻訳のためにわざわざターミナルから離れたくない人
translate.sh
getopts "j:e:" OPT
case $OPT in
j) SOURCE=en; TARGET=ja; S_FLAG="🇺🇸"; T_FLAG="🇯🇵" ;;
e) SOURCE=ja; TARGET=en; S_FLAG="🇯🇵"; T_FLAG="🇺🇸" ;;
*) echo "Usage: $0 {-e|-j} <sentence>"; exit; ;;
esac
REGION=ap-northeast-1
echo "$S_FLAG → $T_FLAG"
aws translate translate-text \
--region $REGION \
--source-language-code "$SOURCE" \
--target-language-code "$TARGET" \
--text "$OPTARG" \
| jq -r .TranslatedText
使い方
シェルのエイリアスを以下のように設定
ej='/path/to/translate.sh -j'
je='/path/to/translate.sh -e'
% je "あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。"
🇯🇵 → 🇺🇸
That strong wind from Ihatovo, the blue sky that is cold at the bottom even in summer, the city of Molio decorated with beautiful forests, and the glaring grass waves in the suburbs.
% ej "That strong wind from Ihatovo, the blue sky that is cold at the bottom even in summer, the city of Molio decorated with beautiful forests, and the glaring grass waves in the suburbs."
🇺🇸 → 🇯🇵
イハトヴォからの強風、夏でも底が冷たい青空、美しい森に彩られたモリオの街、郊外のまぶしい草の波。
学び
イーハトーブとは、宮沢賢治による造語で、賢治の心象世界中にある理想郷を指す言葉である。
Cheers, 🍻