MAUI Blazor not respecting device language on Android - Stack Overflow

So i've implementend an datetime picker using Maui Razor Hybrid.So my problem is like this: MAUI

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?

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?

Share Improve this question asked Nov 18, 2024 at 16:45 Mr. DeveloperMr. Developer 3,4178 gold badges49 silver badges119 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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 8.0. I also tried to recreate the MainActivity programatically. But it work well in 9.0.

You can report this as a new issue on the gihub repo and upgrade your project to 9.0. And for 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");
            }
        }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745606163a4635684.html

相关推荐

  • MAUI Blazor not respecting device language on Android - Stack Overflow

    So i've implementend an datetime picker using Maui Razor Hybrid.So my problem is like this: MAUI

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信