1

后面的代码:

 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 文件夹中有以下文件

在此处输入图像描述

设计 :

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

但是它不起作用我该怎么做

请帮忙

4

2 回答 2

0

您编写的代码非常好。似乎问题在于您从资源文件访问字符串的方式。您必须在每个资源文件中维护相同的 Key,而 value 将被翻译成不同的语言。假设您将在 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"));
        }
于 2013-04-22T10:08:41.410 回答
0

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

"protected override void InitializeCulture()" -> 待调用的方法覆盖应用层文化

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

}
于 2013-04-22T09:48:05.827 回答