如何使用 RESX 本地化 WPF .net-core 应用程序

How to localize WPF .net-core application with RESX

我想用 .resx 文件本地化 .Net Core 应用程序。谁能给我一个完整的分步解决方案?我是 WPF 和 .Net-Core 的新手。当我更改代码中的当前文化时,什么也没有发生。 这是我的代码:

<ToolBar>
    <Button Content="{x:Static strings:Resource.NewCustomer}" Command="{Binding NewCustomerDelegateCommand}"/>
</ToolBar>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
        base.OnStartup(e);
    }
}

我用这个解决方案解决了一个类似的问题:

  1. Created a new Resource.resx. (Using Add>New Item...>Resource File.resx .
  2. Copy all contents of the first file in the new file (Copy all three columns).
  3. Delete the old file.
  4. Rename the new file to the intended name. 6. Don't forget to change access modifier of resx file to "public"

您也可以使用这个 ASP.NET 解决方案 https://developpaper.com/dotnetcore3-1-multi-language-implementation-of-wpf/

本地化 WPF 应用程序时,您有多种选择。

  1. 使用本机 WPF 的方法(使用 UID),请参阅:MSDN。这种做法有弊端在文章中解释的比较详细,但是笼统地说:

    • 它没有使用 resx(这对大多数开发人员来说很自然)
    • 不支持在运行时更改语言
  2. 使用由 PublicResXFileCodeGenerator / ResXFileCodeGenerator 生成的 resx 的强类型资源 class。

    (请参阅 resx 文件的 VS 属性 window 中的自定义工具;PS:第二个生成器创建内部 class,因此请记住它将是仅可在 xaml 被本地化的同一程序集中使用,并且仅适用于 WPF 绑定,不适用于静态标记扩展)

    这种方法类似于您尝试应用的方法,这意味着在 xaml 中您使用绑定(或 x:Static)到生成的强类型资源的属性 class。这种方法的缺点是生成的强类型资源 class 不会在 culture/language 发生更改时提供任何通知。换句话说,你可以提供字符串、标签等,但它们会在 xaml 为 parsed/loaded 时解析。之后就变成固定内容了:

    • 不支持在运行时更改语言
  3. 您可以在市场上找到使用 resx 文件本地化 WPF 应用程序的现有解决方案,例如看看:https://www.qube7.com/guides/localization.html

    • 它支持在运行时更改语言(UI 当您设置 Culture.Current 时自动刷新)
    • 该解决方案提供了本地化 xaml 的方法,该方法在 VS 设计器中得到支持
    • 本地化方法 xaml 类似于本地化 asp.net:您可能拥有本地资源(每个 xaml 视图的 resx)+您可能拥有全局资源(保存共享资源的 resx )
    • 解决方案还为您提供了本地化视图模型的方法(这也很常见)

    您可以在此处找到资源标记扩展的完整源代码:Resource.cs