如何在 XAML 中显示本地化文本

Ton*_*ile 1 .net c# wpf xaml localization

我有一个 WPF 应用程序,我需要将其本地化为西班牙语。我已经阅读了 MSDN 文档,但我很困惑。这很复杂,因为我们公司有一套三个程序一起工作(一个是 Windows 服务,一个是 Web 应用程序,第三个是我的 WPF 应用程序),我们决定构建一个包含可本地化的资源 DLL所有三个产品的字符串。

因此字符串位于资源 DLL 中,将其命名为 resources.dll。现在,原始英文字符串在 XAML 中进行了硬编码。我已经为表单的每个字符串创建了资源 ID ControlType_StringName。被多个控件使用的字符串被命名为Common_StringName

如何编码 XAML 以显示正确的字符串?例如,我有一个关于框。它使用一个名为“About_Copyright”的字符串。在关于框中,有一个TextBlock显示此字符串。我在Text属性值中输入什么?

<TextBlock x:Uid="TextBlock_4" FontSize="18"
           FontWeight="Bold"
           Foreground="{DynamicResource TextForeground}"
           Grid.Row="5"
           HorizontalAlignment="Center"
           Margin="5"
           Text="Copyright © 2012, 2013, Elsag North America, Inc." />
Run Code Online (Sandbox Code Playgroud)

完成后,Text属性中应该有一个引用,引用资源中的“AboutBox_Copyright”字符串。

并且在我的程序资源的资源 DLL 中有一个单独的静态类,称为 CarSystem,因此代码隐藏会将该字符串称为“CarSystem.AboutBox_Copyright”。

Ham*_*yan 5

添加对您的资源 dll 的引用为

xmlns:res="clr-namespace:LocalizationDll.Properties"
Run Code Online (Sandbox Code Playgroud)

并在您的 UI 中使用它作为

Text="{x:Static res:Resources.AboutBox_Copyright}"
Run Code Online (Sandbox Code Playgroud)

请注意,您必须将 UI 文化设置为

System.Threading.Thread.CurrentThread.CurrentUICulture = 
            new System.Globalization.CultureInfo("es-ES");
Run Code Online (Sandbox Code Playgroud)