LanguageSourceData.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using UnityEngine;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using Object = UnityEngine.Object;
  6. namespace I2.Loc
  7. {
  8. public interface ILanguageSource
  9. {
  10. LanguageSourceData SourceData { get; set; }
  11. }
  12. [ExecuteInEditMode]
  13. [Serializable]
  14. public partial class LanguageSourceData
  15. {
  16. #region Variables
  17. [NonSerialized] public ILanguageSource owner;
  18. public Object ownerObject { get { return owner as UnityEngine.Object; } }
  19. public bool UserAgreesToHaveItOnTheScene = false;
  20. public bool UserAgreesToHaveItInsideThePluginsFolder = false;
  21. public bool GoogleLiveSyncIsUptoDate = true;
  22. [NonSerialized] public bool mIsGlobalSource;
  23. #endregion
  24. #region Variables : Terms
  25. public List<TermData> mTerms = new List<TermData>();
  26. public bool CaseInsensitiveTerms = false;
  27. //This is used to overcome the issue with Unity not serializing Dictionaries
  28. [NonSerialized] public Dictionary<string, TermData> mDictionary = new Dictionary<string, TermData>(StringComparer.Ordinal);
  29. public enum MissingTranslationAction { Empty, Fallback, ShowWarning, ShowTerm };
  30. public MissingTranslationAction OnMissingTranslation = MissingTranslationAction.Fallback;
  31. public string mTerm_AppName;
  32. #endregion
  33. #region Variables : Languages
  34. public List<LanguageData> mLanguages = new List<LanguageData>();
  35. public bool IgnoreDeviceLanguage; // If false, it will use the Device's language as the initial Language, otherwise it will use the first language in the source.
  36. public enum eAllowUnloadLanguages { Never, OnlyInDevice, EditorAndDevice }
  37. public eAllowUnloadLanguages _AllowUnloadingLanguages = eAllowUnloadLanguages.Never;
  38. #endregion
  39. #region Variables : Google
  40. public string Google_WebServiceURL;
  41. public string Google_SpreadsheetKey;
  42. public string Google_SpreadsheetName;
  43. public string Google_LastUpdatedVersion;
  44. #if UNITY_EDITOR
  45. public string Google_Password = "change_this";
  46. #endif
  47. public enum eGoogleUpdateFrequency { Always, Never, Daily, Weekly, Monthly, OnlyOnce, EveryOtherDay }
  48. public eGoogleUpdateFrequency GoogleUpdateFrequency = eGoogleUpdateFrequency.Weekly;
  49. public eGoogleUpdateFrequency GoogleInEditorCheckFrequency = eGoogleUpdateFrequency.Daily;
  50. // When Manual, the user has to call LocalizationManager.ApplyDownloadedDataFromGoogle() during a loading screen or similar
  51. public enum eGoogleUpdateSynchronization { Manual, OnSceneLoaded, AsSoonAsDownloaded }
  52. public eGoogleUpdateSynchronization GoogleUpdateSynchronization = eGoogleUpdateSynchronization.OnSceneLoaded;
  53. public float GoogleUpdateDelay = 0; // How many second to delay downloading data from google (to avoid lag on the startup)
  54. public event LanguageSource.fnOnSourceUpdated Event_OnSourceUpdateFromGoogle; // (LanguageSource, bool ReceivedNewData, string errorMsg)
  55. #endregion
  56. #region Variables : Assets
  57. public List<Object> Assets = new List<Object>(); // References to Fonts, Atlasses and other objects the localization may need
  58. //This is used to overcome the issue with Unity not serializing Dictionaries
  59. [NonSerialized] public Dictionary<string, Object> mAssetDictionary = new Dictionary<string, Object>(StringComparer.Ordinal);
  60. #endregion
  61. #region EditorVariables
  62. #if UNITY_EDITOR
  63. public string Spreadsheet_LocalFileName;
  64. public string Spreadsheet_LocalCSVSeparator = ",";
  65. public string Spreadsheet_LocalCSVEncoding = "utf-8";
  66. public bool Spreadsheet_SpecializationAsRows = true;
  67. #endif
  68. #endregion
  69. #region Language
  70. public void Awake()
  71. {
  72. LocalizationManager.AddSource (this);
  73. UpdateDictionary();
  74. UpdateAssetDictionary();
  75. LocalizationManager.LocalizeAll(true);
  76. }
  77. public void OnDestroy()
  78. {
  79. LocalizationManager.RemoveSource(this);
  80. }
  81. public bool IsEqualTo( LanguageSourceData Source )
  82. {
  83. if (Source.mLanguages.Count != mLanguages.Count)
  84. return false;
  85. for (int i=0, imax=mLanguages.Count; i<imax; ++i)
  86. if (Source.GetLanguageIndex( mLanguages[i].Name ) < 0)
  87. return false;
  88. if (Source.mTerms.Count != mTerms.Count)
  89. return false;
  90. for (int i=0; i<mTerms.Count; ++i)
  91. if (Source.GetTermData(mTerms[i].Term)==null)
  92. return false;
  93. return true;
  94. }
  95. internal bool ManagerHasASimilarSource()
  96. {
  97. for (int i=0, imax=LocalizationManager.Sources.Count; i<imax; ++i)
  98. {
  99. LanguageSourceData source = (LocalizationManager.Sources[i] as LanguageSourceData);
  100. if (source!=null && source.IsEqualTo(this) && source!=this)
  101. return true;
  102. }
  103. return false;
  104. }
  105. public void ClearAllData()
  106. {
  107. mTerms.Clear ();
  108. mLanguages.Clear ();
  109. mDictionary.Clear();
  110. mAssetDictionary.Clear();
  111. }
  112. public bool IsGlobalSource()
  113. {
  114. return mIsGlobalSource;
  115. }
  116. #endregion
  117. public void Editor_SetDirty()
  118. {
  119. #if UNITY_EDITOR
  120. if (ownerObject != null)
  121. {
  122. UnityEditor.EditorUtility.SetDirty(ownerObject);
  123. }
  124. #endif
  125. }
  126. }
  127. }