Search code examples
wpfsilverlightwindows-phone-8localizationisolatedstorage

How to save the language choice of user and change the overall app language in windows phone 8?


I am trying to localize my windows phone 8.0 app (SilverLight). I want to change the default Appresources.resx file upon user choice. When the user changes the language from the settings page I want to save it by in IsolatedStorageSettings and then indicate the saved language's Appresources file in InitializeLanguage() method which was called in my constructor of app.xaml.cs class.

I learned the theoretical process but I'm unable to go further on how to approach it.

Following are the code snippets for a better understanding of my problem.

private void InitializeLanguage()
        {
            try
            {
                RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
                FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
                RootFrame.FlowDirection = flow;
            }
            catch
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
                throw;
            }
        }

And this the settings page code behind where I initially changed the language of a text box for test purposes which changes the language of the TextBox in runtime.

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

            ChangeLanguageCombo.Items.Add(new LanguageComboBox
            {
                Name = "English",
                Code = "en-US"
            });

            ChangeLanguageCombo.Items.Add(new LanguageComboBox
            {
                Name = "Bangla",
                Code = "bn"
            }); 
        }

    public static IsolatedStorageSettings ChangedLanguage = IsolatedStorageSettings.ApplicationSettings;
    
    private void ChangeLanguageCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var languageComboBox = ChangeLanguageCombo.SelectedItem as LanguageComboBox;
        ApplyChange(new CultureInfo(languageComboBox.Code.ToString()));
        //now I want to save the user choice to the `IsolatedStorageSettings ChangedLanguage` and restart the app to take place the changes.
        MessageBox.Show("Restart");
        //after restart I want to indicate the Appresources file to the new selected one,(in InitializeLang() method) RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage); in this line
        }
            
    }

    private void ApplyChange(CultureInfo culInfo)
    {
        Thread.CurrentThread.CurrentCulture = culInfo;
        Thread.CurrentThread.CurrentUICulture = culInfo;
        textBlockHello.Text = AppResources.Salutation;
    }

I am sorry if the question is too clumsy to understand my purpose, I am new in this field and any kind of help or edit suggestion will do.


Solution

  • For retrieving the value of LocalStorageSettings from App.xaml.cs class:

    string value= IsolatedStorageSettings.ApplicationSettings["userData"] as string;
    

    In App.xaml.cs I added following code under the try block of method InitializeLanguage()

    private void InitializeLanguage()
    {
        try
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("selectedLang"))
            {
                 var changedLang = IsolatedStorageSettings.ApplicationSettings["selectedLang"] as string;
                 if (changedLang != null) ApplyChange(new CultureInfo(changedLang));
            }            
         }
         //rest of the part in this method remained same 
    }
    private void ApplyChange(CultureInfo culInfo)
    {
         Thread.CurrentThread.CurrentCulture = culInfo;
         Thread.CurrentThread.CurrentUICulture = culInfo;
    }
    

    And in my settings page when user selects a preferred language:

            public static IsolatedStorageSettings ChangedLanguage = IsolatedStorageSettings.ApplicationSettings;
            private void ChangeLanguageCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var languageComboBox = ChangeLanguageCombo.SelectedItem as LanguageComboBox;
    
                if (languageComboBox != null)
                {
                    if (!ChangedLanguage.Contains("selectedLang"))
                    {
                        ChangedLanguage.Add("selectedLang", languageComboBox.Code.ToString());
                    }
                    else
                    {
                        ChangedLanguage["selectedLang"] = languageComboBox.Code.ToString();
                    }
                    ChangedLanguage.Save();
                    MessageBox.Show("Restart");
                }
    
            }
    

    After restarting the app the default Appresources file would be the new languages's Appresources file as it was saved on the IsolatedStorageSettings and on app launching the App.xaml.cs page calls the InitializeLanguage() method.
    So this is the way how I was able to change the default Appresources file when user changes the language of my app from the settings page.