AWS Translate via the AWS CLI command line - error: 'charmap' codec can't encode characters

With the AWS CLI tool, it is possible to perform machine translation on the command line:

aws translate translate-text --region "eu-west-1" --source-language-code "en" --target-language-code "es" --text "hello, world"

OUTPUT:

{
    "TranslatedText": "hola, mundo",
    "SourceLanguageCode": "en",
    "TargetLanguageCode": "es"
}

notes:

1. Multiple AWS accounts via '--profile' option

- if you have multiple AWS accounts, you can add multiple credentials in your .aws/config file:

[default]
region = eu-west-1

[profile my-aws-1]
aws_access_key_id = xxx-1
aws_secret_access_key = yyy-1

[profile my-aws-2]
aws_access_key_id = xxx-2
aws_secret_access_key = yyy-2

- then, to use a particular AWS account, add the --profile option to AWS CLI tool:

aws translate translate-text --profile my-aws-1 --region "eu-west-1" --source-language-code "en" --target-language-code "es" --text "hello, world"

2. If you receive a character set error like this (especially if translating to a non-latin charachter set language like Spanish or Russian)

{ "TranslatedText": 'charmap' codec can't encode characters in position 1-6: character maps to <undefined>

Then, you need to set some environment variables to tell Python to use Unicode (the AWS CLI tool is written in Python):

On Windows:

set PYTHONIOENCODING=UTF-8
set PYTHONUTF8=1

3. It's also worth upgrading AWS CLI to the most recent version. 


Comments