Change App language at RunTime on-the-fly

前提是你 提交于 2019-12-29 07:32:10

问题


I'm currently developing a metro app in which the user can change current language at runtime and all the custom controls that are loaded must update their text regarding to the new language. Problem is that when I change the language using the following code, the app language changes but it will only update text when I restart my app because the pages and controls that are already rendered are cached.

LocalizationManager.UICulture = new System.Globalization.CultureInfo((string)((ComboBoxItem)e.AddedItems[0]).Tag);
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = ((ComboBoxItem)e.AddedItems[0]).Tag as String;

What should I do to force updating text of all custom controls at runtime without restarting my app?


回答1:


Use this:

var NewLanguage = (string)((ComboBoxItem)e.AddedItems[0]).Tag;

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = NewLanguage;

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

Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.Reset();

and then reload your Page, using Navigate method:

if (Frame != null)
    Frame.Navigate(typeof(MyPage));



回答2:


In order to respond right away, you would need to reset the context of the resource manager.

For Windows 8.1: var resourceContext = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView();

resourceContext.Reset();

You will still need to force your page to redraw itself and thus re-request the resources to get the changes to take place. For Windows 8, you can see https://timheuer.com/blog/archive/2013/03/26/howto-refresh-languages-winrt-xaml-windows-store.aspx




回答3:


You can change the app's language at runtime with the help of this source code. I took help from this and manipulated my app's language settings page as follows:
In languageSettings.xaml.cs:

public partial class LanguageSettings : PhoneApplicationPage
    {
        public LanguageSettings()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (ChangeLanguageCombo.Items.Count == 0)
            {   ChangeLanguageCombo.Items.Add(LocalizationManager.SupportedLanguages.En);
                ChangeLanguageCombo.Items.Add(LocalizationManager.SupportedLanguages.Bn);
            }
            SelectChoice();
        }



        private void ButtonSaveLang_OnClick(object sender, RoutedEventArgs e)
        {
            //Store the Messagebox result in result variable

            MessageBoxResult result = MessageBox.Show("App language will be changed. Do you want to continue?", "Apply Changes", MessageBoxButton.OKCancel);

            //check if user clicked on ok
            if (result == MessageBoxResult.OK)
            {

                var languageComboBox = ChangeLanguageCombo.SelectedItem;

                LocalizationManager.ChangeAppLanguage(languageComboBox.ToString());
                //Application.Current.Terminate(); I am commenting out because I don't neede to restart my app anymore.
            }
            else
            {
                SelectChoice();
            }
        }

        private void SelectChoice()
        {
           //Select the saved language

            string lang = LocalizationManager.GetCurrentAppLang();
            if(lang == "bn-BD")
                ChangeLanguageCombo.SelectedItem = ChangeLanguageCombo.Items[1];
            else
            {
                ChangeLanguageCombo.SelectedItem = ChangeLanguageCombo.Items[0];
            }
        }
    }

***Note: Before understanding what I did on LanguageSettings page's code behind, you must implement the codes from the link as stated earlier. And also it may be noted that I am working on windows phone 8



来源:https://stackoverflow.com/questions/20345029/change-app-language-at-runtime-on-the-fly

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