shinchillahh shinchillahh - 5 months ago 218
iOS Question

Xamarin.Forms : How to use localization independent of device language

I am developing a Xamarin.Forms app (portable class library project) with Visual Studio 2013 CE. First I'm focusing the iOS version.

Now I'm thinking about how to make the app multilingual.
I just read the offical Xamarin documentation about it but i realized that this solution only takes the system language of the target device.

In the portable class library I have a Resources folder with three languages: German (default), English and French.

Resource.resx
Resource.en-US.resx
Resource.fr-FR.resx
Resource.Designer.cs


Now i just created a static settings class which looks like this:

public static class Settings
{
public static Dictionary<String, CultureInfo> Languages = new Dictionary<String, CultureInfo> { { "German", new CultureInfo("de-DE") }, { "English", new CultureInfo("en-US") }, { "French", new CultureInfo("fr-FR") } };

public static CultureInfo CurrentCulture = Languages["German"];
public static CultureInfo CurrentUiCulture = Languages["German"];

public static void ChangeCurrentCultureInfo(CultureInfo cultureInfo)
{
Resource.Culture = cultureInfo;
}
}


Now my question is if it's possible to change the culture in the application while runtime with a button click.
Maybe something like

Settings.CurrentCulture = Settings.Languages["English"];
Settings.ChangeCurrentCultureInfo(Settings.CurrentCulture);


Does anyone can tell me how this can be done?

Regards

Answer

Finally i got an answer of the Xamarin support. The solution i was thinking about works. In the PCL i just have the static Settings class which stores the different CultureInfo objects. In addition I defined a method for changing the current culture and the language of the resources file.

public static void ChangeCurrentCultureInfo(CultureInfo cultureInfo)
    {
        CurrentCulture = cultureInfo;
        Resource.Culture = CurrentCulture;
    }

It was important to set the default language in the App() constructor before I initialize the main page. Now i got a listview with all supported languages with one tapped event which calls the ChangeCurrentCultureInfo method.

Nevertheless i want to thank Andy Hopper for providing his solution!

Regards