异常本地化 MenuStrip 控件
Exception Localizing MenuStrip Control
我有一个 MenuStrip 控件,其中包含针对多种语言输入的所有翻译。当我尝试动态更改区域性时,所有控件都在更改语言,因为它必须是 MenuStrip 除外。这是我专门用于 MenuStrip 的代码:
protected ComponentResourceManager res2;
private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
{
LoginForm login = new LoginForm();
if (login.ShowDialog() == DialogResult.OK)
{
//this is for other controls
resManager = new ComponentResourceManager(this.GetType());
this.ApplyResources(this, Thread.CurrentThread.CurrentCulture);
//this is for MenuStrip
res2 = new ComponentResourceManager(typeof(MenuStrip));
this.ApplyResourcesToolStripMenuItem(Thread.CurrentThread.CurrentCulture);
}
}
private void ApplyResourcesToolStripMenuItem(CultureInfo lang)
{
foreach (ToolStripItem m in this.menuStrip1.Items)
{
//resources.ApplyResources(m, m.Name, lang);
if (m is ToolStripMenuItem)
{
string text = res2.GetString(m.Name + ".Text", lang); //Exception is thrown here
if (text != null)
m.Text = text;
}
}
}
我使用了此论坛中另一位开发人员的代码示例并进行了一些小改动。
现在,当我在运行时更改区域性时,出现以下异常:
An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in
mscorlib.dll
Additional information: Could not find any resources appropriate for
the specified culture or the neutral culture. Make sure
"System.Windows.Forms.MenuStrip.resources" was correctly embedded or
linked into assembly "System.Windows.Forms" at compile time, or that
all the satellite assemblies required are loadable and fully signed.
我检查了资源文件,所有 MenuStripItems 都有 translations/values 那里...
我未能在互联网上找到解决方案,非常感谢您的帮助。
提前致谢!
因此,由于我是本地化的初学者,因此该解决方案比我发布的解决方案简单得多。也许这对其他初学者也有帮助。
所以我删除了所有实际上令人头疼的代码,并将其替换为以下代码:
Thread.CurrentThread.CurrentUICulture = new CultureInfo(StaticValues.currentCulture);
this.Controls.Clear();
this.InitializeComponent();
而且它很容易工作。是的,感谢 Adriano Repetti!
我有一个 MenuStrip 控件,其中包含针对多种语言输入的所有翻译。当我尝试动态更改区域性时,所有控件都在更改语言,因为它必须是 MenuStrip 除外。这是我专门用于 MenuStrip 的代码:
protected ComponentResourceManager res2;
private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
{
LoginForm login = new LoginForm();
if (login.ShowDialog() == DialogResult.OK)
{
//this is for other controls
resManager = new ComponentResourceManager(this.GetType());
this.ApplyResources(this, Thread.CurrentThread.CurrentCulture);
//this is for MenuStrip
res2 = new ComponentResourceManager(typeof(MenuStrip));
this.ApplyResourcesToolStripMenuItem(Thread.CurrentThread.CurrentCulture);
}
}
private void ApplyResourcesToolStripMenuItem(CultureInfo lang)
{
foreach (ToolStripItem m in this.menuStrip1.Items)
{
//resources.ApplyResources(m, m.Name, lang);
if (m is ToolStripMenuItem)
{
string text = res2.GetString(m.Name + ".Text", lang); //Exception is thrown here
if (text != null)
m.Text = text;
}
}
}
我使用了此论坛中另一位开发人员的代码示例并进行了一些小改动。 现在,当我在运行时更改区域性时,出现以下异常:
An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "System.Windows.Forms.MenuStrip.resources" was correctly embedded or linked into assembly "System.Windows.Forms" at compile time, or that all the satellite assemblies required are loadable and fully signed.
我检查了资源文件,所有 MenuStripItems 都有 translations/values 那里...
我未能在互联网上找到解决方案,非常感谢您的帮助。
提前致谢!
因此,由于我是本地化的初学者,因此该解决方案比我发布的解决方案简单得多。也许这对其他初学者也有帮助。
所以我删除了所有实际上令人头疼的代码,并将其替换为以下代码:
Thread.CurrentThread.CurrentUICulture = new CultureInfo(StaticValues.currentCulture);
this.Controls.Clear();
this.InitializeComponent();
而且它很容易工作。是的,感谢 Adriano Repetti!