18

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.

Stephan Ronald
  • 1,395
  • 2
  • 14
  • 39

7 Answers7

17

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;
}
7vikram7
  • 2,764
  • 1
  • 25
  • 43
5

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

Joehl
  • 3,671
  • 3
  • 25
  • 53
5

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#

foluis
  • 988
  • 2
  • 10
  • 23
3

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

Dan
  • 448
  • 10
  • 20
  • 1
    Is it enough to set this once in App.CS or should i set local in every viewModel or code behind? – Ali123 Nov 06 '18 at 07:56
  • 1
    @Ali123 sorry I see comment accidentally now, quote my name next time, so I get a notification. It's enough to do it only once when you're starting your app, initializing it that is. Hope it helps! – Dan Jan 08 '19 at 15:32
1

If you are using the Localize class with the translate extension and lang resources are all set, you can call the setLocale method at runtime available in each platform projects from PCL using the interface ILocalize like this:

CultureInfo ci=new CultureInfo("en-US");
Xamarin.Forms.DependencyService.Get<ILocalize>().SetLocale(ci);                      
AppResources.Culture = ci;
pushkin
  • 9,575
  • 15
  • 51
  • 95
A. Almazidi
  • 25
  • 1
  • 5
0

My solution was to use a combination of the other answers!

Basically, I didn't find the need to create a new Activity and extend it as suggested. Instead, I added the following override into MainActivity.cs:

  protected override void OnResume()
        {
            base.OnResume();

            try
            {
                var userSelectedCulture = new CultureInfo("en-GB");

                Thread.CurrentThread.CurrentCulture = userSelectedCulture;
            }
            catch (Exception exception)
            {

                Console.WriteLine(exception.Message);
            }
        }

and then, inside OnCreate, I added the following:

  try
            {
                string cultureName = "en-GB";
                var locale = new Java.Util.Locale(cultureName);
                Java.Util.Locale.Default = locale;

                var configg = new Android.Content.Res.Configuration { Locale = locale };
                BaseContext.Resources.UpdateConfiguration(configg, BaseContext.Resources.DisplayMetrics);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

            }

I tested it inside an emulator by changing the language to French and showing a simple DateTime to find out whether today displays as samedi or Saturday and it displayed Saturday!

Amir Hajiha
  • 836
  • 8
  • 20
0

I'm working on a Xamarin.Forms App and I have the same requirement, I have ended up using the MultilingualPlugin for Xamarin.Forms

Mohamed Saleh
  • 2,881
  • 1
  • 23
  • 35