简体   繁体   English

C#创建多语言设置

[英]C# Creating a setup for multi-language

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. 添加一个新的资源文件并将其命名为formMain.Strings.resx然后我将为每个应该翻译的字符串输入名称,值对。 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.Designer.cs的文件会在resx设计器的每个关闭时自动重新生成。

Add another resource with name formMain.Strings.de-DE.resx. 添加名为formMain.Strings.de-DE.resx的另一个资源。 Add the same Name key's from previous resource, and just change the Value with coresponding german words. 从以前的资源添加相同的Name键,只需使用相应的德语单词更改Value。 Now to access created resource from the source it will be like this. 现在要从源访问创建的资源,它将是这样的。

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. 所以我已经将资源文件和th-TH dll添加到项目设置中。 And I still get the same problem. 我仍然遇到同样的问题。

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. 因为在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. 我保留默认值的每个文件的所有属性。

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());
    }

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, 谢谢,

Try adding in this at the app's startup (if it's not there): 尝试在应用程序的启动时添加(如果它不存在):

Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;

Here is a short article discussing 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(如果主程序集是库) 。卫星程序集文件总是位于applciation主目录的语言特定子目录上。如果应用程序文件是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. 在您的情况下,它应该位于您的可执行文件下具有适当名称的th-TH子目录中。 If it's there, it should be found and used properly. 如果它在那里,应该找到并正确使用它。

Here is another good source of information about this topic. 这是关于该主题的另一个很好的信息来源

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

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