Xamarin Forms detect device language has changed

Gordon S 501 Reputation points
2021-03-22T13:34:58.243+00:00

If the user changes the selected language in the Android settings, how can I detect that change in my App and update the content to use / show the correct content from the Resources file?

Other apps, such as Settings itself, GMail, etc update as soon as the language is changed in Settings - and the app is brought back to the foreground. My app has to be closed and reopened for it to detect the new language setting.

I currently do this in the constructor of App in the shared project:

        Thread.CurrentThread.CurrentUICulture = CultureInfo.InstalledUICulture;

I tried adding code in "OnConfigurationChanged" of the Android app (in MainActivity.cs), I also added both "ConfigChanges.Locale" and "ConfigChanges.LayoutDirection" to "[Activity]". The "OnConfigurationChanged" method is called but I don't what to do in order to make my App refresh with the newly selected language.

Please Note: I am not changing the language in my own app, instead I want to react to when a user changes the language in the Android device settings themselves.

Also, I am assuming the same does not apply to iOS because it appears as though an iOS device will restart if the language is changed?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Joe Manke 1,091 Reputation points
    2021-03-22T16:22:20.873+00:00

    Have you tried setting CurrentThread.CurrentCulture again from OnConfigurationChanged()?

    Alternatively, remove the flag from ConfigurationChanges and just let the OS restart your app on Android. It's a lot easier.

    1 person found this answer helpful.