Change App language at RunTime on-the-fly

后端 未结 3 1532
鱼传尺愫
鱼传尺愫 2020-12-19 02:48

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 t

相关标签:
3条回答
  • 2020-12-19 03:27

    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

    0 讨论(0)
  • 2020-12-19 03:28

    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

    0 讨论(0)
  • 2020-12-19 03:32

    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));
    
    0 讨论(0)
提交回复
热议问题