Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting user's language preference?

Discussion in 'Editor & General Support' started by Joe ByDesign, Jun 27, 2007.

  1. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Here's a sticky wicket problem;

    I wish to get the user's current language preference and set game localization options from that. The question is, is there support for this natively?

    Have tried using the following from Mono to no avail;
    • System.Globalization.RegionInfo.CurrentRegion (returns null)
      System.Globalization.RegionInfo.ThreeLetterISORegionName (call fails)
      System.StringComparer.CurrentCulture (long shot, is null)
    Of course, Mono docs are largely unwritten, so it makes the issue more difficult.

    Jeremy (on IRC) mentioned this OSX specific info, but it's likely more for OTEE than for us users;
    http://developer.apple.com/document...pple_ref/doc/uid/20002397-DontLinkElementID_8

    Any ideas guys?

    I would like to avoid having to ask the player (who just wants to be entertained) to answer a bunch of not-fun questions at startup, so is there a way to avoid this?

    Else, do I need to hand code a custom solution for each platform (mac, windows, web)? Would rather it be something internal.
     
  2. lgoss007

    lgoss007

    Joined:
    Dec 6, 2006
    Posts:
    88
    I know the System library of Mono as of version 1.0.5000.0 supports the:
    Code (csharp):
    1. System.Globalization.RegionInfo.CurrentRegion
    It returns United States on my linux box, so I know that's implemented in Mono. Maybe OTEE can clue us in some more.
     
  3. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    this worked for me:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Globalization;
    5.  
    6. public class SamplesRegionInfo : MonoBehaviour {
    7.  
    8.    void Start ()  {
    9.         Debug.Log("hi there");
    10.       // Displays the property values of the RegionInfo for "AU".
    11.       RegionInfo myRI1 = new RegionInfo( "AU" );
    12.       Debug.Log( "   Name:                         "+ myRI1.Name );
    13.       Debug.Log( "   DisplayName:                  "+ myRI1.DisplayName );
    14.       Debug.Log( "   EnglishName:                  "+ myRI1.EnglishName );
    15.       Debug.Log( "   IsMetric:                     "+ myRI1.IsMetric );
    16.       Debug.Log( "   ThreeLetterISORegionName:     "+ myRI1.ThreeLetterISORegionName );
    17.       Debug.Log( "   ThreeLetterWindowsRegionName: "+ myRI1.ThreeLetterWindowsRegionName );
    18.       Debug.Log( "   TwoLetterISORegionName:       "+ myRI1.TwoLetterISORegionName );
    19.       Debug.Log( "   CurrencySymbol:               "+ myRI1.CurrencySymbol );
    20.       Debug.Log( "   ISOCurrencySymbol:            "+ myRI1.ISOCurrencySymbol );
    21.       Debug.Log("");
    22.  
    23.       // Compares the RegionInfo above with another RegionInfo created using CultureInfo.
    24.       RegionInfo myRI2 = new RegionInfo( new CultureInfo("en-AU",false).LCID );
    25.       if ( myRI1.Equals( myRI2 ) )
    26.          Debug.Log( "The two RegionInfo instances are equal." );
    27.       else
    28.          Debug.Log( "The two RegionInfo instances are NOT equal." );
    29.  
    30.    }
    31.  
    32. }
    33.  
    Grabbed and converted from here:

    http://msdn2.microsoft.com/en-us/library/system.globalization.regioninfo.aspx

    Cheers.
     
  4. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Hi thylaxene, thanks for this. I saw it and also converted the sample of CurrentCulture with some of the parameters returning, but they all seemed to be based on whether or not RegionInfo is known.

    Unless I'm missing something, in the example you provided, it will return info on any region that RegionInfo is set to ("AU" in this case).

    This is useful if you already know the region, but I'm trying to "get" the region from the current users system.

    Is this possible? Or am I missing it? (very possible)
     
  5. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Well 2.0 is out. It looks like some of these calls no longer give error when used, but I haven't been able to get any useful info to find the user's current language setting.

    For example; ThreeLetterISOLanguageName return IVL which I understand is for Invariant.
    ____

    It's pretty clear that CultureInfo isn't probably what I need here; it returns info if you know the language already.

    Any ideas on how to get the user's current language setting (outside of a custom Plugin) is much appreciated.

    Until then, I guess I'll have to go with a language select screen after all :(
     
  6. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    All said and done I think this is the best option... As I believe just because you live in some such country doesn't always mean you want to use that language in the game you are about to play... I personally think you will get more complaints than thanks if you automatically assume anything regarding what a end user wants to do.

    Cheers.
     
  7. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Thanks Jon, I agree with your comment for the most part, it's just that initial "hey you launched this to be entertained but first answer this question" that I don't care for.

    In any case, even if it were automatic, there would still exist an option to change the language, but it wouldn't be the first thing the player sees when they just want to be entertained.
     
  8. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    To use date format,
    I found that the CurrentCulture gives always a wring value with the code located in a Unity script (cs)

    lg2 = System.Globalization.CultureInfo.CurrentCulture.EnglishName;

    lg2 => "LG Invariant Language (Invariant Country)"

    if I use the same code in Mono 2.4 in a small app

    Code (csharp):
    1.  
    2. using System;
    3. using System.Globalization;
    4.  
    5. public class HelloWorld
    6. {
    7.     static public void Main ()
    8.     {
    9.           string lg;
    10.           lg = System.Globalization.CultureInfo.CurrentCulture.EnglishName;
    11.                    
    12.         Console.WriteLine(lg);
    13.        
    14.         IFormatProvider culture = System.Globalization.CultureInfo.CurrentCulture;
    15.         DateTime tt = Convert.ToDateTime("24/12/2009", culture);    
    16.                
    17.         Console.WriteLine (tt.ToString());
    18.     }
    19.  
    20. }
    21.  
    22.  
    The lg is equal to "French (France)" and then I can use the ToDateTime function.

    How can I know the culture or language used by the user on his machine without asking of course ?


    Thks,
     
  9. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Yes, found the same; Culture gives you useful formatting once you know the lang, but found that getting the lang pref reliably across target platforms wasn't tenable.

    In the end, in Make Bouncy Bouncy we went with the (ugh) "1st launch, ask user" solution...

    It's not preferred, but it works.
     
  10. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    Thanks for your reply
    but in our case, we are reading a xml that contains date in English or French format during the launching of our app.
    So it's not a solution for us. We'll modify the storage of the date to avoid this.
     
  11. yoyo

    yoyo

    Joined:
    Apr 16, 2010
    Posts:
    112
    Old thread, but still an issue -- I just confirmed that System.Globalization.CultureInfo.CurrentCulture still isn't initialized correctly under Unity 3.5.6.

    Running in Unity, it always reports en-US. In a small .NET program compiled with Microsoft csc.exe it's en-CA (I'm in Canada).

    I figured it was a Mono problem, but I recompiled my same .NET program with Mono (the version that came with Unity) and it reports en-CA just fine. So something's wrong in Unity's version of the Mono runtime.

    I downloaded the latest mono source from github and I have to say the following is mighty suspicious! It's in the "eglib" folder, which I take to be a reference implementation of the CLR -- likely what Unity used as a starting point.

    Code (csharp):
    1. gchar*
    2. g_win32_getlocale(void)
    3. {
    4.     /* FIXME: Use GetThreadLocale
    5.      * and convert LCID to standard
    6.      * string form, "en_US" */
    7.     return strdup ("en_US");
    8. }
    From that comment I dug further, and tried calling GetThreadLocale, but it returns 1033, the ID for en-US. However there is another method, GetSystemDefaultLCID, which *does* get the right ID for my system. So ... this (partially) works!

    Code (csharp):
    1. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
    2. private static extern int GetSystemDefaultLCID();
    3. CultureInfo GetSystemCulture()
    4. {
    5.     return new CultureInfo(GetSystemDefaultLCID());
    6. }
    7.  
    This works in the Unity Editor and in a standalone Windows build. It does not work in the web player -- an exception is thrown when attempting to call GetSystemDefaultLCID. (Mac and mobile testing left as an exercise for the reader.)
     
  12. Michitaro-Naito

    Michitaro-Naito

    Joined:
    Jun 15, 2013
    Posts:
    14
    I know it's an old post though share information...

    Unity3D supports language preferences natively.
    See UnityEngine.Application.systemLanguage.
    It works fine for my Mac, Win, iOS and Android devices.

    I hope it helps ;)
     
  13. Piotrku

    Piotrku

    Joined:
    Nov 18, 2012
    Posts:
    14
    If Application.systemLanguage isn't enough for you - please check out this plugin http://u3d.as/vXf

    You might read user's currency symbol and region on iOS and Android