字符串的 WPF 本地化 - 在不同项目之间共享?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13315961/
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
WPF localization of strings - shared between different projects?
提问by cardinalPilot
I'm developing a WPF C# app under VS2012 where I have UserControls from different sources in separate projects in the solution. I would like to localize all the UI strings in the entire solution in one place, if I can.
我正在 VS2012 下开发一个 WPF C# 应用程序,其中我在解决方案的不同项目中拥有来自不同来源的 UserControl。如果可以,我想将整个解决方案中的所有 UI 字符串本地化到一个地方。
It seems like locbaml isn't really set up for this kind of thing since it appears for every class library, I need to create a separate resource DLL for each class library DLL?
看起来 locbaml 并没有真正为这种事情设置,因为它出现在每个类库中,我需要为每个类库 DLL 创建一个单独的资源 DLL?
Can anyone give me some guidance on the Best Practice way to do this? I'm at a loss to figure out how to apply the various localization techniques to a solution with multiple projects.
谁能给我一些关于最佳实践方法的指导?我不知道如何将各种本地化技术应用于具有多个项目的解决方案。
Thanks for any advice. Corey.
感谢您的任何建议。科里。
回答by ianschol
Revising my post a bit to be more inclusive:
稍微修改我的帖子以使其更具包容性:
Overarching best practices are detailed on the MSDN here.
Really, you have three choices.
真的,你有三个选择。
- Localize it yourself - hand-roll your own XML/text file/database solution.
- Use locbaml to localize. This does not allow dynamic language switching in-app.
- Use resx files to localize. This does allow dynamic language switching in-app, depending on implementation chosen.
- 自己本地化 - 手动滚动您自己的 XML/文本文件/数据库解决方案。
- 使用 locbaml 进行本地化。这不允许应用内动态语言切换。
- 使用 resx 文件进行本地化。这确实允许在应用程序内进行动态语言切换,具体取决于所选的实现。
There are plenty of scenarios where locbaml is optimal, which are outlined on the best practices page, but resx files are reasonably portable (the format is simple) and will fulfill a requirement for dynamic language swapping.
有很多 locbaml 是最佳的场景,这些在最佳实践页面中有概述,但是 resx 文件具有合理的可移植性(格式很简单)并且将满足动态语言交换的要求。
If you decide to go with (1), you're on your own. Godspeed! :)
如果您决定采用 (1),那么您就靠自己了。神速!:)
If you choose to use locbaml (2), it's best to approach the problem by first abstracting your strings into a ResourceDictionary, allowing you to put all strings into one file for localizing. See here : How to: Use a ResourceDictionary to Manage Localizable String Resources
如果您选择使用 locbaml (2),那么最好首先将您的字符串抽象到一个 ResourceDictionary 中来解决这个问题,这样您就可以将所有字符串放入一个文件中进行本地化。请参阅此处:如何:使用 ResourceDictionary 管理可本地化的字符串资源
The linked article details still using locbaml, but you should only need to use it to localize the single ResourceDictionary. Keeping the resources neatly isolated in this fashion should make your life a lot easier. Note that if desired, the ResourceDictionaries can be split categorically - just a matter of whether one monolithic file or multiple smaller files are better for your project.
链接的文章详细信息仍然使用 locbaml,但您应该只需要使用它来本地化单个 ResourceDictionary。以这种方式将资源整齐地隔离开来应该会让您的生活更轻松。请注意,如果需要,可以对 ResourceDictionaries 进行分类拆分 - 只是一个整体文件还是多个较小的文件更适合您的项目的问题。
If you choose to use resx files (3) and want to support painless run-time language switching, this short tutorial\example code provides an excellent starting point.It's written with Prism in mind, but can be used outside of Prism with no difficulties. There are other, simpler approaches that also use resx files, but if you want the bestapproach, IMHO you are looking at it. The one (maybe?) major caveat is that this approach will not work on Silverlight. This is a WPF question but I realize it may be a concern for some people.
如果您选择使用 resx 文件 (3) 并希望支持无痛的运行时语言切换,这个简短的教程\示例代码提供了一个很好的起点。它是在考虑 Prism 的情况下编写的,但可以在 Prism 之外轻松使用。还有其他更简单的方法也使用 resx 文件,但如果您想要最好的方法,恕我直言,您正在查看它。一个(也许?)主要的警告是这种方法不适用于 Silverlight。这是一个 WPF 问题,但我意识到这可能是某些人关心的问题。
回答by Jaska
WPF uses two resource formats: XAML and RESX. unfortunately almost all best practices and also all answers here recommend to replace plain and simple strings in XAML with complex and hard to maintain references to RESX strings.
WPF 使用两种资源格式:XAML 和 RESX。不幸的是,几乎所有最佳实践以及此处的所有答案都建议将 XAML 中的简单字符串替换为复杂且难以维护的对 RESX 字符串的引用。
For example if you had
例如,如果你有
<TextBlock Text="Hello World"/>
then the "best practices" recommend to get rid of the XAML string, add it into RESX, and finally replace the XAML with reference
那么“最佳实践”建议去掉 XAML 字符串,将其添加到 RESX 中,最后用引用替换 XAML
<TextBlock Text="{Binding Path=Resource1.HelloText, Source={StaticResource LocalizedString }}"/>
+
<data name="LocalizedString" xml:space="preserve">
<value>Hello World</value>
</data>
Do not do that. Put RESX only those string what are used in your code. Leave all strings of the XAML as they are. Localized both RESX and XAML files. Finally compile satellite assembly files that contains localized RESX and XAML.
不要那样做。仅将 RESX 放在代码中使用的那些字符串。将 XAML 的所有字符串保持原样。本地化了 RESX 和 XAML 文件。最后编译包含本地化的 RESX 和 XAML 的附属程序集文件。
Visual Studio or locbaml can only localize RESX. You can localize XAML manually using a text editor but I do not recommend to do that. The localized XAML must be identical to the original. This means that the structure must be the same. It must contains all the components, properties and event as the original XAML. The only worable way to localize XAML is to use some localization tool.
Visual Studio 或 locbaml 只能本地化 RESX。您可以使用文本编辑器手动本地化 XAML,但我不建议这样做。本地化的 XAML 必须与原始 XAML 相同。这意味着结构必须相同。它必须包含原始 XAML 的所有组件、属性和事件。本地化 XAML 的唯一可行方法是使用一些本地化工具。
You have to be carefull when choosing the localization tool. It must performs following two things.
选择本地化工具时必须小心。它必须执行以下两件事。
- Localize XAML and RESX
- Compile localized XAML and RESX into satellite assembly files.
- 本地化 XAML 和 RESX
- 将本地化的 XAML 和 RESX 编译为附属程序集文件。
Once you use such a localization tool your localization does not only get much more easier but you can also place your user control either in its own assembly or link it to all projects that use it.
一旦您使用了这样的本地化工具,您的本地化不仅变得更加容易,而且您还可以将您的用户控件放置在其自己的程序集中或将其链接到使用它的所有项目。

