Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.mac Resources localization doesn't change by current culture

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:

enter image description here

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 .

like image 238
Nininea Avatar asked Oct 29 '22 18:10

Nininea


1 Answers

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();
like image 173
Gusman Avatar answered Nov 15 '22 05:11

Gusman