C# change app language programmatically UWP realtime

倖福魔咒の 提交于 2019-12-17 18:18:12

问题


In my application for each language string resources are stored separately and are displayed depending of type of language environment. I want to change the language in the application settings. How do I realize that after the language selection instantly apply it in the user interface?


回答1:


We can use ApplicationLanguages.PrimaryLanguageOverride to change the language during runtime without restart the app.

For example: I have two languages supported "en" and "fr", localized message will show up in textblock.

  1. Add using Windows.Globalization;

  2. Change the default language from "en" to "fr" by

    ApplicationLanguages.PrimaryLanguageOverride = "fr";
    
  3. Re-navigate to the current page to refresh the UI.

    Frame.Navigate(this.GetType());
    

Note that, you need to compare the PrimaryLanguageOverride with the system culture to set the language for next app launch, because the PrimaryLanguageOverride setting is persisted. And if you have page cache enabled, when you apply a different language on the fly, you need to clear the cache by setting Frame.CacheSize = 0; first then set it back.




回答2:


Some addition to Alan Yao's answer. There's one missing piece: After you set the Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride and before re-navigating to the current page, you must call these two functions:

Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();

This way you won't need the Task.Delay() workaround mentioned by Michael Woolsey.

One more important last step: when creating a Store package, you should make sure to set the "Generate app bundle" setting value to "Never". Reason from this article:

Because otherwise, it will create a bundle. It means that he will cut your application into different parts to optimize the download. Only the parts that are relevant for the devices will be downloaded. For example, if there are the assets in different resolution, it will only download the ones that are suitable for the device. Same thing for languages, it will only download the resources file relevant to the language of the device. So if you try to change language, it will fall still fall back on the same base language, because others are not installed.




回答3:


@ThisWillDoIt and @Herdo

I added a delay so that the "First" time it would work in my situation:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = languageCode;

await Task.Delay(100);

Frame.Navigate(this.GetType());

Hope it helps work for you.




回答4:


There is a page MSDN that describes the new feather about Language from Windows 8.1.

After modify the

ApplicationLanguages.PrimaryLanguageOverride = "en-US";

I watched the property resourceContext.Languages[0] in order to launch the PropertyChanged event of my LanguageManager, which was a StaticResources declared in App.xaml with x:Key = Loc.

    private void ButtonEn_OnClick(object sender, RoutedEventArgs e)
    {
        ApplicationLanguages.PrimaryLanguageOverride = "en-US";
        UpdateLang("en-US");
    }

    private async void UpdateLang(string newLang)
    {
        var resourceContext = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView();

        while (true)
        {
            if (resourceContext.Languages[0] == newLang)
            {
                var loc = Application.Current.Resources["Loc"] as LanguagesManager;
                loc.ChangeLang();
                break;
            }
            await Task.Delay(100);
        }
    }

The while (true) was just for test, in fact it's better to escape by a "backup", because

Those requirements may vary depending on the UI framework used by the app, and it may be necessary to restart the app.




回答5:


Unfortunately, none of the above answers helped if NavigationCacheMode set to "Required" for the page. Here's the code that solved my problem.

ApplicationLanguages.PrimaryLanguageOverride = language;
await Task.Delay(300);
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Content = null;
rootFrame = null;            
rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), null);
Window.Current.Content = rootFrame;


来源:https://stackoverflow.com/questions/32715690/c-sharp-change-app-language-programmatically-uwp-realtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!