saurabh bohra
saurabh bohra

Reputation: 27

Thread.CurrentThread.CurrentUICulture returns different CultureInfo for the same device for the same settings in Android

We are developing an application that requires the amount to be displayed. Now, this amount needs to be displayed in a formatted way with appropriate currency symbol like $,£ etc depending upon the region the application is being run.

Thread.CurrentThread.CurrentUICulture seems to work fine for UWP and returns CulturalInfo depending upon the settings saved in the operating system/Windows machine.

But, I have observed that for android this code returns different CulturalInfo and results in an amount to be displayed with $ sometimes and £ the other.

It will be helpful if someone over the community can help. Thanks in advance.

Upvotes: 0

Views: 270

Answers (1)

Raimo
Raimo

Reputation: 1536

As @Cheesebaron already mentioned in the comments, you will have to use Java.Util.Locale.Default.

In order to convert this into an CultureInfo object, you will have to understand how Java.Util.Locale and System.Globalization.CultureInfo are formatted.

Java.Util.Locale uses the following format:

<language code>[_<country code>[_<variant code>]]

While System.Globalization.CultureInfo uses the following format:

languagecode2>-country/regioncode2

Therefor, the difference lies in the seperator they use: - versus _.

The following code snippet should give you an idea how to tackle this problem:

public System.Globalization.CultureInfo GetCurrentCultureInfo )
{
    var androidLocale = Java.Util.Locale.Default;
    var netLanguage = androidLocale.ToString().Replace ("_", "-");

    return new System.Globalization.CultureInfo(netLanguage);
}

Upvotes: 4

Related Questions