Why current culture is always en-US and what is the alternative ?
Hello everybody,
I'm using the WWW class to perform a https request and I don't specify accept-language in the headers.
My problem is: the server receives always en-US as Accept-Language in the headers of the request, regardless of the real country of the user.
I want to retrieve the currentCulture of the user and pass it to the WWW class.
But I don't know how to retrieve it ! I read in another threads that System.Globalization.CultureInfo.CurrentCulture and System.Threading.Thread.CurrentThread.CurrentCulture are set to "en-US" by default. Do you know another way to retrieve the culture code ?
PS: I would like to avoid using a geolocation plugin as much as I can because our server relies on currentculture instead of ips to detect the location of the user. It would represent a heavy update to filter by ip rather than culture code.
Thank you for your help guys !
Answer by Eldoir · Mar 07, 2018 at 12:16 PM
Hello there, I found the answer of @streeter12 very interesting but I got an exception when I tried to use the CultureInfo with date formatting. After nearly an hour of struggle and research in obscure documentation, I finally managed to create a broader method, that works with formatting. Here it is!
public static CultureInfo GetCurrentCultureInfo()
{
SystemLanguage currentLanguage = Application.systemLanguage;
CultureInfo correspondingCultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(x => x.EnglishName.Equals(currentLanguage.ToString()));
return CultureInfo.CreateSpecificCulture(correspondingCultureInfo.TwoLetterISOLanguageName);
}
Don't forget to add using System.Linq
and using System.Globalization
on top of your script. I'm now able to write something like this :
Debug.Log(System.DateTime.Now.ToString("dd MMMM", GetCurrentCultureInfo()));
And as I'm french, it displays as "07 mars".
Answer by streeter12 · Feb 16, 2017 at 12:10 PM
You can use this function to get CultureInfo from SystemLanguage enum values.
private static CultureInfo GetCultureDefualt(SystemLanguage language)
{
return CultureInfo.GetCultures(CultureTypes.AllCultures).
FirstOrDefault(x => x.EnglishName == Enum.GetName(language.GetType(), language));
}
Answer by Raitoning · Jun 11, 2016 at 04:53 PM
Hello @VivienAdnot,
I've got the exact same problem as you. I really need to retrieve the current culture of the player's system to adapt the display of the date and time to the player's local standards. As no one answered you, I've created a thread on the forum to find some help. I've also put a link to this question in the thread. I hope someone will find a solution to this.
Link to the thread: http://forum.unity3d.com/threads/c-currentculture-always-return-en-us.410482/
Thanks for reading :)
Answer by Stals · Sep 02, 2016 at 12:38 PM
As I can see this now, there is just no good solution (aside from making a plugin that supports all platforms but it surely feels like Unity should do it in-engine), but we can vote for this task would be resolved https://feedback.unity3d.com/suggestions/fix-localization-issues-with-cor (all of my 10 votes goes towards it)
Answer by Michael-Ryan · Jul 28, 2020 at 11:12 PM
I realize this is an old post, but I found a work around to getting the actual values that the user set in their operating system.
Details are posted here: https://stackoverflow.com/questions/63125198/how-to-convert-datetime-to-string-using-region-short-date/63125506#63125506