question

GordonS-9701 avatar image
0 Votes"
GordonS-9701 asked pdschuller commented

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-xamarin
5 |1600 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
1 Vote"
jcmanke answered pdschuller commented

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
5 |1600 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.

Yep. Not including ConfigChanges.Locale worked for me. With the app running from Debug or not, the android OS pops the new language string in there.
NOTES: - this didn't seem to work the first time I tried it
- each time I try it (tapping Apply) the device seems to have to do some background work before it responds to my next click.

0 Votes 0 ·