[英]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. 我正在使用Unity 2019.2b开发一个项目,它有望在所有平台(android,ios,windows,macos)上工作,我正在尝试获取设备区域设置信息(例如en-us,en-au,en-bz)但我能找到的只是Application.systemLanguage定义。 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?
是否有任何样本或方法可以在Unity中获取区域设置信息或获取设备区域信息? (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)
(如果只有区域信息的解决方案;我不想使用设备位置或不想使用用户IP地址,我想从设置中获取它)
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. 我尝试使用RegionInfo,CultureInfo,System.Threading.Thread.CurrentThread.CurrentUICulture和System.Threading.Thread.CurrentThread.CurrentCulture定义获取区域设置或至少设备区域信息,但它不起作用。
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. 根据平台(ios,android,windows,macos),有时结果是InvariantCulture,有时总是en-US甚至设备区域设置不是en-US。
The issue is coming from Mono, used by Unity. 问题来自Unity使用的Mono。
I found a few solutions in this forum thread . 我在这个论坛帖子中找到了一些解决方案。
If you are working with Windows a workaround might be using PInvoke: 如果您正在使用Windows,则解决方法可能是使用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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.