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”
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.
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
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)
– Stals