Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change App language at RunTime on-the-fly

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?

like image 241
Muhammad Umar Avatar asked Dec 03 '13 07:12

Muhammad Umar


People also ask

How do I change the language of runtime app?

On Android, there is no official support, documentation or API to change an entire app's language at runtime. When a user opens an app, the resource framework automatically selects the resources that best match the device language settings.

How do I change the button click language in Swift?

Click on iOS->App/Single View App->Next. Name your project and select the language as Swift. Select a desired location and click on Create. To start with Localization, click on your Project Name -> go to Info Tab -> Under Localizations, click on the '+' button.


2 Answers

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));
like image 98
CSharper Avatar answered Oct 19 '22 01:10

CSharper


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

like image 28
Eric MSFT Avatar answered Oct 19 '22 01:10

Eric MSFT