changing cultureinfo on android using xamarin and c#
Asked Answered
K

3

3

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);
    }
Kukri answered 7/10, 2013 at 1:41 Comment(1)
additionnal info: in debug mode the cultureinfo is FR but the text of the submit button is still in english. Do I Need to refresh the UI or reload my layout? im not sure...Kukri
B
4

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);  
Boche answered 5/2, 2015 at 16:25 Comment(0)
C
1

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

        Resources.Configuration.Locale = new Locale(lang);
        Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
Cryohydrate answered 8/10, 2013 at 15:19 Comment(3)
not working... here are my attempts... my goal is to find a way to change the Locale and then as per your answer use the UpdateConfiguration...Kukri
Im still waiting. Please tell me how I can using C#/Xamarin change the LOCALE. Cause your first line doesnt compile or work. If its not possible for you to give me an answer just tell me. In the meantime ill take a chance with google.Kukri
I'm sorry, I used to develop that app with a virtual machine but I don't have it anymore. I'm using TFS to look up... We have this code in ActivityBase ` private void SetCurrentCulture() { Resources.Configuration.Locale = new Locale(Thread.CurrentThread.CurrentCulture.Name.ToLower()); Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics); }`Cryohydrate
P
1

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);
}
Peppie answered 28/11, 2017 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.