Getting Started with the Microsoft Translator for PHP Developers
Though language differences may still hinder worldwide communication, Microsoft has worked consistently to bridge the gap by increasing the quality and accessibility of machine translators. These automated interpreters are fast becoming necessary tools to increase accessibility to products, services, and global social networking. How can developers implement the Microsoft Translator into a website or application? There are three easy techniques for PHP developers: the Web widget, AJAX, and HTTP methods.
The Translator Web Widget – Translating a Site
Using the AJAX API – Translating a Block of Text
Using the HTTP API – Detecting the Language of a Block of Text
The Translator Web Widget is the perfect tool for translating an entire site. The sample below shows the widget translating a web page from English to traditional Chinese. Additionally, when the cursor is floated over a portion of the translated text, a window appears showing the reverse translation back into English.
The site is now accessible to people speaking multiple languages.
In this scenario, the page design called for four different languages to be available at the top of the page, and for the language names to each appear in their own language. When the user clicks on the language name, the page is quickly translated into the selected language.
To create a page that utilizes the Translator AJAX API in such a way, you must first obtain an appId for your project to access the Translator. Generate the appId by going to http://www.microsofttranslator.com/Dev/Ajax.
Paste the generated script within the header section of the code for your web project. Next, add this code within a script tag to translate the HTML page (in this case from English to Spanish)
// Tell the API to translate from English into Spanish
Microsoft.Translator.translate(document.body, "en", "es");
Add a clickable link to the page to enable the user to translate the content.
<a onclick="translatePage();" style="cursor: pointer; cursor: hand; text-decoration: underline; color: #0000FF;">Español</a>
By hard-coding the original and resulting languages as shown above, the translator is being told the exact languages to translate from and to. If no values were set for these parameters, the translator would use language identification technology to decipher the originating language.
<a onclick="translatePage(french);" style="cursor: pointer; cursor: hand; text-decoration: underline; color: #0000FF;"> Français</a>
The following shows examples for Spanish, German, English, and the French variable mentioned above.
// Set the languages
var currentLanguage = "en";
var spanish = "es";
var german = "de";
var english = "en";
var french = "fr";
Microsoft.Translator.translate(document.body, currentLanguage, language);
currentLanguage = language;
The HTTP API is easy to assemble and is therefore useful for more dynamic applications. Since the HTTP method can work in conjunction with any programming language and operating system, this option is useful in many scenarios. The following HTTP example demonstrates how the Microsoft Translator can be used to detect the written language of a page using PHP. The demonstration begins with obtaining the necessary HTTP API application ID, then demonstrations the writing of the code for the sample and proceeds to creation of the interface.
Before beginning, you must obtain a Live Search API App ID. Go to http://search.live.com/developers/appids.aspx, sign in with your Windows Live ID, and follow the instructions to generate the appId.
Use the generated text in place of the highlighted yourappIdhere in the code snippets below.
The below example handles the HTTP query and response to and from the Microsoft Translator web service, and displays it on the page. Begin a new PHP page and call it HTTPDemo.php. Create a POST form and add a textbox, label, and a submit button to the page. Do not add a POST action because you want the page to post back to itself.
<p>Enter text of the language you would like to detect:
<p><input type="text" name="language" /></p>
<p><input type="submit" /></p>
Now add the code to handle the language detect functionality. Enter the PHP tags below the form and create a PHP function to detect when the language textbox has been posted back. Within the function, add a query variable that receives the text from the textbox.
if ($_POST['language'] == true)
$query = urlencode($_POST['language']);
Once the query variable has been created above, add a new variable that will receive the response from the HTTP request. Use the below code which does a request to the HTTP Translator web service, but replace yourappIdhere with the appId generated at the start of this section.
Append the query variable that contains the textbox input to the end of the web service URL (the text sent to be detected). Finally, add an echo statement that will display the contents of the result returned from the request.
$res = implode('', file('http://api.microsofttranslator.com/V1/Http.svc/Detect?AppId=yourappIdhere&text='.$query));
echo "The detected language is: ".$res;
When text is typed into the textbox, the end result should appear like the following:
The above demonstration shows how PHP can be combined with an HTTP call to the Microsoft Translator service so that it detects a language.
As the world shrinks and desire for instantaneous communication expands, language barriers no longer impose the restrictions they once did. With Microsoft’s Translator APIs, it is possible to attract a wider audience to web pages and applications written in PHP.
Use the web widget to expose information on any web page to worldwide visitors by simply inserting the code generated by the Translator. Implement AJAX to customize Translator behaviors such as designating specific languages. The HTTP method can be useful for detecting what language was used in a block of text.
PHP developers have the benefit of using any of these methods to allow translation for users. They are also able to work outside the Microsoft platform while leveraging the Translator APIs. Use the samples in this paper to build upon and create your own applications and solutions using PHP and the Microsoft Translator.
This is Microsoft owned content displayed under license.