Tuesday, 18 June 2013

Translation

How to fetch data from database and Translate it into another language


Introduction- In this article I am going to explain how to fetch data from database and convert it into another language like (hindi.swidden,russian, Arabic, Bulgarian, Danish etc) using asp.net c#.

Implementation- create a website ,add page named language.aspx. place a textbox named txt_msg and place a label named lbl_msg and a button named btn_submit. Then download a zip file from the below mentioned link.






Then open this Zip file. There will be a bin folder in the downloaded file. Copy this bin folder and paste it into the root of your website. There will be two .dll files named GoogleTranslateAPI.dll and Newtonsoft.Json.dll and a xml file named GoogleTranslateAPI.XML.



Code for language.aspx.cs Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Globalization;


public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
bind_lbl();
}

}
protected void Button1_Click(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();


SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into tb_detail values(@detail)";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@detail",txt_msg.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
bind_lbl();






}
private void bind_lbl()
{
try
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("select Top(1) detail from tb_detail order by id desc", con);
DataSet ds = new DataSet();
adp.Fill(ds);
adp.Dispose();
String detail = Convert.ToString(ds.Tables[0].Rows[0]["detail"]);
Console.WriteLine("Before Translation:{0}", detail);

detail = Google.API.Translate.Translator.Translate(detail,Google.API.Translate.Langua e.English,

// below after the laguage. , you can select the language according to your need ,


Google.API.Translate.Language.Hindi);
Or
Google.API.Translate.Language.Arabic);
Or
Google.API.Translate.Language.Bulgarian);
Or
Google.API.Translate.Language. Catalan);



lbl_msg.Text = detail;

}
catch
{ }
}
}

Conclusion- I hope someone will find this information useful and make your programming job easier. Please send me an email at bharti222000@yahoo.com , if you want to help improve this article.

Database Script
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_detail]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tb_detail](
[id] [int] IDENTITY(1,1) NOT NULL,
[detail] [varchar](1000) NULL,
CONSTRAINT [PK_tb_detail] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END

No comments:

Post a Comment