9

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?

3 Answers 3

3

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
  • 1
    i had some weird problem when i was clicking on button to change language, long story short: 1. change PrimaryLanguageOverride on button click and then reload page 2. in page counstructor reset reources; the problem was that i had to click on button twice to actually change text on current page Oct 26, 2016 at 16:04
  • I had the same problem, thank to you i fixed it! it's really weird tho
    – DennisVA
    Mar 29, 2017 at 15:25
1

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

1
  • 1
    Do you know how to do this on Windows Phone 8.1? It works fine on Win8.1 but on WinPhone8.1 the language doesn't update without restarting the app. Oct 5, 2014 at 21:09
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.