Unity3D/Tips

[펌] Automatically set Hindi language in Unity

blueasa 2020. 9. 15. 15:55

[추가] Android / iOS 방식이 달라서 플랫폼에 따라 Two Letter ISO 받는 방식을 다르게 적용

 

    /// <summary>
    /// Two Letter ISO Language
    /// [참조] https://lonewolfonline.net/list-net-culture-country-codes/
    /// [참조] https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
    /// </summary>
    /// <returns></returns>
    public static string GetTwoLetterISOLanguage()
    {
        string strCurrentCultureName = "en";
#if UNITY_EDITOR
        strCurrentCultureName = "en";
#elif UNITY_ANDROID
        strCurrentCultureName = GetTwoLetterISOLanguage_Android();
#elif UNITY_IOS || UNITY_IPHONE
        strCurrentCultureName = GetTwoLetterISOLanguage_iOS();
#endif
        return strCurrentCultureName;
    }

    public static string GetTwoLetterISOLanguage_iOS()
    {
        string strTwoLetterISOLanguageName = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
        Debug.LogFormat("[GetCurrentCultureName_iOS] {0}", strTwoLetterISOLanguageName);
        return strTwoLetterISOLanguageName;
    }

    // returns "en" / "de" / "hi" / "th" / "ar" / ...
    public static string GetTwoLetterISOLanguage_Android()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        try
        {
            var locale = new AndroidJavaClass("java.util.Locale");
            var localeInst = locale.CallStatic<AndroidJavaObject>("getDefault");
            var name = localeInst.Call<string>("getLanguage");
            Debug.LogFormat("[getLanguage] {0}", name);
            return name;
        }
        catch (System.Exception e)
        {
            return "Error";
        }
#else
        return "Not supported";
#endif
    }

    // returns "eng" / "deu" / "hin" / ...
    public static string GetThreeLetterISOLanguage_Android()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        try
        {
            var locale = new AndroidJavaClass("java.util.Locale");
            var localeInst = locale.CallStatic<AndroidJavaObject>("getDefault");
            var name = localeInst.Call<string>("getISO3Language");
            Debug.LogFormat("[getISO3Language] {0}", name);
            return name;
        }
        catch (System.Exception e)
        {
            return "Error";
        }
#else
        return "Not supported";
#endif
    }

[출처]

answers.unity.com/questions/729801/using-applicationsystemlanguage-returns-unknown.html?_ga=2.105896064.227436567.1600081424-730033693.1580204381

 

Using Application.systemLanguage returns Unknown - Unity Answers

 

answers.unity.com

 

 

[참조]

You should also be able to use .NET to get the current culture instead:

using System;
using System.Globalization;
using System.Threading;

CultureInfo myCulture = Thread.CurrentThread.CurrentCulture;


// You can then use all these member variables
myCulture.DisplayName
myCulture.EnglishName
myCulture.Name (e.g. es-ES / en-GB)
myCulture.Parent (e.g. es / en)
myCulture.ThreeLetterISOLanguageName (e.g. spa/eng/hin)
myCulture.TwoLetterISOLanguageName (e.g. es/en/hi)

 

Here's a selection of the Indian 3 and 2 letter ISO codes (see attached jpg)

 

[출처] forum.unity.com/threads/automatically-set-hindi-language.624973/

 

Automatically set Hindi language

As you all know it's possible to use SystemLanguage to detect the default language of the device running the game. When my game opens, i use it to...

forum.unity.com

 

반응형