I want to convert my site to different languages based on the culture.(frame work .Net 3.5)When we use localization, we have to write all translated text in the resource files.I want a method which translate every text into the chosen language automatocally.Can
I do it with google translator?It will work in IE?Please help me to find a solution.
This is possible. Below you can find the code in which the real text served by ASP.Net page is in English but when delivered on browser, it is automatically converted to Spanish using Google Language API. In the sample, I have converted the text present
in Label control called "Translation". You can further extend it to work for entire page or a part of it.
Here goes the code
ASPX Code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Automatic Text Conversion using Google Language API</title>
</head>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
var defLang = "";
var targetLang = "";
var ControlID = "Translation";
google.load("language", "1");
function initialize() {
var container = document.getElementById(ControlID);
if (container != null) {
google.language.translate(container.innerHTML, defLang, targetLang, function(result) {
if (!result.error) {
container.innerHTML = "";
container.innerHTML = result.translation;
}
});
} else {
alert("Invalid ControlID provided.");
}
}
</script>
<body>
<form id="form1" runat="server">
<asp:Label ID="Translation" runat="server" Text="Its Friday, let's party."></asp:Label>
</form>
</body>
</html>
Code-Behind
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'You can find the whole list of code at following link
'http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable
'Variables to hold the primary & target language code for Google Language APIs
Dim PrimaryGoogleLanguageCode As String = "en" 'English
Dim TargetGoogleLanguageCode As String = "es" 'Spanish
' Define the name and type of the client script on the page.
Dim csName As [String] = "GoogleLangAPI"
Dim csType As Type = Me.[GetType]()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the client script is already registered.
If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
Dim csText As New StringBuilder()
csText.Append("<script type=""text/javascript"">defLang=""")
csText.Append(PrimaryGoogleLanguageCode)
csText.Append(""";targetLang=""")
csText.Append(TargetGoogleLanguageCode)
csText.Append(""";google.setOnLoadCallback(initialize);</script>")
cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
End If
End Sub
End Class
anjuv
Member
162 Points
42 Posts
Google Translation Service
Aug 12, 2010 09:43 AM|LINK
Hi,
I want to convert my site to different languages based on the culture.(frame work .Net 3.5)When we use localization, we have to write all translated text in the resource files.I want a method which translate every text into the chosen language automatocally.Can I do it with google translator?It will work in IE?Please help me to find a solution.
Thanks
Anju
.net multilanguage
smirnov
All-Star
23446 Points
4029 Posts
Re: Google Translation Service
Aug 12, 2010 12:16 PM|LINK
Here you go.
ankur.nigam
Member
536 Points
141 Posts
Re: Google Translation Service
Aug 12, 2010 12:29 PM|LINK
Hi Anju,
This is possible. Below you can find the code in which the real text served by ASP.Net page is in English but when delivered on browser, it is automatically converted to Spanish using Google Language API. In the sample, I have converted the text present in Label control called "Translation". You can further extend it to work for entire page or a part of it.
Here goes the code
ASPX Code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Automatic Text Conversion using Google Language API</title> </head> <script type="text/javascript" src="//www.google.com/jsapi"></script> <script type="text/javascript"> var defLang = ""; var targetLang = ""; var ControlID = "Translation"; google.load("language", "1"); function initialize() { var container = document.getElementById(ControlID); if (container != null) { google.language.translate(container.innerHTML, defLang, targetLang, function(result) { if (!result.error) { container.innerHTML = ""; container.innerHTML = result.translation; } }); } else { alert("Invalid ControlID provided."); } } </script> <body> <form id="form1" runat="server"> <asp:Label ID="Translation" runat="server" Text="Its Friday, let's party."></asp:Label> </form> </body> </html>Code-Behind
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'You can find the whole list of code at following link 'http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable 'Variables to hold the primary & target language code for Google Language APIs Dim PrimaryGoogleLanguageCode As String = "en" 'English Dim TargetGoogleLanguageCode As String = "es" 'Spanish ' Define the name and type of the client script on the page. Dim csName As [String] = "GoogleLangAPI" Dim csType As Type = Me.[GetType]() ' Get a ClientScriptManager reference from the Page class. Dim cs As ClientScriptManager = Page.ClientScript ' Check to see if the client script is already registered. If Not cs.IsClientScriptBlockRegistered(csType, csName) Then Dim csText As New StringBuilder() csText.Append("<script type=""text/javascript"">defLang=""") csText.Append(PrimaryGoogleLanguageCode) csText.Append(""";targetLang=""") csText.Append(TargetGoogleLanguageCode) csText.Append(""";google.setOnLoadCallback(initialize);</script>") cs.RegisterClientScriptBlock(csType, csName, csText.ToString()) End If End Sub End ClassFor the list of language codes used by Google Language Translation service, refer the following URL : http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable
To refer Google Language Translation API, refer : http://code.google.com/apis/ajaxlanguage/documentation/
Ankur
My Blogs:
Twitter | DevAstrum
Tapan Bhatt
Contributor
2352 Points
403 Posts
Re: Google Translation Service
Aug 12, 2010 12:50 PM|LINK
Here, you get solution: http://code.google.com/apis/ajaxlanguage/
"Simple things should be simple, complex things should be possible."