C#和WPF语言支持

huangapple go评论75阅读模式
英文:

C# & WPF Language support

问题

我正在处理一个现有的解决方案,各处都有硬编码的字符串。我们希望提供应用程序的多语言版本,因此需要在两个地方本地化C#代码和WPF界面:

App.Core

该项目是使用C#编写的,包含ViewModel等内容。

它还负责触发通知(Windows系统通知和自定义UI通知),并提供数据给WPF项目,因此这些字符串需要本地化。

App.Windows

该项目包括WPF和C#类的混合。

它包含UI界面、按钮标签等。这些值来自该项目内部或由App.Core项目提供。

我已经查看了一些关于如何本地化的文章,但它们似乎只涵盖了本地化C#或WPF的其中一个,而不是两者的结合。我尝试了一些方法,但对它们不太满意,也许它们适用于C#,但不适用于WPF,反之亦然。

理想情况下,我们希望用户能够在应用程序运行时更改语言,因此需要确保这是可能的(以某种方式)。

*我们目前只打算使用少数几种语言(荷兰语、英语、法语、德语)

任何帮助、建议或意见将非常受欢迎。谢谢!

英文:

I am working on an existing solution and there are hard-coded strings all over the place. We are wanting to offer a multi-language version of the app*, so need to localise in both C# code and WPF in 2 places:

> App.Core
>
> This project is C# and contains ViewModels etc.
>
> It is also responsible for raising notifications (Windows System
> notifications & custom UI notifications) and provides data to the WPF
> project, so these strings would need to be localised.

> App.Windows
>
> This project is a mix of WPF and C# classes.
>
> It has UI interfaces, buttons labels etc. The values for these come
> from within this project or are provided by the App.Core project.

I have looked at a few articles on how to localise (localize) but they seem to cover either localising C# or WPF, not really a combination of the two. I have tried some approaches out, but am not completely happy with them, maybe they work for C# but not for WPF and vice-versa.

Ideally, we want the user to be able to change their language while the app is running, so need to make sure this is possible (somehow)

*We are only expecting to use a handful of languages at this point (Dutch, English, French, German)

Any help, suggestions or advice would be very welcomed. TIA

答案1

得分: 1

处理UI字符串的最常见方式是使用resx文件。这使它们在代码中可用,并且它们也应该在xaml文件中可访问。如果您有多个项目,可能需要在属性中配置'custom tool'为PublicResXFileCodeGenerator以使生成的文件变为公共文件。

如果您希望在更改语言时获得实时更新,您可以在绑定xaml代码中的字符串时使用类似WPFLocalizeExtension的东西。如果您在代码中使用UI字符串,您需要确保在更改语言时更新所有字符串。据我所知,没有内置或方便的方法来实现这一点。

英文:

The most common way to handle UI strings is in resx-files. This makes them available both in code, and they should also be accessible in xaml-files. If you have multiple projects you may need to configure the 'custom tool' in the properties to PublicResXFileCodeGenerator to make the generated files public.

If you want real time update when changing language you could use something like WPFLocalizeExtension when binding to strings in xaml code. If you are using UI strings in code you need to ensure all strings are updated when the language is changed. There is as far as I know no builtin or convenient way to do this.

huangapple
  • 本文由 发表于 2023年2月23日 22:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定