Louis Martin
Louis Martin

Reputation: 71

changing cultureinfo on android using xamarin and c#

Im calling a custom method to dynamically switch the current cultureinfo to french "fr"

Like this but after calling that method my android app still use the default culture which is 'en' but in debug mode the culture seems to be ok. My folder are ok. I have both and the string values are configured. folder: resource/values/strings.xml, resource/values-fr/strings.xml.

Do I need to reload my contentview or something? what do I miss here?

    private void SetLocal(string lang) 
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
    }

Upvotes: 3

Views: 6053

Answers (3)

Edgar David
Edgar David

Reputation: 11

All this in the MainActivity

using System.Threading;
using System.Globalization;

void SetLocale() {

    CultureInfo ci = new CultureInfo("es-US");

    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;

    Console.WriteLine("CurrentCulture set: " + ci.Name);
}

Upvotes: 1

esiprogrammer
esiprogrammer

Reputation: 1438

I know it's a bit late to answer this question but I found the solution!! Try this it works for me:

 string cultureName = "fr-FR";
        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);  

Upvotes: 4

Guzmangm
Guzmangm

Reputation: 11

I can't test it right now, but try this:

        Resources.Configuration.Locale = new Locale(lang);
        Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);

Upvotes: 1

Related Questions