Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force Localization Culture to en-US for whole applicaiton in Xamarin

How to force Xamain - Android / iOS to work in US English culture regardless of user setting. The issue I am facing in my application is, The app only support US/UK English, But if the user changes the langue preference to Spanish , German etc. The number date etc format will change.

For Example, 2.35 will represent in Spanish, German as 2,35.

So if the user try to use the application with similar language, the app will miss behave or crash. Crash will occur in situation like when I try for Convert.ToDouble("2,35"); or similar.

So my doubt is,

Is it possible in Xamarin to forcefully set the culture as en-US. May be in one place, otherwise I need to change it all the places I performing Conversion.

Please help.

like image 336
Stephan Ronald Avatar asked Oct 07 '15 12:10

Stephan Ronald


4 Answers

I am working with a Xamarin Forms App.

Setting the Culture in the App Class did the trick for me.

using System.Globalization;
using System.Threading;


private void SetCultureToUSEnglish()
{
    CultureInfo englishUSCulture = new CultureInfo("en-US");
    CultureInfo.DefaultThreadCurrentCulture = englishUSCulture;
}
like image 126
7vikram7 Avatar answered Nov 13 '22 17:11

7vikram7


You can set the default culture with following property:

CultureInfo.DefaultThreadCurrentCulture

But this won't work in Android. So for Android you need to set the culture every time an activity gets resumed. You can add a base activity like:

internal class MyBaseActivity : Activity
{
    protected override void OnResume ()
    {
        base.OnResume ();

        // Here you would read it from where ever.
        var userSelectedCulture = new CultureInfo ("fr-FR");

        Thread.CurrentThread.CurrentCulture = userSelectedCulture;
    }
}

Found in the xamarin forum: https://forums.xamarin.com/discussion/9764/how-to-set-a-global-cultureinfo-for-an-app

like image 27
Joehl Avatar answered Nov 13 '22 18:11

Joehl


I try Joehl approach but it didn't work for me. I used this approach

        string cultureName = "es-US";
        var locale = new Java.Util.Locale(cultureName);
        Java.Util.Locale.Default = locale;

        var config = new Android.Content.Res.Configuration { Locale = locale };
        BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics);

Also created a base activity

Find answer here:

changing cultureinfo on android using xamarin and c#

like image 5
foluis Avatar answered Nov 13 '22 18:11

foluis


You must set current culture in your app. Localisation works really well. This is how I did it:

public void SetLocale(CultureInfo ci)
    {
        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;

    }

And here you get the culture from device:

public CultureInfo GetCurrentCultureInfo()
    {
        var netLanguage = "en";
        if (NSLocale.PreferredLanguages.Length > 0)
        {
            var pref = NSLocale.PreferredLanguages[0];

            netLanguage = iOSToDotnetLanguage(pref);
        }

        // this gets called a lot - try/catch can be expensive so consider caching or something
        CultureInfo ci = null;
        try
        {
            ci = new CultureInfo(netLanguage);
        }
        catch (CultureNotFoundException e1)
        {
        }
     }
private string iOSToDotnetLanguage(string iOSLanguage)
    {
     // Testing special cases..
    }

If you use PCL project, use abstraction. Use interface in PCL and its implementation in native project.

You can see more here: https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/localization

like image 3
Dan Avatar answered Nov 13 '22 18:11

Dan