1

So i've implementend an datetime picker using Maui Razor Hybrid. So my problem is like this: MAUI Blazor not respecting device language on iOS

But with Android.. i've alredy test with this, without a success, into MainActivity.cs:

protected override void OnCreate(Bundle? savedInstanceState)
   {
       base.OnCreate(savedInstanceState);

       this.SetLocale();

   }
   void SetLocale()
   {

       CultureInfo ci = new CultureInfo("it-IT");

       Thread.CurrentThread.CurrentCulture = ci;
       Thread.CurrentThread.CurrentUICulture = ci;

       Console.WriteLine("CurrentCulture set: " + ci.Name);
   }

So any idea to resolve?

0

1 Answer 1

1

First of all, you don't have to do anything when user changes the android device's system language.

Android system will recreate the running app's current activity when the system language changed. You can add a break point at the MainActivity's OnCreate method to check it.

But in my test, the app will be freezed when user go back to the app by the recent task after the system language changed in .net 8.0. I also tried to recreate the MainActivity programatically. But it work well in .net 9.0.

You can report this as a new issue on the gihub repo and upgrade your project to .net 9.0. And for .net 8, I killed the app when the system languaged changed. And then the app will restart when user tap it in the recent task list.

  1. Detect system language changed in the /Platforms/Android/MainApplication.cs:
    public class MainApplication : MauiApplication
    {
        public MainApplication(IntPtr handle, JniHandleOwnership ownership)
            : base(handle, ownership)
        {
        }
        public override void OnConfigurationChanged(Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);
            Runtime.GetRuntime().Exit(0);
            //kill the app
        }
       
        protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    }
  1. Detect system language changed with the Broadcast Receiver:
    [BroadcastReceiver(Exported = true)]
    [IntentFilter(new[] {Intent.ActionLocaleChanged})]
    public class Class1 : BroadcastReceiver
    {
        public override void OnReceive(Context? context, Intent? intent)
        {
           // Runtime.GetRuntime().Exit(0);
        }
    }

===============================

You can just put the following code into the MainActivity to change the application locale for Android 12 and lower versions.

        protected override void AttachBaseContext(Context? @base)
        {
            var locale = LocaleListCompat.ForLanguageTags("it-IT");
            AppCompatDelegate.ApplicationLocales = locale;
            base.AttachBaseContext(@base);
        }

For Android 13 and higher versions, please add following code into \Platforms\Android\MainApplication.cs

        public override void OnCreate()
        {
            base.OnCreate();
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Tiramisu)
            {
                var manager = this.GetSystemService(LocaleService) as LocaleManager;
                manager.ApplicationLocales = LocaleList.ForLanguageTags("it-IT");
            }
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Liyun Zhang maybe you misunderstanding the answer, i need to change datetime picker language, like the language system, so now is set to italian but the datetime picker is show in english. I need to set datetime picker like the language system, so italian.
Sorry for my mistake. Please check the updated part in my answer. @Mr.Developer
In addition, you can refer to this case: Set Locale programmatically. @Mr.Developer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.