Change application Language in Android

Hello all. I have a request for help in one matter. I have this code for changing the language in the application:

      public void ChangeLanguage()
    {
        string Lang = CultureInfo.CurrentCulture.ToString();
        string content = string.Empty;

        var prefs = Application.Context.GetSharedPreferences("KalkulatorCNC", FileCreationMode.Private);
        var somePref = prefs.GetString("Language", null);

        switch (somePref)
        {
            case "de-DE":
                Lang = "de-DE";
                Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
                break;
            case "en-US":
                Lang = "en-US";
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                break;
            case "fr-FR":
                Lang = "fr-FR";
                Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
                break;
            case "pl-PL":
                Lang = "pl-PL";
                Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL");
                break;
        }
        switch (Lang)
        {
            case "de-DE":
                content = "de";
                break;
            case "en-US":
                content = "en";
                break;
            case "fr-FR":
                content = "fr";
                break;
            case "pl-PL":
                content = "pl";
                break;
            default:
                content = "en";
                break;
        }

        var language = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext).GetString("Language", content);
        var locale = new Locale(language);
        Locale.Default = locale;
        Configuration config = new Configuration();
        config.Locale = locale;
        this.BaseContext.Resources.UpdateConfiguration(config, this.BaseContext.Resources.DisplayMetrics);
    }

Visual Studio 2017 highlights this code as deprecated:

        this.BaseContext.Resources.UpdateConfiguration(config, this.BaseContext.Resources.DisplayMetrics);

Does anyone know of you what the correct form of this line should look like?
Im using Xamarin with c#.
Thank you in advance for your help.

Tagged:

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @MichalSchmude have you found a solution?

  • MichalSchmudeMichalSchmude USMember ✭✭
    edited October 27

    Yes, i found. This is my code to change language in app:

             public void ChangeLanguage()
               {
              var langauage = CultureInfo.CurrentCulture.ToString();
              string content = string.Empty;
              try
               {
                switch (langauage)
                {
                    case "en-US":
                        content = "en";
                        break;
                    case "pl-PL":
                        content = "pl";
                        break;
    
                }
    
                var language = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext).GetString("Language", content);
                var locale = new Locale(language);
                Locale.Default = locale;
                Configuration config = new Configuration();
    
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    config.SetLocale(locale);
                }
                else
                {
                    config.Locale = locale;
                }
            }
    
            catch (Exception ex)
            {
                ShowMassage("Unable to change language " + " ERROR CODE:" + ex.Message);
            }
        }
    
Sign In or Register to comment.