I’ve been using Twilio’s API for a while now on a variety of projects, and finally decided to enter one of their cool contests. Now, the holidays aren’t really my thing… I don’t celebrate much and when I do I don’t make a big deal out of it, but this contest got me thinking about how people celebrate holidays. It is often a very insular activity, confined to one’s own cultural or family group. But in reality, people all over the world celebrate the same holidays, albeit in different languages and fashions. So I thought it would be a neat idea to give people a quick and easy way to learn holiday greetings and facts from around the world so that their holiday celebrations didn’t have to be so insular. Lofty goal for a small application like this, but it sure was fun to write!

On to the technical aspects…
I decided that since it was a basic concept demo I would scope it out in two basic PHP files – holidaytranslator_sms.php and holidaytranslator_voice.php – both have similar functions but are simply requests for different parts of the Twilio API (SMS/Voice).
I started out trying to decide how I would go about handling the translation aspect of the application. The logical choice for me was Google Translate, since I am familiar with RESTful APIs and with Google Translate’s capabilities. I initially started out using the open source PHP wrapper gtranslate-api-php, but soon decided that since this wrapper uses v1 of the API, that I would instead experiment with v2. The main difference between the two versions is that while v1 is a JavaScript API, v2 is RESTful. Both return JSON and have essentially the same functionality.
My next step was to create a list of languages that it was possible to translate into. I borrowed the languages.ini file from gtranslate-api-php for this purpose. Using PHP’s parse_ini_file function, I turn this INI file into an array and use array_rand to select one of the elements, in this case a random language.

$available_languages = parse_ini_file(“languages.ini”);

$language_key = array_rand($available_languages);
$language = urlencode(strtolower($available_languages[$language_key]));

Pretty straight-forward, I parse the file into an array, get a random key from that array, and then get the corresponding value. To make sure Google will receive it correctly, I force it into lowercase and then urlencode the value for the GET request.
Using the Google Translate API was not much more complicated. The easiest way I found to use it is to form a URL with all of your required parameters, and then use file_get_contents as a way to send the request and receive the output.

$url = “https://www.googleapis.com/language/translate/v2?key=”.$apikey.”&source=en&target=”.$language.”&q=”.$translate_string;
$response = json_decode(file_get_contents($url));
$translation = $response->data->translations[0]->translatedText;

So the only parameters I am providing are my API Key from Google’s handy API Console, the source language (always English), the target language (selected randomly), and the string to translate. I then get back a JSON object and pull out the translated text. This worked like a charm, and I continued on to form my message and my TwiML.
The messaging worked as expected, but I quickly discovered that my phone cannot view foreign characters in a TXT message, and simply turned them into ?s. This was an unfortunate turn of events, but a quick Google Search turned up a function someone had written on PHP.net called normalize that translated foreign characters into the most similar English equivalent. Unfortunately, I could not find any straightforward way of doing the same for Asian languages, and ended up removing them from the possible random language list.
For the random holiday fact, I searched around and compiled some small .txt files with one fact per line. To get a random fact, I used array_rand again after using the file function to bring all of the lines into an array. And voila, TXT message support!
The voice application was very similar. I used the same fact files and Google Translate API. However, I started out attempting to use the somewhat hidden Google Translate_TTS API as detailed here. Unfortunately, Google refused all requests from Twilio’s command to its translate_tts API. I then decided to just translate the phrases in the same way as TXT messages, and simply have Twilio them.
After experimenting with a bit, I realized that using when asking for a phrase in any non-English language butchered the phrase. In an application whose purpose was to break language barriers, this would be useless. To make the voice application more usable, I ended up restricting the translator to Spanish, French, and German – the three languages that Twilio had support for. Now the translated greetings sound as they should!
TL;DR I wrote a PHP application using Twilio’s TwiML API and Google Translate to send users a random translation of their favorite holiday greeting plus an interesting fact about that holiday.
Application is up at github

Leave a Comment