繁体   English   中英

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

[英]How to make my webform to be multi-language?

我将使用Devexpress v14.1通过Visual Studio 2013编写Web表单。

需要Web表单才能使用CheckedChanged事件更改语言。

我已经阅读了Google的一些文章,但似乎需要逐个设置所有控件。

例如,如果我的网页中有30个标签,则需要添加30行:

Label1.Text = ...;

Label2.Text = ...;

...

Label30.Text = ...;

制作多语言网页的最佳方法是什么?

请帮忙!!!

我希望这些文章ASP.NET网页资源概述如何:以编程方式检索资源值可以帮助您解决问题:

  1. 为ASP.NET网站创建资源文件
  2. 在Web页面中使用资源
  3. 选择不同语言的资源文件

例如。

<%@ 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> 

实现多语言并不像您想象的那么简单。

先决条件: -

在您的页面上需要多语言的所有控件应该是服务器控件。 例如

<Label runat="server" ID="lblName" Text="Type your name"></Label>
  1. 创建资源文件

从ASP.NET网页生成本地资源文件

  1. 打开要为其创建资源文件的页面。

  2. 切换到设计视图。

  3. 在“工具”菜单中,单击“生成本地资源”。 (它将在本地资源文件夹中创建资源文件)

  4. 在应用程序中键入所需的每个资源的值,然后保存该文件。

阅读更多从网页创建资源

  1. 成功创建默认资源文件(例如mypage.resx )后,复制/粘贴它并使用特定语言重命名复制的文件,例如mypage.fr.resx for french

  2. 将值更改为特定于语言的值


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

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

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

考虑几个方面:

1.您必须在会话中跟踪UICulture(例如,使用SessionManager类)。 然后,您必须在您的页面上初始化。

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

您网页中的代码:

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

2.更改页面特定事件的UICulture(在本例中为onCheckedChanged)。 在此示例中,该页面仅提供两种可能的语言,英语或西班牙语

    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.更改页面的标签。 最佳方法:使用资源文件,即您可以拥有两个资源文件。

您必须使用服务器控件才能实现此目的。 例如:

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

然后你必须在你的代码隐藏中更改它:

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

您可以在这里找到更多信息https://msdn.microsoft.com/en-us/library/fw69ke6f.aspx

4.除了使用资源文件作为页面内容之外,您还可能需要翻译菜单(使用站点地图时)。

此页面向您展示如何做到这一点https://msdn.microsoft.com/en-us/library/ms178426.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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