Getting Started with Microsoft Translator for PHP Developers

 [This is preliminary documentation and is subject to change.]

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

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.


Create the same functionality on any page using the web widget. You can later customize the widget’s color, size, and translation behaviors using JavaScript. Simply go to and specify a site’s domain name and current language, select from the color and width options, and agree to Microsoft’s use policies. Then click the Generate code button and copy and paste the new code into a site.


The site is now accessible to people speaking multiple languages.

Using the AJAX API – Translating a Block of Text

While delivering similar functionality as the Web widget, the AJAX API is useful when more customization is desired. It is also more flexible and allows for improved server response time because it can be called asynchronously from the client and does not require a page reload if used with JavaScript.


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


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)

function translatePage()
                //  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.

It is also possible to use JavaScript variables to set the language when the link is clicked instead of hard-coding it. First, create the link so that it uses the variable. In the following case, the link specifies that it will translate to French but the method does not use a hardcoded “fr” language code; instead it uses the French variable that we will assign. 

<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.

<script type="text/javascript">
        // Set the  languages
        var  currentLanguage = "en";
        var spanish  = "es";
        var german = "de";
        var english  = "en";
        var french =  "fr";
        function  translatePage(language)
Microsoft.Translator.translate(document.body,  currentLanguage, language);
        currentLanguage = language;

Using the HTTP API – Detecting the Language of a Block of Text

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, sign in with your Windows Live ID, and follow the instructions to generate the appId.

appidHTTPSOAP.jpg     appidHTTPSOAP2.jpg

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:&nbsp;


                <form method="post">

                                <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(''.$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.