Forcing a Localization to Load in Windows Phone 8 Apps

Problem:

Can we load a resource file without changing the language from device settings?

 

Solution:


The answer is, Yes!

You can actually force an application to load a French resource by manipulating the Culture Settings in the Thread class. There's a trick about that.

Based on my experiences, I always use this small but efficient method to force loading various languages:

public void SetLang(string lang)

{

 App.RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);

 Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);

 Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);

 NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml?random={0}", Guid.NewGuid()), UriKind.Relative));

 NavigationService.RemoveBackEntry();

}

Let's talk a bit! This method changes current culture settings and then redirects to the current page with a unique querystring. If we hadn't written the querystring then the culture would be the default culture, that is English in our app. By using this code, we ensure that each culture has a unique identifier when called and that prevents complexity.

 

To force the English culture, I would write code such as:

SetLang("en-US");

And to force the French language:

SetLang("fr-FR");



Similar Articles