在没有 LocBAML 的情况下本地化 WPF 应用程序的最佳方法是什么?

发布于 2024-07-06 01:15:02 字数 330 浏览 8 评论 0原文

似乎没有本地化 WPF 应用程序的好方法。 MSDN 似乎认为,在我的 XAML 中添加 x:Uid,生成 CSV 文件,然后生成新的程序集(使用其示例代码!)就是答案。 更糟糕的是,这个过程没有解决如何本地化嵌入在代码中的图像、二进制 blob(例如 PDF 文件)或字符串。

那么,您如何本地化一个应用程序:

  1. 包含多个程序集
  2. 包含需要本地化的图像和其他二进制 blob(例如:PDF 文档)
  3. 包含 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:

  1. Contains several assemblies
  2. Contains images and other binary blobs (eg: PDF docs) that need to be localized
  3. Has string data that isn't in XAML (eg: MessageBox.Show("Hello World");)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

治碍 2024-07-13 01:15:02

您应该查看此处提供的文章和代码。 它描述了使用 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.

无人接听 2024-07-13 01:15:02

虽然不是专家,但在 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.

故事和酒 2024-07-13 01:15:02

您可以使用旧的“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

記柔刀 2024-07-13 01:15:02

尝试 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.

拒绝两难 2024-07-13 01:15:02

我对自己的 WPF 应用程序也有同样的问题,并决定编写一个轻量级本地化库。 它允许翻译 xaml 资源字典文件并在运行时在支持的语言之间切换。

您可以查看 Armat.Localization GitHub 存储库了解更多详细信息,并参考“armat.localization.localization”。 wpf”WPF 应用程序中的 NuGet 包。 使用适当的演示应用程序克隆/构建源代码将使您充分了解 Armat.Localization 库的工作原理。

主要思想如下:

  • 将 WPF 控件中的所有可本地化内容提取到 WPF 资源字典文件中
  • 通过将根 xaml 元素更改为“Armat.Localization.Wpf.LocalizedResourceDictionary”,使资源字典可本地化
  • 使用本地化设计器(来自同一存储库) )为所有支持的语言创建翻译文件
  • 在 WPF 应用程序中提供语言选择器,用于在支持的语言之间切换

Localization.CoreLocalization.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:

  • Extract all localizable contents from WPF controls into WPF Resource Dictionary file(s)
  • Make Resource Dictionaries localizable by changing the root xaml element to "Armat.Localization.Wpf.LocalizableResourceDictionary"
  • Use the Localization Designer (from the same repo) to create translation file for all supported languages
  • Provide a language selector in WPF application for switching between supported languages

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文