0

I need to localize my UWP application to German language.I am maintaining two resource files for English and German. I have added a button as follow.

                    <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>

When changing the language to German executes the following method.

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());
    }

However it doesn't change the text in the ui elements which used x:Uid and bind the text from resources file.

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

This is how the above button text refer in resources.resw file.

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

it only translates the words which binds from the code behind as below.

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

However if I restart the app only all the words(both UI and code behind) get translated to selected language.

How can I fix this.I need to translate the words once the language preference changes from the dropdown,without restarting the app.

5
  • I follow the official document Localize the string resources, and I did not reproduce your behavior i.sstatic.net/rscGh.gif . Have you check your default language in Package.appxmanifest? Commented Aug 16, 2023 at 4:21
  • @JunjieZhu-MSFT is your app UI binded using x:Uid and maintaining the texts on resource file? Commented Aug 16, 2023 at 6:44
  • Yes, I use x:Uid. Maybe you need to provide more detailed reproduction steps so that we can find the problem for you. The above is my guess, it may be related to the language setting, what is the default language of your project? Commented Aug 17, 2023 at 2:24
  • I use en-US as default and need to change to German Commented Aug 17, 2023 at 19:08
  • Could you please tell us about your steps to produce UsernameText? If you follow the official steps, can you reproduce the same problem? Commented Aug 23, 2023 at 8:12

1 Answer 1

0

Try to clear the cache before navigating to the page:

Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();  
Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.Reset();
Frame.Navigate(this.GetType());  
Sign up to request clarification or add additional context in comments.

1 Comment

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' getting this error when i use the given code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.