[英]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. 我将使用Devexpress v14.1通过Visual Studio 2013编写Web表单。
The web form is required to change the language with CheckedChanged event. 需要Web表单才能使用CheckedChanged事件更改语言。
I have read some of the articles from Google, but it seems that it required to set all of the controls one by one. 我已经阅读了Google的一些文章,但似乎需要逐个设置所有控件。
For example, if there are 30 labels in my web page, it is necessary to add 30 lines: 例如,如果我的网页中有30个标签,则需要添加30行:
Label1.Text = ...; Label1.Text = ...;
Label2.Text = ...; Label2.Text = ...;
... ...
Label30.Text = ...; Label30.Text = ...;
What is the best approach to make multi-language web page ? 制作多语言网页的最佳方法是什么?
Please help!!! 请帮忙!!!
I hope these articles ASP.NET Web Page Resources Overview and How to: Retrieve Resource Values Programmatically can help you to solve your problem: 我希望这些文章ASP.NET网页资源概述和如何:以编程方式检索资源值可以帮助您解决问题:
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>
Implementing multi-lingual is not as simple as you think. 实现多语言并不像您想象的那么简单。
All controls which need multi-lingual on your page should be server control. 在您的页面上需要多语言的所有控件应该是服务器控件。 eg
例如
<Label runat="server" ID="lblName" Text="Type your name"></Label>
To generate a local resource file from an ASP.NET Web page
从ASP.NET网页生成本地资源文件
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 (eg mypage.resx
) then copy/paste it and rename the copied file with the language specific eg mypage.fr.resx
for french 成功创建默认资源文件(例如
mypage.resx
)后,复制/粘贴它并使用特定语言重命名复制的文件,例如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. Asp.net使用基于当前线程文化的resx文件,但问题是
CheckedChanged
后发生事件Page_Load
事件等等CheckedChanged
事件的方法是不改变线程文化的正确位置。
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 culture因此,您需要在
Request.Form
值的Page_Init
事件(在Page_Load之前发生)事件中手动捕获CheckedChanged
值并设置文化Or 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或者在
CheckedChanged
保存会话或cookie中的值并重新加载页面,并在Page_Init
使用session / cookie值来设置线程文化
Take into account several aspects: 考虑几个方面:
Code in your SessionManager class: SessionManager类中的代码:
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 您可以在这里找到更多信息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 此页面向您展示如何做到这一点https://msdn.microsoft.com/en-us/library/ms178426.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.