Localization for german language

Hi, I am trying to make a button that will change my language to german. This is my SetLocale function and how I call it:
private void Mbtn_Click(object sender, System.EventArgs e)
{
SetLocale("de-DE");
Recreate();
}
public void SetLocale(string language = "")
{
Java.Util.Locale locale = string.IsNullOrEmpty(language) ? new Java.Util.Locale("en-US") : new Java.Util.Locale(language);
Java.Util.Locale.Default = locale;
var config = new global::Android.Content.Res.Configuration();
config.Locale = locale;
var context = global::Android.App.Application.Context;
context.Resources.UpdateConfiguration(config, context.Resources.DisplayMetrics);
}

My values folder is called > values-de , and I added this > android:configChanges="locale" to my AndroidManifest.
But it doesnt work, anyone has idea why?

Answers

  • Luis_dmLuis_dm USMember ✭✭✭
    edited October 2017
     Locale leng = new Locale("de-DE");
    
     if (version >= 24)
     { Locale.SetDefault(Locale.Category.Format, leng); }
    
     DisplayMetrics dm = Resources.DisplayMetrics;
    Configuration config = new Configuration();
    config.Locale = leng;
     Resources.UpdateConfiguration(config, dm);
    
    recargarActivity();
    

    And you must update activity.

            private void recargarActivity() {
              finish();
               var intent = new Intent(this.Activity, typeof(MiActivity));
                startActivity(intent);  
            }
    

    I recommend saving the value of the language in ISharedPreferences

  • misko101misko101 HRMember ✭✭
    edited October 2017

    I am getting this error on that code: Java.Lang.ClassNotFoundException: Didn't find class "java.util.Locale$Category" on path: DexPathList , do you maybe know why?

    EDIT: I figured why, my version is lower than 24, and now its not working, should i be doing something else for lower versions in that if?

Sign In or Register to comment.