Milind
Milind

Reputation: 1955

Windows Phone 8.1 app Multi language

I am creating windows phone app 8.1 in SilverLight with Visual Studio 2015. I am creating multi language app in English and Arabic. For that I have created Strings folder in the project with two folder of en-US and ar-KW with Resources.resw file in each folder.

I am x:Uid setting properties. For example Key:- Actual.Text Value:- Actual

<TextBlock x:Uid="Actual" TextWrapping="Wrap" MaxWidth="65" HorizontalAlignment="Center" />

Above is working very good. I have combobox with Item EN and AR. I am triggering SelectionChanged event to change the Language But the issue is when I stop the app and run it again than only it change the text and layout.

How can I do it at runtime without restart.

private void LanguageComboBoxName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedItem = (e.AddedItems[0] as ComboBoxItem).Content as string;
            var RootFrame = Window.Current.Content as SlideApplicationFrame;

            if (selectedItem == "EN")
                ApplicationLanguages.PrimaryLanguageOverride = "en-US";
            else if (selectedItem == "AR")
                ApplicationLanguages.PrimaryLanguageOverride = "ar-KW";

            RootFrame.Navigate(this.GetType());
    }

Upvotes: 4

Views: 597

Answers (2)

Michal Kania
Michal Kania

Reputation: 606

As far as i remember it is not recommended by Microsoft to change language in a runtime (by overriding it) but you can do it by reloading the page. The easiest way is to put your combobox with the code on another page and there you will override language, so when you navigate back (and page will be reloaded) you will have what you want. Keep in mind that overriding language will not localize controls in the runtime (but it shouldn't be a great issue for you). You must restart the app to localize controls.

When you override language with this code:

ApplicationLanguages.PrimaryLanguageOverride = "xx-XX";

then it is saved and you don't have to override it again on the start. App will load with the overrided language.

Additionally when you localize the app it is good to override culture info because if you have some dates - they are shown properly for the culture (e.g. 12/01/2015 or 01-12-2015)

string lang = "en-US";    //default
var culture = new CultureInfo(lang);
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = lang;
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

There's one more important thing.

In your app manifest or what you have there, you need to set Generate app bundle to "Never". The reason is that when you uploaded packaged version to the store then your runtime localization won't work. You may be able to localize your app in the runtime with emulator or debug versions or even sometimes with deployed release versions. But you need to do that so when your app is in the store then there is no problem with localization and all of the string for languages can be accessed (because for packaged version some of them might not be if you haven't installed the language on your phone).

Upvotes: 5

Depechie
Depechie

Reputation: 6142

If you want the app to depend on a language through user selection, you'll need to store that selection and read it out on app start. That way you can change the ApplicationLanguage when the app starts.

So in App.Xaml.cs look for the method InitializePhoneApplication() and add following lines ( or you could also try your approach here with ApplicationLanguages.PrimaryLanguageOverride )

string savedLanguage = string.Empty;
var hasSavedLanguage = AppSettings.TryGetSetting(Constants.LanguageSettingKey, out savedLanguage);
if (hasSavedLanguage)
{
  Thread.CurrentThread.CurrentUICulture = new CultureInfo(savedLanguage);
  Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
}

Of course you'll also need to save the language when the user selected one from the combobox

Upvotes: 0

Related Questions