Locale.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Globalization;
  3. using UnityEngine;
  4. #if UNITY_IOS && !UNITY_EDITOR
  5. using System.Runtime.InteropServices;
  6. #endif
  7. namespace Unity.Services.Analytics.Internal
  8. {
  9. static class Locale
  10. {
  11. #if UNITY_IOS && !UNITY_EDITOR
  12. [DllImport("__Internal")]
  13. private static extern string unity_services_current_language_code();
  14. internal static string CurrentLanguageCode()
  15. {
  16. return unity_services_current_language_code();
  17. }
  18. #elif UNITY_ANDROID && !UNITY_EDITOR
  19. internal static string CurrentLanguageCode()
  20. {
  21. AndroidJavaClass localeClass = new AndroidJavaClass("java.util.Locale");
  22. AndroidJavaObject defaultLocale = localeClass.CallStatic<AndroidJavaObject>("getDefault");
  23. return defaultLocale.Call<string>("getLanguage");
  24. }
  25. #else
  26. internal static string CurrentLanguageCode()
  27. {
  28. return CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
  29. }
  30. #endif
  31. public static string AnalyticsRegionLanguageCode()
  32. {
  33. // As we can't reliably report current country code (as the only country code we have access to is the region settings,
  34. // not the user's current country as expected by the Analytics service) then we return ZZ to have the Analytics service
  35. // infer country from GeoIP instead.
  36. return $"{CurrentLanguageCode()}_ZZ";
  37. }
  38. /// <summary>
  39. /// Returns the current culture info. Invokes native method on Android and iOS.
  40. /// </summary>
  41. /// <returns></returns>
  42. [Obsolete("The 'language-regionSettingsCountry' code used by Analytics is non-standard, so this method may throw exceptions when used on systems with non-ISO language/region combinations. Prefer using AnalyticsRegionLanguageCode instead.")]
  43. public static CultureInfo CurrentCulture()
  44. {
  45. return CultureInfo.CurrentCulture;
  46. }
  47. /// <summary>
  48. /// Returns the current culture info. Invokes native method on iOS.
  49. /// </summary>
  50. /// <returns></returns>
  51. [Obsolete("The 'language-regionSettingsCountry' code used by Analytics is non-standard, so this method may throw exceptions when used on systems with non-ISO language/region combinations. Prefer using AnalyticsRegionLanguageCode instead.")]
  52. public static CultureInfo SystemCulture()
  53. {
  54. return CultureInfo.InvariantCulture;
  55. }
  56. }
  57. }