Text translation for Asterisk
Text translation and language detection for Asterisk and the command line using Azure AI Translator.
The AGI script translates text strings and returns translations or detected language codes as channel variables. A command-line script is also included. See the README for complete documentation.
Dependencies
- Perl, the Perl programming language
- perl-libwww, the World-Wide Web library for Perl
- perl-Crypt-SSLeay or perl-IO-Socket-SSL for TLS encryption
- JSON::PP, included with modern Perl
- URI::Escape for URI query parameter escaping
- Internet access to contact Azure and retrieve translation data
Create an Azure AI Translator resource and obtain its subscription key and, for a regional resource, its region.
Installation
To install, copy mstranslate.agi to your agi-bin directory, usually
/var/lib/asterisk/agi-bin/. Check /etc/asterisk/asterisk.conf to
confirm its location.
Set MSTRANSLATE_SUBSCRIPTION_KEY in the environment of the CLI or Asterisk process.
For a regional resource, also set MSTRANSLATE_SUBSCRIPTION_REGION; leave it unset
for a global resource. When using AGI, configure the Asterisk service so that the script inherits
these variables. Credentials are not accepted as command-line arguments.
Store credentials in a protected environment file, service manager configuration, or secret
manager rather than typing them into shell commands. Restrict any credential file to the
appropriate account, for example with mode 0600.
AGI usage
agi(mstranslate.agi,"text",[target language],[source language]):
- The text string is sent to Azure AI Translator for translation to the target language.
- The translated text is stored in the channel variable
mstranslation. - If the source language is omitted, the translator engine tries to auto-detect it.
-
If the target language is omitted, the script detects the language and stores its code in the
channel variable
mslang.
Language codes consist of a two- or three-letter language identifier followed by optional
hyphen-separated subtags containing 2 to 8 letters or digits, for example en,
fr, or zh-Hans.
CLI usage
perl cli/mstranslate-cli.pl -l en -o fr -t "Hello world" perl cli/mstranslate-cli.pl -o fr -t "Hello world" perl cli/mstranslate-cli.pl -t "Salut tout le monde" perl cli/mstranslate-cli.pl -v
The first command translates explicitly from English to French, the second auto-detects the
source language, the third detects the language and prints fr, and the fourth lists
supported languages. Run perl cli/mstranslate-cli.pl -h for all options and examples.
Asterisk dialplan examples
;Translate a text string from English to French:
exten => 1234,1,agi(mstranslate.agi,"This is some random text.",fr)
exten => 1234,n,Verbose(1,Translated text: ${mstranslation})
;Translate from Greek to German:
exten => 1234,1,agi(mstranslate.agi,"Αυτό είναι ένα απλό τέστ στα ελληνικά.",de)
exten => 1234,n,Verbose(1,Translated text: ${mstranslation})
;Translate from Japanese to English:
exten => 1234,1,agi(mstranslate.agi,"これは、日本の簡単なテストです。良い一日を。",en)
exten => 1234,n,Verbose(1,Translated text: ${mstranslation})
;Detect the language of a text string:
exten => 1236,1,Set(MYTEXT="This is some random text for detection.")
exten => 1236,n,agi(mstranslate.agi,"${MYTEXT}")
exten => 1236,n,Verbose(1,Detected language: ${mslang})
API and security
The scripts use version 3.0 of the
Azure AI Translator REST API
at api.cognitive.microsofttranslator.com. Both scripts use TLS to protect traffic in
transit between your system and Azure. AGI variable values are escaped, and values containing CR
or LF are rejected to protect the AGI command stream.
Download
Download the current source as a ZIP archive or a tar.gz archive, or clone the project with Git:
git clone https://github.com/zaf/asterisk-mstranslate.git
License
The mstranslate scripts for Asterisk are distributed under the GNU General Public License v2.
Author
Lefteris Zafiris (zaf@fastmail.com)