Externalize your i18n in your Alexa Skill
Externalize your i18n in your Alexa Skill
In this article, we discuss how externalize the i18n to enhance your Alexa Skill development and mantenance.
Join the DZone community and get the full member experience.Join For Free
In computing, internationalization is the process of designing software so that it can be adapted to different languages and regions without the need of re-engineering or making changes in code.
In the case of software, it means to translate into several languages, using different currencies or using different date formats.
In terms of Alexa Skills, it is a good practice having the translations separated from the AWS Lambda source code. There are many tools that can help us in this process. We will guide you in this process using POEditor.
Here you have the technologies used in this project.
- Amazon Developer Account - How to get it
- AWS Account - Sign up here for free
- ASK CLI - Install and configure ASK CLI
- AWS CLI - Install and configure AWS CLI
- POEditor Account - Register here
- Node.js v10.x
- Java Runtime Environment (JRE) version 6.x or newer
- Visual Studio Code
- npm Package Manager
The Alexa Skills Kit Command Line Interface (ASK CLI) is a tool for you to manage your Alexa skills and related resources, such as AWS Lambda functions. With ASK CLI, you have access to the Skill Management API, which allows you to manage Alexa skills programmatically from the command line. If you want how to create your Skill with the ASK CLI in Node.js using DynamoDB on local machine, please follow the steps explained in my repository here sample. Let's start!
POEditor is an online localization platform and translation management system, designed for teams to collaborate easily, but also suited for individuals. You can use POEditor to translate apps, websites, themes, plugins, games or other software products, and to automate the localization workflow. Supported most of the l18n file formats. They also have a really nice and easy-to-use RESTFul API.
What POEditor offer to us?
- Simple REST API Bring POEditor to home base by connecting it to your software via our simple API. Let the API automate your localization workflow and forget about manually managing your localization projects.
- GitHub, Bitbucket, GitLab and Azure DevOps integration Connect your repos to your POEditor account to quickly communicate data between our localization platform and GitHub/Bitbucket/GitLab/Azure Devops. Use the WordPress translation plugin to manage the localization of your language files directly in WordPress, via the POEditor API.
- Slack and Microsoft Teams integration Connect your POEditor account to Slack or Microsoft Teams and never miss any important events during the localization process. Add your localization team members to dedicated channels, and you'll always be on the same page, in real time.
- Crowdsourced translation projects Set your software localization project to "Public" to crowdsource the translation of your strings. You will get a link that you can share in order to invite people to enroll as contributors.
- Rich statistics The stats page offers real-time information about your translators' activity, the amount of terms, translations, words and characters belonging to a localization project, as well as charts and the percentage of completion for each language
- Smart Translation Memory Don't like to translate strings multiple times? Then you will enjoy using the Translation Memory feature. The TM searches through all your previous translations so you can reuse them.
- Realtime translation updates If collaborative software localization sounds like a mess, relax. POEditor lets you know of your fellow translators' whereabouts when you're sharing the same language page, and automatically saves any changes you make to the translation.
- Human translation services Purchase professional human translations for your localization project directly from your POEditor account. We've neatly aggregated the translation providers we are partners with, for you to easily compare and integrate into your continuous localization workflow.
- Automatic Translation Automatic Translation offers the possibility to work with the machine translation engines from Google, Microsoft and DeepL, in order to automatically fill in missing translations.
Once we have signed up on POEditor, now it is time to create a new project for our Alexa Skill. It is a good practice to have one POEditor project per Skill.
We have created one called
test-skill that contains 15 strings and it is available in Spanish and English:
As you can see above, we have a quick look at the status of our projects. We can see we have the 57% of our project translated.
If we click on our project, we will see the status of that project:
In this view we can see we have 15 strings in 2 languages with 7 terms to translate and only 8 available translations. Below this it is available the information per language, we have all the Spanish translations but in English, we only have the 14%, it means that in English we only have one translation done.
In this page you can make a bulk import, add terms to translate, add new languages, show the stats of the translations and setting up some configuration of this project like permissions, contributors, code hosting service integration, make it open source, etc.
And finally if we click in a language, e.g. Spanish, we will see the translations:
Here you can make a bulk import as well, export the current language translations and execute the translation memory tool. If you do not have all the translation of this language you can activate the automatic translation (It will use Google, Microsoft or DeepL to make that).
So, now we have the project properly configured. To download the translations, we will use the POEditor API to download the terms and all translations of one specific language:
This is an example of the request:
This is an example of the response:
Understanding how the API works, it is time to integrate it into our Alexa Skill.
We will use the following flow to download the translations:
The following code located in
utilities/i18nUtils.js will execute the same as we explained in the flow diagram:
In order to make in an easy way a
x-www-form-urlencoded POST request in Node.js, we will use the npm package
This code will be called from the
Now we have the translations downloaded in our Alexa Skill, it is time to use it there!
I have removed the
i18next npm package because we will not use it anymore. Now we will have our own translation function.
This function will be search a term in the translations downloaded before. It is located in
i18nUtils.js as well:
Now we can call this function wherever we want in our Alexa Skill:
This function supports string replacement like the
i18next package. In this case, I am using the npm package
In order to not to download the translations every time we run the Skill, we will persist them to DynamoDB following this flow:
This flow is implemented on the
launch.json file in
.vscode folder has the configuration for Visual Studio Code which allow us to run our lambda locally:
This configuration file will execute the following command:
For a new incoming skill request a new socket connection is established. From the data received on the socket the request body is extracted, parsed into JSON and passed to the skill invoker's lambda handler. The response from the lambda handler is parsed as a HTTP 200 message format as specified here The response is written onto the socket connection and returned.
After configuring our launch.json file and understanding how the local debugger works, it is time to click on the play button:
After executing it, you can send Alexa POST requests to http://localhost:3001.
NOTE: If you want to start the local DynamoDB you have to set to
true the environment variable
DYNAMODB_LOCAL in this file.
Following the steps before, now you can set up breakpoints wherever you want inside all JS files in order to debug your skill.
In my post talking about Node.js Skill you can see how to test your Skill either directly with Alexa Developer Console or locally with Postman.
When we are running the DynamoDB locally, this local instance we will set up a shell in http://localhosta:8000/shell
In that shell we can execute queries in order to check the content of our local database. These are some example of queries you can do:
- Get all the content of our table:
Then we can show the data of the table:
- Get the information of our table:
Now we can show the information of our table:
This local DynamoDB is accessible by the AWS CLI as well. Before using the CLI, we need to create a
fake profile that will use the region, accessKeyId and secretAccessKey used by our local database and client. So in our
~/.aws/credentials we have to create the
And in our
~/.aws/config we set the local region for our
After creating it, now we can execute queries using the AWS CLI using our
This command will return a list of tables in our local database:
You can find more information about how to make queries with the AWS CLI here
Internationalization takes place as a fundamental step in the design and development process, rather than as a subsequent addition, which can often involve a difficult and expensive process of reengineering. We have to take the correct decision in terms of i18n in the beginning of our development process.
POEditor is freemium and you can start for free with 1,000 strings. POEditor is not so expensive, with very affordable prices. You can check it here.
I hope this example project is useful to you.
You can find the code in my GitHub.
That's all folks!
Opinions expressed by DZone contributors are their own.