UWP 应用程序 UI 元素未本地化

问题描述 投票:0回答:1

我需要将我的 UWP 应用程序本地化为德语。我正在维护英语和德语的两个资源文件。我添加了一个按钮,如下所示。

                    <Button
                    x:Name="localizationButton"
                    Width="32"
                    Height="32"
                    Padding="0"
                    Click="SettingsButton1_Click">
                    <Image
                        Margin="4"
                        Source="ms-appx:///Assets/Images/Common/AppBarIcons/LocalisationIcon.png"
                        Stretch="Uniform" />
                    <Button.Flyout>
                        <MenuFlyout x:Name="DropdownFlyout">
                            <MenuFlyoutItem Text="English" Click="MenuFlyoutItem_Click_En" />
                            <MenuFlyoutItem Text="Deutsch " Click="MenuFlyoutItem_Click_De" />
                        </MenuFlyout>
                    </Button.Flyout>
                </Button>

将语言更改为德语时执行以下方法。

private async void MenuFlyoutItem_Click_De(object sender, RoutedEventArgs e)
    {
        Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "de-DE";
        ApplicationData.Current.LocalSettings.Values["LanguagePreference"] = "de-DE";
        ChangeLanguage("de-DE");

        await Task.Delay(100);
        Frame.Navigate(this.GetType());
    }

但是,它不会更改使用 x:Uid 的 ui 元素中的文本并绑定资源文件中的文本。

                                   <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    FontSize="{StaticResource PrimaryFontSize16}"
                                    FontWeight="SemiBold"
                                    Style="{StaticResource PrimaryTextBlockStyle}"
                                    x:Uid="/Util/Resources/UsernameText"
                                    TextTrimming="CharacterEllipsis"
                                    TextWrapping="NoWrap" />

这就是上面的按钮文本在 resources.resw 文件中的引用方式。

<data name="UsernameText.Text"
        xml:space="preserve">
<value>Username</value>

它仅翻译从后面的代码绑定的单词,如下所示。

 LblLoginSubHeader.Text = ResourceHandler.Get("info_262");

但是,如果我重新启动应用程序,则只有所有单词(UI 和后面的代码)都会翻译为所选语言。

我该如何解决这个问题。一旦语言首选项从下拉列表中更改,我需要翻译单词,而无需重新启动应用程序。

windows uwp localization uwp-xaml globalization
1个回答
0
投票

在导航到该页面之前尝试清除缓存:

Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();  
Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.Reset();
Frame.Navigate(this.GetType());  
© www.soinside.com 2019 - 2024. All rights reserved.