这是多语言的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsMultiLanguage
{
public partial class Form1 : Form
{
ResourceManager m_resourceManger;
public Form1()
{
InitializeComponent();
m_resourceManger = new ResourceManager("WindowsFormsMultiLanguage.Localization", Assembly.GetExecutingAssembly());
// Init UICulture to CurrentCulture
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
// Init Controls
UpdateUIControls();
}
private void UpdateUIControls()
{
try
{
if (m_resourceManger != null)
{
this.label1.Text = m_resourceManger.GetString("test1");
this.label2.Text = m_resourceManger.GetString("test2");
}
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
}
private void OnLanguageChange(object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
string culture = string.Empty;
switch (radioButton.Text)
{
case "French - France (fr-FR)":
culture = "fr-FR";
break;
case "U.S. English (en-US)":
culture = "en-US";
break;
}
// This is used for the language of the user interface
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
}
}
}
我收到错误消息,此时无法解析资源项test&,test2:
this.label1.Text = m_resourceManger.GetString("test1");
this.label2.Text = m_resourceManger.GetString("test2");
我已经添加了2个用于英语的资源文件1和用于法语的第2个资源文件,我不知道这是什么错误..
![在此处输入图片说明] [1] ![在此处输入图像说明] [2]
答案 0 :(得分:0)
资源Localization
可能不在命名空间Localization
中,因此"Localization.Localization"
构造函数的参数ResourceManager
是错误的。我只是在这里猜测,因为没有足够的信息来发现问题。
UpdateUIControls
在开始时只调用了一次,但在我更改语言时却没有。我将UpdateUIControls
的两个主要行复制到OnLanguageChange
的末尾,之后我更改语言时文本也发生了变化。
所以,我没有看到任何问题。问题出在其他地方。