我想更改语言,但是在编译时会弹出异常。它说
“找不到适用于指定区域性或中性区域性的任何资源。请确保在编译时已将“ System.Type.resources”正确嵌入或链接到程序集“ mscorlib”中,或者确保所需的所有附属程序集都可加载且完全签。”
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "English")
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("En");
ChangeLanguage("En");
}
else if (comboBox1.SelectedItem.ToString() == "German")
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("De");
ChangeLanguage("De");
}
}
private void ChangeLanguage(string lang)
{
foreach (Control c in this.Controls)
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(Type));
resources.ApplyResources(c, c.Name, new CultureInfo(lang));
}
}
有什么建议么?
ComponentResourceManager resources = new ComponentResourceManager(typeof(Type));
构造函数的参数是错误的,您要告诉它查找System.Type的资源。这就是为什么它抱怨找不到“ System.Type.resources”的原因。它永远找不到。
您需要传递实际要本地化的表单的类型。使用this.GetType()
代替。尽管这可能只是将“选项”表单而不是应用程序中其余窗口本地化。您可以代替Application.OpenForms()。也有必要将本地化应用于所有控件。不仅是表单上的表单,还包括位于面板之类的容器内的表单。从而:
private static void ChangeLanguage(string lang) {
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
foreach (Form frm in Application.OpenForms) {
localizeForm(frm);
}
}
private static void localizeForm(Form frm) {
var manager = new ComponentResourceManager(frm.GetType());
manager.ApplyResources(frm, "$this");
applyResources(manager, frm.Controls);
}
private static void applyResources(ComponentResourceManager manager, Control.ControlCollection ctls) {
foreach (Control ctl in ctls) {
manager.ApplyResources(ctl, ctl.Name);
applyResources(manager, ctl.Controls);
}
}
请谨慎使用类似wiz-bang的功能。在使用您的程序时,实际上没有人更改其母语。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句