问题
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);
}
回答1:
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);
回答2:
I can't test it right now, but try this:
Resources.Configuration.Locale = new Locale(lang);
Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
回答3:
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);
}
来源:https://stackoverflow.com/questions/19215964/changing-cultureinfo-on-android-using-xamarin-and-c-sharp