如何将 C# .NET 应用程序转换为支持多种语言?
我有一个 C# 应用程序,需要将其转换为支持英语和西班牙语,是否有一种半简单的方法可以添加该应用程序并能够在以后添加其他语言?
I have a C# application that I need to convert to support English and Spanish, is there a semi easy way to add that in and be able to add other languages later on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的!它称为资源 (.resx) 文件。您要做的就是:
Localized
属性更改为true
。这将使设计人员从 .resx 文件中获取文本和其他属性,而不是对它们进行硬编码。Language
属性更改为其他语言,例如西班牙语。瞧!你完成了!
Yes! It's called resource (.resx) files. What you do is this:
Localizable
property of your localizable forms totrue
. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.Language
property of the form to the other language, let's say Spanish.And voilà! You're done!
您将所有表单和控件标记为可本地化。这会将所有 UI 相关文本(标签等)放入资源文件中。如果您需要在代码中创建字符串,则可以使用字符串资源文件并通过资源键查找字符串(例如
StringResource.Get("My_Message")
)。然后你可以使用一个工具来翻译你的所有资源。通常,您为每种语言创建一个本地化的 .dll。我们使用 Passolo 来完成此任务,但还有其他工具。You mark all your forms and controls as localizable. This will put all UI related text (labels etc.) in resource files. If you need to create strings in code then you use string resource files and look up the string by the resource key (e.g
StringResource.Get("My_Message")
). Then you can use a tool to translate all your resources. Typically you create a localized .dll for each language. We use Passolo for that but there are other tools around.您可以通过两种方式制作多语言应用程序:
通过使应用程序可本地化,因此当用户更改设备的文化时,如果您已将此语言添加到支持的语言中,应用程序将自动切换到文化的 UI应用。
您可以通过将项目上每个表单的 Localized 属性设置为 Localizable,然后将 UI 更改为新的区域性来执行此操作。
You can make a multilingual application in two ways:
By making the application Localizable, so when the user changes the culture of the device, the application will switch automatically to culture's UI if you added this language already to the supported languages in the application.
You can perform this by setting each form's Localizable property on the project to Localizable, and then changing the UI to the new culture.
By making a language option and a resource file (.resx) for each added language in your application, and depending on the selected language, you can load the images or the strings from selected language's resource file.
无需安装任何第 3 方工具、API 或 dll 对象,我就可以使用 App_LocalResources。尽管我仍然使用谷歌翻译来翻译单词和句子,并将其复制并粘贴到文件中,如下面的屏幕截图之一所示(或者您可以找个人翻译并手动输入来添加)。在您的项目文件夹(使用 MS Visual Studio 作为编辑器)中,添加 App_LocalResources 文件夹并创建英语和其他语言(resx 文件)。就我而言,它是西班牙语 (es-ES) 翻译。请参阅下面的屏幕截图。

接下来,在您的 aspx 上,添加将在 App_LocalResources 中匹配的元标记 (meta:resourcekey)。一个用于英语文件,另一个用于西班牙语文件。请参阅下面的屏幕截图:
西班牙语:(filename.aspx.es-ES.resx)
英语:(filename.aspx.resx)
。
然后使用查询字符串在母版页文件上创建一个链接,该链接将切换页面翻译并将在所有页面上可用:
。
在母版页代码后面,创建一个指向超链接标记的动态链接:
。
现在,在页面文件的代码后面,您可以设置一个会话变量,通过始终向网址添加查询字符串,使所有链接或重定向坚持所需的翻译。
在页面加载时:
.
单击事件示例:
我希望我的描述足够简单。
Without installing any 3rd party tool, APIs, or dll objects, I am able to utilize the App_LocalResources. Although I still use Google Translate for the words and sentences to be translated and copy and paste it to the file as you can see in one of the screenshots below (or you can have a person translator and type manually to add). In your Project folder (using MS Visual Studio as editor), add an App_LocalResources folder and create the English and other language (resx file). In my case, it's Spanish (es-ES) translation. See screenshot below.

Next, on your aspx, add the meta tags (meta:resourcekey) that will match in the App_LocalResources. One for English and another to the Spanish file. See screenshots below:
Spanish: (filename.aspx.es-ES.resx)
English: (filename.aspx.resx)
.
Then create a link on your masterpage file with a querystring that will switch the page translation and will be available on all pages:
.
On your masterpage code behind, create a dynamic link to the Hyperlink tags:
.
Now, on your page files' code behind, you can set a session variable to make all links or redirections to stick to the desired translation by always adding a querystring to urls.
On PageLoad:
.
On Click Events sample:
I hope my descriptions were easy enough.