Wednesday, 30 March 2011

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

11 comments:

  1. Hi

    It is very helpful and greate for programmers who works in language field. I find it very helpful for me to integrate my application with google API.
    But I have some question about translating from my own database with has two tabels Sweden_language and Tigrina_language. I can translate single word from Sweden word to Tigrina and vice versa. But my problem is How can I translate long or full text from Sweden to Tigrina language. I can illustrate by example

    Swedish --> tigrina
    väntar ምጽባይ
    it works greate for single word translation.
    My problem is if it is text to translate

    Swedish --> tigrina

    Du kan skriva
    i textfält
    från dessa ord
    och Tigrina
    Lexicon kommer
    att översätta till
    önskade språk

    it doesn't works to translate to tigrina?
    How can solve this problem.

    muehai

    ReplyDelete
  2. This code gives transtate failed error ,How it will be solved ,If you know so pls send me mail with suitable code.Thank you
    my mail id:abjain786@gmail.com

    ReplyDelete
  3. It shows dat conversion is done bt not showing the converted value of the text . Pls leyt me know how can i see that cnvrted text on my page?

    ReplyDelete
    Replies
    1. Hi!! sapna. when i posted this article it was working perfectly but some days ago its not working properly, This error come due to the some problem related to the dll. It gives an error translation failed in the exception. i am working for this issue and i will definately solve this problem as soon as possible.

      Thanks

      Delete
  4. It give me following error:
    [error code:403]Suspected Terms of Service Abuse. Please see http://code.google.com/apis/errors

    ReplyDelete
    Replies
    1. Hi!! kapil kumawat. Dear this code was running successfullly at the last year but now the .dll file creates some error, there is no any problem in the code. I'll work again for this article. Please give me some time to sort out the problem.

      Thanks
      Using Asp.net

      Delete
  5. Hi!! கவிதைச் சாரல். This error is related to the .dll file that is provided by the google. There is no type of error inside the code. I am going to Write my problem to the google. I hope that they will send the solution as soon as possible. See below link
    http://code.google.com/p/google-language-api-for-dotnet/issues/detail?id=10

    Thanks
    Using Asp.net

    ReplyDelete
  6. Hi

    I am getting "Translation Failed " Exception when i try with this exampl.

    Can you please update asap. if you have any update.

    Regards

    ReplyDelete
  7. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. servicios de traducción

    ReplyDelete
  8. Hello, i study your blog often and i own an analogous one and i used to be just wondering if you get much of spam comments? If so how do you catch it, any plugin or anything you’ll be able to advise? I get thus often lately it’s driving me crazy so any help is very abundant appreciated. for older folks, retirement or a huge promotion at work is a time when celebration events are going to be a great idea. How to Translate Good Evening to Italian

    ReplyDelete