简体   繁体   中英

How can i get locale info in Unity C#?

I am developing a project with Unity 2019.2b, it is expected to work at all platforms (android, ios, windows, macos) , i am trying to get device locale info(eg en-us, en-au, en-bz) but all i can find is Application.systemLanguage definition. Even i decide to use this information i will still need region info. Are there any sample or way to get locale info in Unity or to get device region info? (If the solution for only region info; I dont want to use device location or dont want to use user ip address, i want to get it from settings)

I tried to get locale or at least device region info with RegionInfo,CultureInfo,System.Threading.Thread.CurrentThread.CurrentUICulture and System.Threading.Thread.CurrentThread.CurrentCulture definitions, but it didn't work.

string regionName = System.Globalization.RegionInfo.CurrentRegion.Name;
string cultureName = System.Globalization.CultureInfo.CurrentCulture.Name;
string cname = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
string uiname = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;

Depending on the platform (ios, android, windows,macos) sometimes the result comes as InvariantCulture, sometimes always en-US even device locale is not en-US.

The issue is coming from Mono, used by Unity.

I found a few solutions in this forum thread .

If you are working with Windows a workaround might be using PInvoke:

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

And then check the global culture like so:

var currentCulture = new CultureInfo(GetSystemDefaultLCID());

This might be used to set the culture:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM