2

I'm trying to read the language setting on iOS. So in the following example I'd like to get "en" or "English", but not "de" or "German":

I already inspected the whole CultureInfo.CurrentCulture object from System.Globalization and NSLocale.CurrentLocale from MonoTouch.Foundation. But I only see de_DE everywhere.

Falko
  • 17,076
  • 13
  • 60
  • 105

1 Answers1

10

Ok, I found a solution based on this very similar thread:

On iOS you can get the current language code as the first item of the "preferred languages":

NSLocale.PreferredLanguages[0]

On Android the cross-platform solution works just fine:

CultureInfo.CurrentCulture.TwoLetterISOLanguageName

To make the language code accessible to the shared code, I'd recommend a public static member of App.cs, which is initialized in OnCreate or FinishedLaunching, respectively.

Community
  • 1
  • 1
Falko
  • 17,076
  • 13
  • 60
  • 105
  • iOS solution: this will return a string in the well-known format "xx-YY" with xx being the language designator and YY being the region/country designator. but unlike Microsoft's platforms, this string can assume weird combinations. for example, I had ten iPhone 5s showcased for testing on a fair in Lisbon, and when plugging them in and running in the xamarin debugger, it returned "en-PT". – Cee McSharpface Jul 19 '18 at 19:53