changing cultureinfo on android using xamarin and

2019-04-15 16:56发布

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);
    }

3条回答
SAY GOODBYE
2楼-- · 2019-04-15 17:26

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);
}
查看更多
够拽才男人
3楼-- · 2019-04-15 17:42

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

        Resources.Configuration.Locale = new Locale(lang);
        Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-15 17:49

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);  
查看更多
登录 后发表回答