App_GlobalResources不适用于asp.net中的多语言支持

时间:2013-04-22 09:32:31

标签: c# asp.net app-globalresources

代码背后:

 ResourceManager rm;
    CultureInfo ci;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Session["Flag"] = null;
            Session["Language"] = null;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            rm = new ResourceManager("Resources.Language",
                     System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadLanguage(ci);

        }
    }
    private void LoadLanguage(CultureInfo ci) 
    {
        lblHindi.Text = rm.GetString("Hindi", ci);
        lblTelugu.Text = rm.GetString("Telugu", ci);

    }

我的App_GlobalResources文件夹中有以下文件

enter image description here

设计:

   <center>


                         
      

        <a href="Default.aspx"><asp:Label ID="lblHindi" runat="server" Text="Hindi"></asp:Label></a>
        <br />
 <br />

        <a href="Default.aspx"><asp:Label ID="lblTelugu" runat="server" Text="Telugu"></asp:Label></a>
  <br />
        </center>

然而,它无法正常工作

请帮忙

2 个答案:

答案 0 :(得分:0)

您可以添加并试用此代码吗?

“protected override void InitializeCulture()” - &gt;待调用覆盖应用层Culture

的方法
protected override void InitializeCulture()
{
    //...
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    base.InitializeCulture();
    ci = Thread.CurrentThread.CurrentCulture;
    //...
}

private void LoadLanguage(CultureInfo ci) 
{
    lblHindi.Text = rm.GetString("Hindi", ci);
    lblTelugu.Text = rm.GetString("Telugu", ci);

}

答案 1 :(得分:0)

你写的代码完全没问题。似乎问题是您从资源文件访问字符串的方式。您必须在每个资源文件中维护相同的密钥,而值将以不同的语言进行翻译。假设您将在Language.resx文件中创建一个具有英语值的键“SOME_KEY1”。在其他resx文件中创建具有其他语言翻译的不同值的相同密钥。之后,您可以按如下方式访问翻译的字符串:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Flag"] = null;
                Session["Language"] = null;

                rm = new ResourceManager("Resources.Language",
                         System.Reflection.Assembly.Load("App_GlobalResources"));

                LoadLanguage();
            }
        }

        private void LoadLanguage()
        {
            lblHindi.Text = rm.GetString("SOME_KEY1", new CultureInfo("hi-IN"));
            lblTelugu.Text = rm.GetString("SOME_KEY1", new CultureInfo("te-IN"));
        }