简体   繁体   English

为什么本地化不起作用

[英]Why localization doesn't work

I try to write simple C# project to test how localication work. 我尝试编写简单的C#项目来测试本地化的工作方式。 I did how here is written. 我这样做是怎么写的。

How to use localization in C# 如何在C#中使用本地化

I think I use the same like in the questions/1142802. 我想我在问题/ 1142802中使用相同的。 But it doesn't work in my Demo project. 但它在我的演示项目中不起作用。 I can't explain why. 我无法解释原因。 This is why I attached Demo project. 这就是我附加Demo项目的原因。

I have two resource files for FRA and RUS languages. 我有两个FRA和RUS语言的资源文件。

And try to switch language using next code 并尝试使用下一代码切换语言

private void rbFra_Click(object sender, EventArgs e)
{
  System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("fr-Fr");
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  Thread.CurrentThread.CurrentCulture = cultureInfo;

  textBox1.Text = Properties.Resources.String1;
}

private void rgEng_Click(object sender, EventArgs e)
{
  System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US");
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  Thread.CurrentThread.CurrentCulture = cultureInfo;

  textBox1.Text = Properties.Resources.String1;
}

private void rbRus_Click(object sender, EventArgs e)
{
  System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ru-RU");
  Thread.CurrentThread.CurrentUICulture = cultureInfo;
  Thread.CurrentThread.CurrentCulture = cultureInfo;

  textBox1.Text = Properties.Resources.String1;
}

But the result is always in English. 但结果总是用英语。

I think that Thread.CurrentThread.CurrentUICulture = ... should force to reload resource file and String1 return text from loaded resource. 我认为Thread.CurrentThread.CurrentUICulture = ...应强制重新加载资源文件,String1从加载的资源返回文本。

Here is the DemoProject https://yadi.sk/d/4zEWVhso3Q4eqV 这是DemoProject https://yadi.sk/d/4zEWVhso3Q4eqV

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

There are two problems with your settings: 您的设置存在两个问题:

  • You are putting your language resource files in a different folder than the neutral resource file. 您将语言资源文件放在与中性资源文件不同的文件夹中。 So the namespace would be different. 所以命名空间会有所不同。 Currently your neutral resource file is in Properties folder and the other resources are in root folder of the project. 目前,您的中性资源文件位于Properties文件夹中,其他资源位于项目的根文件夹中。

  • The first part of the the file names of the language resources should be the same as the neutral resource file. 语言资源的文件名的第一部分应与中性资源文件相同。 So since the neutral resource file name is Resources.resx , those language files should be Resources.ru.resx and Resources.fr.resx . 因此,由于中性资源文件名是Resources.resx ,因此这些语言文件应为Resources.ru.resxResources.fr.resx They lack s character at the end of file name. 他们缺乏s在文件名末尾字符。

After fixing those problems it will work well. 解决这些问题后,它将运作良好。

Apart from what you are trying to do, you can take a look at Windows Forms Localization feature: 除了您尝试执行的操作之外,您还可以查看Windows窗体本地化功能:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM