在没有 LocBAML 的情况下本地化 WPF 应用程序的最佳方法是什么?
似乎没有本地化 WPF 应用程序的好方法。 MSDN 似乎认为,在我的 XAML 中添加 x:Uid
,生成 CSV 文件,然后生成新的程序集(使用其示例代码!)就是答案。 更糟糕的是,这个过程没有解决如何本地化嵌入在代码中的图像、二进制 blob(例如 PDF 文件)或字符串。
那么,您如何本地化一个应用程序:
- 包含多个程序集
- 包含需要本地化的图像和其他二进制 blob(例如:PDF 文档)
- 包含 XAML 中没有的字符串数据(例如:
MessageBox.Show("你好世界”);
)
There seems to be no good way to localize a WPF application. MSDN seems to think that littering my XAML with x:Uid
's, generating CSV files, and then generating new assemblies (using their sample code!) is the answer. Worse, this process doesn't address how to localize images, binary blobs (say, PDF files), or strings that are embedded in code.
So, how might you localize an application that:
- Contains several assemblies
- Contains images and other binary blobs (eg: PDF docs) that need to be localized
- Has string data that isn't in XAML (eg:
MessageBox.Show("Hello World");
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该查看此处提供的文章和代码。 它描述了使用 LocBaml、自定义标记扩展或附加属性来本地化 WPF 应用程序的不同方法。 恕我直言,最好的解决方案是使用标记扩展和 Resx 资源。 该代码包含一个用于执行此操作的本地化框架。
You should have a look at the article and code available here. It describes different ways of localizing WPF apps, using LocBaml, custom markup extensions, or attached properties. IMHO the best solution is to use the markup extensions and Resx resources. The code contains a localization framework for doing that.
虽然不是专家,但在 xaml 中使用 x:Uids 并不比在 Windows 窗体代码中使用本地化字符串表的所有废话更糟糕。
据我了解,WPF 应用程序仍然支持“所有框架 CLR 资源,包括字符串表、图像等”。 这意味着您可以拥有本地化资源。
当然,如果您创建一个标记扩展来为您处理大部分这些废话,事情就会简单得多。 您可以在此处找到某人执行此操作的示例。 在
http://blog.taggersoft 上还有另一个类似的解决方案。 com/2008/07/wpf-application-localization-pattern_29.html
,但该链接不再有效。Not an expert here, but "littering" your xaml with x:Uids is not worse than "littering" your Windows Forms code with all the string table nonsense you have to do for localizing them.
As far as I understand, WPF apps still support "all the Framework CLR resources including string tables, images, and so forth." which means you can have localized resources.
Of course, it would be much simpler if you created a markup extension that handled much of this nonsense for you. You can find an example of someone doing this here. And there was another, similar solution at
http://blog.taggersoft.com/2008/07/wpf-application-localization-pattern_29.html
, but that link no longer works.您可以使用旧的“ResX”文件,它支持您提到的所有场景。 这里解释了如何在 WPF 应用程序中实现这一点:
WPF 应用程序框架 (WAF) => 查看本地化示例
You can use the old "ResX" files which support all of your mentioned scenarios. How this can be accomplished in a WPF application is explained here:
WPF Application Framework (WAF) => See Localization Sample
尝试 Gnu GetText 实用程序和支持应用程序。 它确实基于 ResourceManager 和 ResourceSets生成 C# 类,并且当然,您可以将翻译重复用于应用程序的其他部分 - 例如网页、本机代码或 iPhone 等。
try the Gnu GetText utilities, and supporting applications. It does generate C# classes based around ResourceManager and ResourceSets, and of course you can reuse the translations for other parts of your application - eg web pages, native code, or iphone etc.
我对自己的 WPF 应用程序也有同样的问题,并决定编写一个轻量级本地化库。 它允许翻译 xaml 资源字典文件并在运行时在支持的语言之间切换。
您可以查看 Armat.Localization GitHub 存储库了解更多详细信息,并参考“armat.localization.localization”。 wpf”WPF 应用程序中的 NuGet 包。 使用适当的演示应用程序克隆/构建源代码将使您充分了解 Armat.Localization 库的工作原理。
主要思想如下:
Localization.Core 和 Localization.Wpf 项目将指导您完成一般使用模式,并且 Localization.Demo 应用程序将为您提供可本地化的 WPF 项目示例。
I had the same issue with own WPF application and decided to write a lightweight localization library. It allows translating xaml resource dictionary files and switching between supported languages at runtime.
You can look into Armat.Localization GitHub repository for more details and refer to "armat.localization.wpf" NuGet package in WPF applications. Cloning / Building the source code with appropriate demo application will give you enough understanding about how the Armat.Localization library works.
The main idea is following:
Markdown files in Localization.Core and Localization.Wpf projects will guide you through the general usage patterns, and the Localization.Demo application will serve you as a sample localizable WPF project.