简体   繁体   English

如何让我的webform成为多语言?

[英]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网页资源概述如何:以编程方式检索资源值可以帮助您解决问题:

  1. Creating Resource Files for ASP.NET Web Sites 为ASP.NET网站创建资源文件
  2. Working with Resources in Web Pages 在Web页面中使用资源
  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> 

Implementing multi-lingual is not as simple as you think. 实现多语言并不像您想象的那么简单。

Prerequisite: - 先决条件: -

All controls which need multi-lingual on your page should be server control. 在您的页面上需要多语言的所有控件应该是服务器控件。 eg 例如

<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 从ASP.NET网页生成本地资源文件

  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 (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

  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. Asp.net使用基于当前线程文化的resx文件,但问题是CheckedChanged后发生事件Page_Load事件等等CheckedChanged事件的方法是不改变线程文化的正确位置。

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 因此,您需要在Request.Form值的Page_Init事件(在Page_Load之前发生)事件中手动捕获CheckedChanged值并设置文化

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 或者在CheckedChanged保存会话或cookie中的值并重新加载页面,并在Page_Init使用session / cookie值来设置线程文化

Take into account several aspects: 考虑几个方面:

1. You have to keep track of the UICulture in your session (for example, using your SessionManager class). 1.您必须在会话中跟踪UICulture(例如,使用SessionManager类)。 Then, you have to initialize in on your Page. 然后,您必须在您的页面上初始化。

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();
    }

2. Change the UICulture on the specific event of your page (onCheckedChanged in this case). 2.更改页面特定事件的UICulture(在本例中为onCheckedChanged)。 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. 3.更改页面的标签。 Best approach: use resource files ie 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 您可以在这里找到更多信息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). 4.除了使用资源文件作为页面内容之外,您还可能需要翻译菜单(使用站点地图时)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM