Articles
|
|
Please use FireFox to view this page
This website has been designed for use with the FireFox browser. Please use FireFox to view this page.
Lingua Translation / Localization Webservice
The Lingua localization (or globalisation) web service
resides at http://network.programming-in.net/lingua.asmx
it is capable of translating text and or complete URL’s to/from English (en), French (fr), German (de),
Italian (it), Portuguese (pt), and Spanish (es). It is free to use, and is easily
integrated with ASP.NET, C#, VB.NET, or in fact any .NET language.
Information on how to include a web service in asp.net pages, or windows forms
applications can be found in chapter 2 & 17 of Network
Programming in .NET
(Buy at Amazon UK)
(Buy at Amazon US)
. If you would like to learn how best to develop and
use web services, such as Lingua, it is recommended that you read this book.
Using the ready-made URL translator
The simplest way of using the service is to use the “translateurl” page, which resides at http://network.programming-in.net/articles/translateURL.aspx,
using this page, you can provide visitors to your site with alternative
languages, such as French, German, Italian, Portuguese, or Spanish. To use it,
simply pass two parameters in the query string, (1) URL, for the fully
qualified URL to the page you wish to translate, and (2) Lang, for the
two-letter language code for the language to be translated to. The source
language is assumed to be English. An example could be:
http://network.programming-in.net/articles/translateURL.aspx?URL=http://www.xaml.net&lang=de
The advantage of using this over a using links from Google
URL translator is that, (1) your page will not be displayed in a frame, or
contain any Google branding. (2) The page is index-able by search engines, so
that non-English speaking visitors will be able to find the translated version
of your site in search engines. This does not happen with Google-translated
sites.
Using the translation webservice from asp.net
More advanced uses of the web service are possible. For
instance, as everybody knows, machine translation is far from perfect, so you
may wish to have sections of your site professionally translated by native
speakers. However, if you have a large database containing product details, it
could prove expensive to manually translate a whole database. This is where
this system lends itself very well to a hybrid system.
Let us say you have your ASPX pages translated into some
foreign language, say French. But a SQL server database containing product
descriptions in English, take the Northwind products table for example, (item
20, Rodney’s marmalade), the quantity is described as “30 gift boxes”. A
non-English speaking person may not know what “gift boxes” are, and thus, does
not buy the product. To correct this, you could create a new column “QuantityPerUnit_FR” in the table, and
insert French translations into this column, using the webservice to perform
the English to French translation.
Information on how to perform these simple database
operations, and how to include a web service in asp.net pages, or windows forms
applications can be found in chapter 2 & 17 of Network
Programming in .NET
(Buy at Amazon UK)
(Buy at Amazon US)
. If you would like to learn how best to develop and
use web services, such as Lingua, it is recommended that you read this book.
However for completeness, here is the Lingua.cs,
which you could include in your code-behind asp.net page, or windows form app.
NB: Data returned from this webservice is base-64 encoded unicode!
This is in order to avoid problems with malformed XML. In order to convert this back to a readable string
call Encoding.Unicode.GetString(Convert.FromBase64String(text64))
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using
System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="LinguaServiceSoap",
Namespace="http://www.webtropy.com/linguaService/")]
public class
LinguaService : System.Web.Services.Protocols.SoapHttpClientProtocol {
/// <remarks/>
public
LinguaService() {
this.Url
= "http://www.webtropy.com/lingua.asmx";
}
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.webtropy.com/linguaService/TranslateURL",
RequestNamespace="http://www.webtropy.com/linguaService/",
ResponseNamespace="http://www.webtropy.com/linguaService/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public
string TranslateURL(string
URL, string fromLanguage, string toLanguage) {
object[]
results = this.Invoke("TranslateURL",
new object[] {
URL,
fromLanguage,
toLanguage});
return
((string)(results[0]));
}
/// <remarks/>
public
System.IAsyncResult BeginTranslateURL(string
URL, string fromLanguage, string toLanguage, System.AsyncCallback callback, object asyncState) {
return
this.BeginInvoke("TranslateURL", new object[] {
URL,
fromLanguage,
toLanguage},
callback, asyncState);
}
/// <remarks/>
public
string EndTranslateURL(System.IAsyncResult
asyncResult) {
object[]
results = this.EndInvoke(asyncResult);
return
((string)(results[0]));
}
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.webtropy.com/linguaService/TranslateText",
RequestNamespace="http://www.webtropy.com/linguaService/",
ResponseNamespace="http://www.webtropy.com/linguaService/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public
string TranslateText(string
Content, string fromLanguage, string toLanguage) {
object[]
results = this.Invoke("TranslateText",
new object[] {
Content,
fromLanguage,
toLanguage});
return
((string)(results[0]));
}
/// <remarks/>
public
System.IAsyncResult BeginTranslateText(string
Content, string fromLanguage, string toLanguage, System.AsyncCallback callback, object asyncState) {
return
this.BeginInvoke("TranslateText", new object[] {
Content,
fromLanguage,
toLanguage},
callback, asyncState);
}
/// <remarks/>
public
string EndTranslateText(System.IAsyncResult
asyncResult) {
object[]
results = this.EndInvoke(asyncResult);
return
((string)(results[0]));
}
}
Other ideas, and uses for the Lingua webservice
During the course of developing this web service, some other
possibilities for the uses of this service came to mind. For instance, you
could use it to automatically localize resource (.resx) files for applications,
or even better, write an application that could scan a third party application
using API calls, and replace their text with localized text.
This application is beyond the scope of this article,
however, you should look in the Windows API reference section of this site to
read more on this possibility.
Copyright 2013 Open Merchant Account Ltd.
|