We recently announced support for Alexa in two new languages, English (UK) and German. In order to easily add all three supported languages to your skills, we have updated the Alexa SDK for Node.js. We’ve also updated our Fact, Trivia and How To skill samples to include support for all three languages using the new SDK feature. You can find these updated samples over at the Alexa GitHub.
Fact – This skill helps you to create a skill similar to “Fact of the Day”, “Joke of the Day” etc. You just need to come up with a fact idea (like “Food Facts”) and then plug in your fact list to the sample provided.
Trivia – With this template you can create your own trivia skill. You just need to come up with the content idea (like “Santa Claus Trivia”) and plug in your content to the sample provided.
How To – This skill enables you to parameterize what the user says and map it to a content catalog. For example, a user might say "Alexa, Ask Aromatherapy for a recipe for focus" and Alexa would map the word "focus" to the correct oil combination in the content catalog.
If you are not familiar with the existing SDK or have not previously created a skill, you can reference the fact skill tutorial or read the SDK Getting Started Guide before continuing.
Let’s take a look at the new version of the fact skill, and walk through the added multi-language support. You can find the entire skill code here.
The first thing that you will notice is that we now define a resource object when configuring the Alexa SDK. We do this by adding this line within our skill handler:
alexa.resources = languageStrings;
languageStrings is the name of the object that contains our international resources. We need to define that too. You can name this whatever you wish. Here’s what it looks like:
var languageStrings = { "en-GB": { "translation": { "FACTS": [| "A year on Mercury is just 88 days long.", "The Moon is moving approximately 3.8 cm away from our planet every year." ], "SKILL_NAME": "British Space Facts", "GET_FACT_MESSAGE": "Here's your fact: ", "HELP_MESSAGE": "You can say tell me a space fact, or, you can say exit... What can I help you with?", "HELP_REPROMPT": "What can I help you with?", "STOP_MESSAGE": "Goodbye!" } }, "en-US": { "translation": { "FACTS": [ "A year on Mercury is just 88 days long.", "The Moon is moving approximately 3.8 cm away from our planet every year." ], "SKILL_NAME": "American Space Facts", "GET_FACT_MESSAGE": "Here's your fact: ", "HELP_MESSAGE": "You can say tell me a space fact, or, you can say exit... What can I help you with?", "HELP_REPROMPT": "What can I help you with?", "STOP_MESSAGE": "Goodbye!" } }, "de-DE": { "translation": { "FACTS": [ "Ein Jahr dauert auf dem Merkur nur 88 Tage.", "Der Mond entfernt sich von unserem Planeten etwa 3,8 cm pro Jahr." ], "SKILL_NAME": "Weltraumwissen auf Deutsch", "GET_FACT_MESSAGE": "Hier sind deine Fakten: ", "HELP_MESSAGE": "Du kannst sagen, \u201eNenne mir einen Fakt \u00fcber den Weltraum\u201c, oder du kannst \u201eBeenden\u201c sagen... Wie kann ich dir helfen?", "HELP_REPROMPT": "Wie kann ich dir helfen?", "STOP_MESSAGE": "Auf Wiedersehen!" } } }
The languageStrings object contains 3 objects, one for each supported language (en-US, en-GB and de-DE). The object names are identical to the value of the locale property that is passed to our skill when it is invoked by Alexa. This tells us what language model the end user’s device is configured to use so that we can respond with the appropriate language.
Each language has a translations object. This is where we specify any properties that are different for each language. For the fact sample, we have our array of facts in there, as well as a number of other skill attributes, such as skill name and various speech outputs.
Within the skill handlers, you need to respond with the relevant language to the user. Here is an example from the fact sample for the AMAZON.StopIntent.
'AMAZON.StopIntent': function () { this.emit(':tell', this.t("STOP_MESSAGE")); }
You can see here that when we emit the response for Alexa to relay to your users. Rather than returning a single response such as STOP_MESSAGE as we would in a single language skill, we use this.t(“STOP_MESSAGE”) which instructs the SDK to provide the STOP_MESSAGE property associated with the appropriate language. Here the SDK is calling a function that identifies the locale object in your skills request and then returns the appropriate variable from your earlier defined resources object.
This blog post explains how to implement multi-language support within the both the Alexa SDK for node.js and the Alexa Skills Kit samples. Both of these are available for you to use today and we are excited to see what you create. Please do keep us up to date on Twitter via @alexadevs.
Remember if you submit and publish a skill in any of the three supported languages by the end of the month, you can bag yourself one of our free limited edition Alexa Dev t-shirts. There are custom, limited edition designs for the US, UK and Germany. Quantities are limited. See terms and conditions.