Changing language in UWP doesn't change system features language - only on app restart

放肆的年华 提交于 2019-12-07 03:10:34

问题


I have a UWP application.

And i have a need to change locale on the fly, so i have this for language changing:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = language.FourDigitCode;
ResourceContext.GetForViewIndependentUse().Reset();
ResourceContext.GetForCurrentView();

But there is a problem that system features language doesn't switch ( only after application relaunch ) how can i fix it?

Here is an example:

Now i run this code:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "lv-LV";
ResourceContext.GetForViewIndependentUse().Reset();
ResourceContext.GetForCurrentView();

The UI gets localized, but system features still remain unlocalized:

But when i restart the app, all is OK:

Any ideas how can i fix it?


回答1:


I'm afraid there is no fix for this and what you've seen is by design. Ref Remarks of PrimaryLanguageOverride property:

When you set the PrimaryLanguageOverride, this is immediately reflected in the Languages property. However, this change may not take effect immediately on resources loaded in the app UI. To make sure the app responds to such changes, you can listen to the QualifierValues property on a default resource context and take whatever actions may be needed to reload resources. Those requirements may vary depending on the UI framework used by the app, and it may be necessary to restart the app.

For your scenario, a restart is needed. I'd suggest that you can add a tip to tell users to restart the app and also a button to close the app like what used in News App.
And to close the app, we can call Application.Exit method like the following.

Application.Current.Exit();



回答2:


Maybe page reloading can fix it? Try to re-navigate to the same page. Found the example below here.

//like this
private bool Reload(object param = null)
{
    var type = Frame.CurrentSourcePageType; 
    Frame.Navigate(type, param);
    Frame.BackStack.Remove(Frame.BackStack.Last());
}

// or like this
private bool Reload(object param = null)
{
    var frame = Window.Current.Content as Frame; 
    frame.Navigate(frame.CurrentSourcePageType, param);
    frame.BackStack.Remove(frame .BackStack.Last());
}


来源:https://stackoverflow.com/questions/42859407/changing-language-in-uwp-doesnt-change-system-features-language-only-on-app-r

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