Howie
Howie

Reputation: 2778

Changing localization on-the-fly

I'm developing a multilingual app for WP8.

I've read this blog entry, but I find the "Enabling the user to change the language on the fly" step too cumbersome.

Is it possible to somehow refresh the Language and Text attributes of UI elements for the whole app, or at least page?

Upvotes: 1

Views: 963

Answers (1)

anderZubi
anderZubi

Reputation: 6424

The problem with the method in the article is that the elements of the current page are not automatically updated because they are already rendered, and you need to update the strings in code.

A possible solution is to reload the page when changing the language. The SetUILanguage method would look like:

private void SetUILanguage(string locale)
{
    // Set this thread's current culture to the culture associated with the selected locale.
    CultureInfo newCulture = new CultureInfo(locale);
    Thread.CurrentThread.CurrentCulture = newCulture;
    Thread.CurrentThread.CurrentUICulture = newCulture;

    // Set the FlowDirection of the RootFrame to match the new culture.
    FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection),
        AppResources.ResourceFlowDirection);
    App.RootFrame.FlowDirection = flow;

    // Set the Language of the RootFrame to match the new culture.
    App.RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);

    // Refresh the page in order to localize already rendered elements
    NavigationService.Navigate(new Uri(NavigationService.Source + "?Refresh=true", UriKind.Relative));
}

That way the elements will be re-rendered, and will show the localized strings, without the need of changing them one by one.

Upvotes: 3

Related Questions