LocalizationManager_SystemLanguage.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5. using System.Globalization;
  6. using System.Collections;
  7. namespace I2.Loc
  8. {
  9. public static partial class LocalizationManager
  10. {
  11. static string mCurrentDeviceLanguage;
  12. public static string GetCurrentDeviceLanguage( bool force = false )
  13. {
  14. if (force || string.IsNullOrEmpty(mCurrentDeviceLanguage))
  15. DetectDeviceLanguage();
  16. return mCurrentDeviceLanguage;
  17. }
  18. static void DetectDeviceLanguage()
  19. {
  20. #if UNITY_ANDROID && !UNITY_EDITOR
  21. try {
  22. AndroidJavaObject locale = new AndroidJavaClass("java/util/Locale").CallStatic<AndroidJavaObject>("getDefault");
  23. mCurrentDeviceLanguage = locale.Call<string>("toString");
  24. //https://stackoverflow.com/questions/4212320/get-the-current-language-in-device
  25. if (!string.IsNullOrEmpty(mCurrentDeviceLanguage))
  26. {
  27. mCurrentDeviceLanguage = mCurrentDeviceLanguage.Replace('_', '-');
  28. mCurrentDeviceLanguage = GoogleLanguages.GetLanguageName(mCurrentDeviceLanguage, true, true);
  29. if (!string.IsNullOrEmpty(mCurrentDeviceLanguage))
  30. return;
  31. }
  32. }
  33. catch (System.Exception)
  34. {
  35. }
  36. #endif
  37. mCurrentDeviceLanguage = Application.systemLanguage.ToString();
  38. if (mCurrentDeviceLanguage == "ChineseSimplified") mCurrentDeviceLanguage = "Chinese (Simplified)";
  39. if (mCurrentDeviceLanguage == "ChineseTraditional") mCurrentDeviceLanguage = "Chinese (Traditional)";
  40. }
  41. }
  42. }