LocalizationManager_SystemLanguage.cs 1.7 KB

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