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 01 Answer
Reset to default 1First 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.
- 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();
}
- 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");
}
}