How can I determine the current locale, or at least the currency symbol?

I have tried multiple approaches - but they all return the wrong, or inadequate results.

My test system is set to en-GB out of the box but everything behaves as if it is en-US in Unity (I am testing in the editor).

  • Application.systemLanguage.ToString()
    returns “English” which is not very
    helpful at all
  • String.Format( "{0:C}", 0.0f ) gives me a dollar
    formatted value instead of pounds
  • System.Globalization.RegionInfo.CurrentRegion.CurrencySymbol
    returns “USD”

As I can see this now, there is just no good solution, but we can vote for this task would be resolved https://feedback.unity3d.com/suggestions/fix-localization-issues-with-cor (all of my 10 points goes towards it)

4 Answers

4

After some research, it looks like this is a problem with the Mono version used in Unity - current locale is always returned as “en-US”. As a workaround, if your application will work under Windows only, you can use PInvoke (example found here):

[System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
private static extern int GetSystemDefaultLCID();

And then, you can get current culture by calling:

var currentCulture = new CultureInfo(GetSystemDefaultLCID());

You can also set current culture globally, by using:

Thread.CurrentThread.CurrentCulture = new CultureInfo(GetSystemDefaultLCID());
Thread.CurrentThread.CurrentUICulture = new CultureInfo(GetSystemDefaultLCID());

at the beginning of your application. After this, CultureInfo.CurrentCulture (or CurrentUICulture) will return proper info, and {0:C} format should work as expected.

Unfortunately, RegionInfo.CurrentRegion will not return your true RegionInfo. I guess that Unity accesses CurrentRegion property before any of your scripts, and by accessing it, sets it to the earlier culture, which is en-US. But I believe this is not a big issue, as you can always create new RegionInfo(lcid), in case you need it.

One issue which doesn’t work - if someone has some specific regional settings in Windows, e.g. changed $ to $$, the currency symbol returned will be $. At least it’s the behavior on my machine.

thanks for taking the time to reply. this confirms my suspicion that this is a bug. unfortunately i am working on a multi-platform title and windows isn't one of our platforms... :/

I'm sure each of the platforms you're working with has a native method to get current locale. Maybe even in the same standard LCID form. You can write separate GetSystemDefaultLCID() for each platform, call native method inside, and after receiving data from native call create desired CultureInfo and pass it to current thread cultures. And then, just conditional compilation symbols around these methods, and you have a solution.

you are right, i am aware of the various apis available on all of my targets, but this is still a bug, although i can see an argument that it is just incredibly sloppy work instead... i will still make the effort to report it at some point. since this was for an optional 'stretch' goal i'm not too worried that i can't solve the problem with a reasonable amount of effort... thanks for the extensive feedback though, it is appreciated. :)

So we get a brand new real-time global illumination system in unity 5 ... but still no way in 2015 to properly get the user's current language or to use DLLs compiled against something else than the prehistoric 2.0 runtime. This sense of priorities is ... questionable.

The solution von ArkaneX is fine, but according to this articel: here from stackoverflow
We shouldn’t use the CultureInfo constructor. A faster way is:

CultureInfo culture = CultureInfo.GetCultureInfo(GetUserDefaultLCID());

Please check out this plugin: Precise Locale | Integration | Unity Asset Store

You can read user’s region and currency symbol using its API

I had a similar issue when trying to access the “short date” and “short time” formats that the user has defined in the operating system.

In my case, Unity recognized that I was using “en-US” for region settings, but it was always returning the default “short date” (M/d/yyy) and “short time” formats, which I has overridden (yyyy-MM-dd). I found a solution similar to calling GetSystemDefaultLCID(), but I used GetLocaleInfoEx() instead. You can use that function to get all sorts of information, including the currency symbols. There are more than 150 values that can be passed to a particular parameter that determines what will be returned. In my posted solution, I only deal with few date and time values, but you can access currency symbols, and much more.

Details are posted here: c# - How to convert DateTime to string using Region Short Date - Stack Overflow