- Home /
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 allString.Format( "{0:C}", 0.0f )
gives me a dollar formatted value instead of poundsSystem.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)
Answer by ArkaneX · Aug 17, 2013 at 01:05 AM
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. $$anonymous$$aybe 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 ins$$anonymous$$d... 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 illu$$anonymous$$ation 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.
Answer by Darkproduct · Mar 10, 2017 at 07:07 AM
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());
Answer by Piotrku · Jun 30, 2016 at 10:44 PM
Please check out this plugin: http://u3d.as/vXf
You can read user's region and currency symbol using its API
Answer by Michael-Ryan · Jul 28, 2020 at 11:20 PM
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: https://stackoverflow.com/questions/63125198/how-to-convert-datetime-to-string-using-region-short-date/63125506#63125506