question

GordonS-9701 avatar image
0 Votes"
GordonS-9701 asked ·

Xamarin Forms detect device language has changed

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?

dotnet-xamarinforms
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

jcmanke avatar image
0 Votes"
jcmanke answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.