Search This Blog

Friday, April 5, 2013

How can develop a C# .NET application to support multiple languages?



I have start work on one asp.net (in 2008) application client wants multiple languages in application. Client wants application run in Hindi, English, Japanese, and Urdu language on same page for all content. In this application all page content convert in different languages on one click on selected language.

ASP.NET provides App_GlobalResources for resources file, always used resource files for multi-language applications.

Step for creating new application in multiple languages:

Step1.
Create new web application name language as per below screen in asp.net 2008.

 
 Step2.
After that add resource file in application, when add Resource file ENUS.en.resx  ASP.NET framework Ask for folder where save .resx file click on YES then auto create App_GlobalResources  folder in application as per below screen.





Step3.
Like above process also add many resource file for different languages. As per screen I am using 5 language in this application. So add 5 Resource file for 5 different languages.
ENUS.en.resx for ENGLISH
ENUS.es.resx for SPANISH
ENUS.hi.resx for HINDI
ENUS.ja.resx for JAPANESE
ENUS.ur.resx for URDU



Step4.
After this step we have create our application page Defualt.aspx in this page I have define all fields in label. As per below screen. I want change all label in different language on click given buttons.Also remember on aspx page UICulture=”auto”
<%@ Page Language="C#" AutoEventWireup="true" UICulture="auto" Culture="auto"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



Step5.
Open resx file and you can see 3 fields Name , Value and comment. As per below screen you can define Name and value in different languages.



Name :: Default_lbl_department   name format not fix ,its depend on you in my name Default is page name and lbl_depatment is label name  which are define on my default page .

Value :: Value is different language text, its define in value field. Like I have one field Department:: विभाग. I have put in Hindi in this way you can define all fields in Value field as per below screen.


Step6.
We can define namespace in default.cs file, that provided by asp.net.
using System.Threading;
using System.Globalization;
using System.Resources;
using Resources;

Step7.
Write cs page code:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using System.Resources;
using Resources;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    protected void But_spanish_Click(object sender, EventArgs e)
    {
        string lng = "es";
        LangChange(lng);
    }
    protected void But_japanese_Click(object sender, EventArgs e)
    {
        string lng = "ja";
        LangChange(lng);
    }

    protected void But_urdu_Click(object sender, EventArgs e)
    {
        string lng = "ur";
        LangChange(lng);

    }
    protected void But_hindi_Click(object sender, EventArgs e)
    {
        string lng = "hi";
        LangChange(lng);
    }
    protected void But_eng_Click(object sender, EventArgs e)
    {
        string lng = "en";
        LangChange(lng);
    }

    public void LangChange(string strLanguage)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strLanguage);
        lbl_department.Text = ENUS.Default_lbl_department;
        lbl_designation.Text = ENUS.Default_lbl_designation;
        lbl_email.Text = ENUS.Default_lbl_email;
        lbl_emp.Text = ENUS.Default_lbl_emp;
        lbl_password.Text = ENUS.Default_lbl_password;
        lbl_phone.Text = ENUS.Default_lbl_phone;
        lbl_userid.Text = ENUS.Default_lbl_userid;
    }
}

Step8.
After that we can run application and find page on browser as per below screen.On click button change reflect on web page as per languages.

Important Notes : If you find below error when build the application.

Error    1          The namespace 'Resources' already contains a definition for 'ENUS'            c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\language\630c8d9e\42ad8c5b\App_GlobalResources.__9l9ovu.1.cs     12 
      
In this case open given path and delete all Resources file and again build your application.
I hope this step useful for your application. If any query or feedback then put your valuable comments on this topic.

2 comments :

summ3r said...

If you don't mind, I would like to recommend a tool which might help you in the process of making multilingual applications using .resx files.
It's an online software localization platform, https://poeditor.com/
The tool is project based and it uses the power of your own community to translate the strings, giving you a neat workspace and powerful features like API and Translation Memory to automate the workflow as much as possible.
I hope you will give it a try and see if it fits your needs.

Chandra Prakash Yadav said...

Dear Summ3r thanks for new things.