I am trying to use .resx files for localization in xamarin.mac
Inside AppDelegate I changed current culture of the thread :
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ru");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo ("ru");
also I have two resource files inside my app:
But strings are always shown from the default resource file... Any solution?
I also use native language change, here is whole AppDelegate constructor :
public AppDelegate ()
{
string [] lang = { "ru", "en" };
NSUserDefaults.StandardUserDefaults.SetValueForKey (NSArray.FromObjects (lang), (Foundation.NSString)"AppleLanguages");
NSUserDefaults.StandardUserDefaults.Synchronize ();
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ru");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo ("ru");
Resources.Culture = GetCurrentCultureInfo ();
}
BTW , xamarin studio 6.1.1 (build 15) doesn't allow me to add resource.ru.resx if I have resource.ru in my project , kinda bug !
I have created resource-ru.resx file and than renamed it .
You need to set the app culture after retrieving it from the OS, something like this (warning, this is from Xamarin iOS, but for the documentation it works the same way, if not advice me and I will delete the post):
//Add this somewhere
public System.Globalization.CultureInfo GetCurrentCultureInfo ()
{
var netLanguage = "en";
if (NSLocale.PreferredLanguages.Length > 0) {
var pref = NSLocale.PreferredLanguages [0];
netLanguage = pref.Replace ("_", "-"); // turns en_US into en-US
}
try
{
return new System.Globalization.CultureInfo(netLanguage);
}catch{
try{
return new System.Globalization.CultureInfo(netLanguage.Substring(0, netLanguage.IndexOf("-")));
}
catch{
return new System.Globalization.CultureInfo("en");
}
}
}
//And finally add this at the start of the app
AppResources.Culture = GetCurrentCultureInfo();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With