Learn how to add support for a language that isn't listed on the official artyom languages.

How to add a language that is not totally supported by Artyom.js

Artyom.js is an useful library that allows you to implement some kind of little assistant that is powered by the Speech Recognition and Speech Synthesis of Google Chrome. You can add with JavaScript voice commands either with plain text or with the help of Regular Expressions. A command is triggered when the spoken text by the user matches any of the providen options inside a command, triggering finally a function that determines what the commands does.

Originally and till the date, Artyom.js supports 13 languages. The "support" of this library for these languages, is based on the availability of the Speech Recognition API of Google for a voice (either native or from Google) for the language in the Browser. The only problem is, that itself the Webkit Speech Recognition currently supports more than 40 languages in Chrome. Some languages have specifical codes according to the region (the identifiers follow the BCP-47 format). For more information about which regions and language codes are available, please read this article.

In this article you will learn how to extend the support of Artyom for other languages.

Requirements

You need 2 things to extend Artyom:

A voice installed for your language

The first one is that your browser indeed has a voice for your language, for example if your language is Turkish, your browser needs to have at least a native voice installed (of your operative system) and the language code needs to match the one listed in the supported languages by Webkit Speech Recognition. 

You can use the following snippet with Artyom to list all the voices in the console as a string:

let voiceResults = [];

let artyom = new Artyom();

// or if you don't want to use artyom
//let voices = speechSynthesis.getVoices();
let voices = artyom.getVoices();

voices.forEach((voice) => {
    voiceResults.push({
        voiceURI: voice.voiceURI,
        name: voice.name,
        lang: voice.lang
    });
});

console.log(
    JSON.stringify(voiceResults, null , 5)
);

// Outputs something like
//[
//     {
//          "voiceURI": "Microsoft Zira Desktop - English (United States)",
//          "name": "Microsoft Zira Desktop - English (United States)",
//          "lang": "en-US"
//     },
//    {
//          "voiceURI": "Google Deutsch",
//          "name": "Google Deutsch",
//          "lang": "de-DE"
//     },
//     {
//          "voiceURI": "Google US English",
//          "name": "Google US English",
//          "lang": "en-US"
//     },
//     etc...

In many OS you can install extra voices, so don't forget to take a look in Google about it.

Speech Recognition API support for your language

The second one, is that the Speech Recognition API of Google supports your language, so you may want to verify it by checking the list of supported languages by the API here.

If one of the listed items in the voices has the language code of your language (the language that Artyom doesn't support) and the Speech Recognition of Google supports your language, then you can proceed to extend Artyom with your language !

Extending Artyom with a new language

To add a new language to Artyom, you only need to know the code of the language you want to use. For example, if we want to add the Turkish language, our code would be tr-TR. This language needs to be set as key in the ArtyomVoicesIdentifier object of your instance of Artyom, which as value receives an array with the language code (or codes) of the voice installed in your computer:

const myAssistant = new Artyom();

// The code language that will be used to initialize artyom
// with Speech Recognition
let newLanguageIdentifier = "tr-TR";

// An array with the possible code languages of the installed voice
// on your device (or voiceURI or name)
let possibleVoiceLangIdentifiers = ["tr-TR", "tr_TR"];

// This will add support to Turkish for Voice Commands
// and (only if a voice available for turkish in the browser) for speech synthesis
myAssistant.ArtyomVoicesIdentifiers[newLanguageIdentifier] = possibleVoiceLangIdentifiers;

// Add a command in Turkish
myAssistant.addCommands({
    indexes: ["günaydın"],
    action: () => {
        myAssistant.say("Günaydın, bugün nasılsın?");
    }
})

// Start Artyom in Turkish
myAssistant.initialize({
    lang: newLanguageIdentifier,
    // rest of your initialization code !
});

With the previous snippet, an installed voice in your operative system and support for your language in the Google Speech Recognition API you can now add a new language to artyom easily !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors