Tony Wu
Tony Wu

Reputation: 1107

How to make my webform to be multi-language?

I am going to write a web form via Visual Studio 2013 with Devexpress v14.1.

The web form is required to change the language with CheckedChanged event.

I have read some of the articles from Google, but it seems that it required to set all of the controls one by one.

For example, if there are 30 labels in my web page, it is necessary to add 30 lines:

Label1.Text = ...;

Label2.Text = ...;

...

Label30.Text = ...;

What is the best approach to make multi-language web page ?

Please help!!!

Upvotes: 2

Views: 6504

Answers (3)

juankamilomarin
juankamilomarin

Reputation: 11

Take into account several aspects:

1. You have to keep track of the UICulture in your session (for example, using your SessionManager class). Then, you have to initialize in on your Page.

Code in your SessionManager class:

    public string SUICulture
    {
        get
        {
            if (HttpContext.Current.Session["SUICulture"] == null)
            {
                HttpContext.Current.Session["SUICulture"] = "es";
            }
            return HttpContext.Current.Session["SUICulture"].ToString();
        }
        set
        {
            HttpContext.Current.Session["SUICulture"] = value;
        }
    }

Code in your Page:

    protected override void InitializeCulture()
    {
        String currentUICulture = clsSessionManager.GetInstance().SUICulture;
        if(currentUICulture != null){
            UICulture = currentUICulture;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentUICulture);
        }
        base.InitializeCulture();
    }

2. Change the UICulture on the specific event of your page (onCheckedChanged in this case). In this example the page just offers two possible languages, English or Spanish

    protected void chckOnCheckedChange(object sender, EventArgs e)
    {
        if (clsSessionManager.GetInstance().SUICulture== "es")
        {
            clsSessionManager.GetInstance().SUICulture= "en";

        }else
        {
            clsSessionManager.GetInstance().SUICulture= "es";
        }
        Response.Redirect(Page.Request.Url.ToString(), true);
    }

3. Change the labels of your page. Best approach: use resource files i.e. you can have two resource files.

You have to use server controls in order to achieve this. For example:

   <asp:Literal ID="lblTitle" runat="server" />

Then you have to change it in your codebehind:

    lblTitle.Text = GetGlobalResourceObject("Default","labelTitle").ToString();

You will find more information here https://msdn.microsoft.com/en-us/library/fw69ke6f.aspx

4. Beside using resource files for the content of your page there is a possibility that you need to translate the menu as well (when using a sitemap).

This page show you how to do that https://msdn.microsoft.com/en-us/library/ms178426.aspx

Upvotes: 1

Waqas Raja
Waqas Raja

Reputation: 10872

Implementing multi-lingual is not as simple as you think.

Prerequisite: -

All controls which need multi-lingual on your page should be server control. e.g.

<Label runat="server" ID="lblName" Text="Type your name"></Label>
  1. Create a resource file

To generate a local resource file from an ASP.NET Web page

  1. Open the page for which you want to create a resource file.

  2. Switch to Design View.

  3. In the Tools menu, click Generate Local Resource. (It will create the a resource file in local resources folder)

  4. Type values for each resource that you need in your application, and then save the file.

Read more Creating Resources From a Web Page

  1. When you have successfully created a default resource file (e.g. mypage.resx) then copy/paste it and rename the copied file with the language specific e.g. mypage.fr.resx for french

  2. Change the values to the language specific values


Asp.net uses the resx file based on the current thread culture but the problem is CheckedChanged event occurs after the Page_Load event so CheckedChanged event method is not the correct place to change thread culture.

So you will need to capture the CheckedChanged value manually in Page_Init event (which occurs before Page_Load) event from Request.Form values and set the culture

Or inside CheckedChanged save a value in session or cookie and reload the page and in Page_Init use the session/cookie value to set the thread culture

Upvotes: 1

huoxudong125
huoxudong125

Reputation: 2056

I hope these articles ASP.NET Web Page Resources Overview and How to: Retrieve Resource Values Programmatically can help you to solve your problem:

  1. Creating Resource Files for ASP.NET Web Sites
  2. Working with Resources in Web Pages
  3. Selecting Resource Files for Different Languages

eg.

<%@ Page Language="C#" %>

<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
    Button1.Text = 
        GetLocalResourceObject("Button1.Text").ToString();
    Image1.ImageUrl = 
        (String)GetGlobalResourceObject(
        "WebResourcesGlobal", "LogoUrl");
    Image1.Visible = true;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="Button1" runat="server" 
        OnClick="Button1_Click" 
        Text="Get Resources" />
    <asp:Image ID="Image1" runat="server" 
        Visible="false" />
</div>
</form>
</body>
</html> 

Upvotes: 1

Related Questions