VS 2017 在使用全球化时始终使用默认语言

问题描述

我有一个以英语和法语运行的程序。在项目中,我有一个 Resources 文件夹,其中包含一个带有英文字符串的 Localization.resx 文件和一个带有法语字符串的 Localization.fr-FR.resx 文件。 File setup

当我运行法语版本时,我已经将 Current Culture 和 Current UI Culture 设置为 fr-FR 但是当我运行程序时,客户端查询 Localization.Designer.cs 以获取翻译后的字符串,但我仍然得到英文字符串. Here is an example of how I get the string from Localization.Designer.cs。从调试中可以看出,resourceCulture 已正确设置为法语,但 ResourceManager 仍返回英语版本。有什么我遗漏的或者我可以设置错误的任何方式吗?任何帮助将不胜感激

解决方法

首先,您的项目是什么类型的?

您是否设置了 Thread.CurrentUICulture Property Definition 并调用了 ComponentResourceManager.ApplyResources Method 以将资源的值应用于每个控件?

为了更方便的访问resx文件,建议同时为“English”创建一个resx文件“en-US.resx”。

这是一个 winforms 演示。

resx 文件:

enter image description here

private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
    if (comboBox1.Text == "en")
    {
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
        ApplyResource();
    }
    if (comboBox1.Text == "fr")
    {
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
        ApplyResource();
    }
}

private void ApplyResource()
{
    System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(Form1));
    foreach (Control ctl in Controls)
    {
        res.ApplyResources(ctl,ctl.Name);
    }
    res.ApplyResources(this,"$this");
}

测试结果:

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...