是否有任何理由不使用标准 resx+静态绑定来本地化 WPF?
我正在寻找一种非常简单的方法来将我的应用程序本地化为日语以及默认英语。唯一的要求是我们能够以指定的语言启动它。我们使用的 LocBaml 东西笨重、复杂、容易出错,并且使我们的构建过程变得极其困难。
我正在考虑将所有内容移回资源文件(Strings.resx、Strings.ja.resx)并仅进行静态绑定,如下所示:
<TextBlock Text="{x:Static resx:MyWindow.MessageText}" />
然后在启动时找出他们想要的语言并切换从中提取字符串的资源:
public static void Main(string[] args)
{
if (args[0] == "-lang")
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(args[i + 1]);
}
App app = new App();
app.InitializeComponent();
app.Run();
}
这很简单,似乎唯一真正的缺点是我们无法在运行时切换,这不是我们想要做的事情。我见过一些这样的本地化扩展:
http://wpflocalization.codeplex.com/
http://www.wpftutorial.net/LocalizeMarkupExtension.html
它们提供了更干净的 Xaml,并且在设计时看起来更好一些,但我看不到任何功能除了允许您在运行时更改语言之外,还有其他区别。我在这里错过了什么,还是我们应该选择简单且内置的路线?总共我们只有大约 100 个字符串需要本地化。我认为最简单的路线是最好的,特别是考虑到我们的应用程序相对简单。
I'm looking for a dead-simple way to get my app localized to Japanese as well as the default English. The only requirement is that we be able to launch it in a specified language. We were using the LocBaml stuff which is clunky, complicated, error-prone, and makes our build process exceedingly difficult.
I'm considering moving everything back to resource files (Strings.resx, Strings.ja.resx) and just doing static binding, like this:
<TextBlock Text="{x:Static resx:MyWindow.MessageText}" />
Then at launch time finding out what language they want and switching which resource it pulls strings from:
public static void Main(string[] args)
{
if (args[0] == "-lang")
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(args[i + 1]);
}
App app = new App();
app.InitializeComponent();
app.Run();
}
This is simple and it seems the only true drawback is that we cannot switch at runtime, which is not something we will ever want to do. I've seen a few localization extensions like these:
http://wpflocalization.codeplex.com/
http://www.wpftutorial.net/LocalizeMarkupExtension.html
They provide cleaner Xaml and look a bit nicer at design time, but I can't see any functional difference besides allowing you to change languages at runtime. Am I missing something here, or should we just go for the easy and built-in route? Sum total we only have ~100 strings that need to be localized. I think that the simplest route is the best here, especially considering the relative simplicity of our app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我肯定会推荐 resx 路线。我刚刚完成了一个大型 wpf 应用程序的构建,该应用程序将以多种语言进行本地化(目前只有 en_GB 和 it_IT,但很快就会推出更多语言环境),并且运行良好。
需要考虑的一些缺点:
如果你愿意的话,这是一个很好的解决方案
动态切换语言
(虽然这仍然可以实现
使用标记进行一些工作
扩展)。
你需要将资源放入
你的 resx 前期增加了一个小
一点开销
本地化字符串(其中 locBaml,如
据我所知,你几乎可以
本地化所有元素属性)
在我们看来,resx 方法的小缺点远远超过了 locBaml 的缺点。
需要注意的是,我没有在完整的构建项目中使用 locBaml 方法。我的情况和你一样,必须研究这两种方法。事后看来,这对我们来说绝对是正确的决定。
I'd definitely recommend the resx route. I've just finished building a large wpf application that will be localised in a variety of languages (currently just en_GB and it_IT but will be pushing more locales out shortly) and it's worked perfectly.
Some drawbacks to consider:
great solution if you wanted to
switch languages dynamically
(although this can still be achieved
with a bit of work using markup
extensions).
you need to place resources in
your resx upfront which adds a small
bit of overhead
localising strings (where locBaml, as
far as i'm aware, you can pretty much
localise all element properties)
In our respects the minor draw backs of the resx approach by far trumped the drawbacks of locBaml.
One caveat is I've not used the locBaml approach on a full build project. I was in the same situation as you and had to investigate both approaches. In hindsight it was definitely the correct decision for us.
我们使用 WPF 本地化扩展。它提供本地化字符串的运行时切换和设计时查看。
使用 resx 的好处是它具有良好的后备功能(例如 de-DE、de、默认资源)。此外,locBaml 的缺点是它使用 CSV 文件,并会导致所有可能导致的问题(例如需要转义包含逗号的字符串)。此外,在该工具运行后,必须对强名称签名的程序集进行签名。
We use the WPF localization extension. It provides runtime switching and design-time viewing of the localized strings.
The nice part about using resx is that it has good fallback (ex. de-DE, de, default resource). Also locBaml has the disadvantage that it uses a CSV file, with all the problems that can cause (such as needing to escape strings which contain commas). Also, strong-name signed assemblies have to be resigned after the tool has run.