Reputation: 1107
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
Reputation: 11
Take into account several aspects:
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();
}
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);
}
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
This page show you how to do that https://msdn.microsoft.com/en-us/library/ms178426.aspx
Upvotes: 1
Reputation: 10872
Implementing multi-lingual is not as simple as you think.
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>
To generate a local resource file from an ASP.NET Web page
Open the page for which you want to create a resource file.
Switch to Design View.
In the Tools menu, click Generate Local Resource. (It will create the a resource file in local resources folder)
Type values for each resource that you need in your application, and then save the file.
Read more Creating Resources From a Web Page
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
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 inPage_Init
event (which occurs before Page_Load) event fromRequest.Form
values and set the cultureOr inside
CheckedChanged
save a value in session or cookie and reload the page and inPage_Init
use the session/cookie value to set the thread culture
Upvotes: 1
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:
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