C# 创建多语言设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:38:17  来源:igfitidea点击:

C# Creating a setup for multi-language

c#installationmultilingual

提问by ant2009

I have added multi-language using the short article below.

我使用下面的短文添加了多语言。

When you add for example German language you will have these files: formMain.resx formMain.de-DE.resx formMain.Designer.cs formMain.cs

例如,当您添加德语时,您将拥有以下文件: formMain.resx formMain.de-DE.resx formMain.Designer.cs formMain.cs

In first file you will have resources for neutral language, like strings, images, ..

在第一个文件中,您将拥有中性语言的资源,例如字符串、图像、..

So now you need to add also resources for strings used in code. Add a new resource file and name it formMain.Strings.resx Then i will enter name, value pair for every string that should be translated. When you add resource file then it is automatically typed because another file with name formMain.Strings.Designer.cs is automatically regenerated on every close of resx designer.

所以现在您还需要为代码中使用的字符串添加资源。添加一个新的资源文件并将其命名为 formMain.Strings.resx 然后我将为每个应该翻译的字符串输入名称,值对。当您添加资源文件时,它会自动键入,因为在每次关闭 resx 设计器时都会自动重新生成另一个名为 formMain.Strings.Designer.cs 的文件。

Add another resource with name formMain.Strings.de-DE.resx. Add the same Name key's from previous resource, and just change the Value with coresponding german words. Now to access created resource from the source it will be like this.

添加另一个名为 formMain.Strings.de-DE.resx 的资源。从以前的资源中添加相同的 Name 键,只需使用相应的德语单词更改 Value。现在要从源访问创建的资源,它将是这样的。

MessageBox.Show(formMain_Strings.SameStringName);

MessageBox.Show(formMain_Strings.SameStringName);

However, I have changed my to Thai language. Everything works fine when I run my app in VS.

但是,我已将我的语言更改为泰语。当我在 VS 中运行我的应用程序时,一切正常。

However, as soon as I add a setup project and install on the clients machine it won't change the language to Thai and just keeps to the default language.

但是,一旦我添加了一个安装项目并安装在客户端机器上,它就不会将语言更改为泰语,而只会保持默认语言。

So I have added the resource files and the th-TH dll to the project setup. And I still get the same problem.

所以我已经将资源文件和 th-TH dll 添加到项目设置中。我仍然遇到同样的问题。

Packaging file 'Lang.Strings.resx'...
Packaging file 'MultiLanguage.resources.dll'...
Packaging file 'MultiLanguage.exe'...
Packaging file 'Lang.Strings.th-TH.resx'...

As everything works fine when running in visual studio. Is there something I need to do to get it to run once its been installed. All the properties for each of the file I have keep the default.

因为在 Visual Studio 中运行时一切正常。我需要做些什么才能让它在安装后运行。我拥有的每个文件的所有属性都保留默认值。

Many thanks,

非常感谢,

=========

==========

static void Main()
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = 
new System.Globalization.CultureInfo("th-TH");

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

采纳答案by ant2009

I found the answer

我找到了答案

Click the Setup Project in Solution Explorer and then click Add\Project Output\ . From dialog select the project for which you want to include localization (satelite) assemblies and then select Localized resources.

单击解决方案资源管理器中的安装项目,然后单击添加\项目输出\。从对话框中选择要包含本地化(卫星)程序集的项目,然后选择本地化资源。

After the installation in the folder that I install to, I have the th-TH folder which includes the satellite assembly.

在我安装到的文件夹中安装后,我有包含卫星程序集的 th-TH 文件夹。

Thanks,

谢谢,

回答by Reed Copsey

Try adding in this at the app's startup (if it's not there):

尝试在应用程序启动时添加(如果它不存在):

Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;

Here is a short articlediscussing some of the options of how to make this work, and options for selecting the locale at runtime.

这是一篇简短的文章,讨论了如何实现这一点的一些选项,以及在运行时选择语言环境的选项。



Edit after comments:

评论后编辑:

Make sure your satellite assembly is in the appropriate place, and built correctly. From that article I referenced:

确保您的卫星组件位于适当的位置,并正确构建。从那篇文章中我引用了:

"When .NET runtime starts an application it looks for a possible satellite assembly file. A satellite assembly file is a resource only assembly file that has .resources.dll extension instead of .exe ir .dll (if the main assembly is a library). Satellite assembly files always locate on a language specific sub directory of the applciation's main directory. If application file is Converter.exe then the Japanese satellite assembly file is ja\Converter.resources.dll."

“当 .NET 运行时启动一个应用程序时,它会寻找一个可能的附属程序集文件。附属程序集文件是一个只有资源的程序集文件,它具有 .resources.dll 扩展名而不是 .exe ir .dll(如果主程序集是一个库) . 附属程序集文件总是位于应用程序主目录的特定语言子目录中。如果应用程序文件是 Converter.exe,那么日语附属程序集文件是 ja\Converter.resources.dll。

There are a few things that you should check here. Check the name of the assembly. Also, make sure it's in the proper location. In your case, it should be in a th-TH subdirectory with the appropriate name under your executable. If it's there, it should be found and used properly.

有几件事你应该在这里检查。检查程序集的名称。另外,请确保它位于正确的位置。在您的情况下,它应该在您的可执行文件下具有适当名称的 th-TH 子目录中。如果它在那里,它应该被找到并正确使用。

Here is another good source of informationabout this topic.

这是有关此主题的另一个很好的信息来源