TermData.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 enum eTermType
  9. {
  10. Text, Font, Texture, AudioClip, GameObject, Sprite, Material, Child, Mesh,
  11. #if NGUI
  12. UIAtlas, UIFont,
  13. #endif
  14. #if TK2D
  15. TK2dFont, TK2dCollection,
  16. #endif
  17. #if TextMeshPro
  18. TextMeshPFont,
  19. #endif
  20. #if SVG
  21. SVGAsset,
  22. #endif
  23. Object
  24. }
  25. public enum TranslationFlag : byte
  26. {
  27. Normal = 1,
  28. AutoTranslated = 2,
  29. }
  30. [Serializable]
  31. public class TermData
  32. {
  33. public string Term = string.Empty;
  34. public eTermType TermType = eTermType.Text;
  35. #if !UNITY_EDITOR
  36. [NonSerialized]
  37. #endif
  38. public string Description;
  39. public string[] Languages = new string[0];
  40. public byte[] Flags = new byte[0]; // flags for each translation
  41. [SerializeField] private string[] Languages_Touch = null; // TO BE REMOVED IN A FUTURE RELEASE
  42. public string GetTranslation ( int idx, string specialization=null, bool editMode=false )
  43. {
  44. string text = Languages[idx];
  45. if (text != null)
  46. {
  47. text = SpecializationManager.GetSpecializedText(text, specialization);
  48. if (!editMode)
  49. {
  50. text = text.Replace("[i2nt]", "").Replace("[/i2nt]", "");
  51. }
  52. }
  53. return text;
  54. }
  55. public void SetTranslation( int idx, string translation, string specialization = null)
  56. {
  57. Languages[idx] = SpecializationManager.SetSpecializedText( Languages[idx], translation, specialization);
  58. }
  59. public void RemoveSpecialization(string specialization)
  60. {
  61. for (int i = 0; i < Languages.Length; ++i)
  62. RemoveSpecialization(i, specialization);
  63. }
  64. public void RemoveSpecialization( int idx, string specialization )
  65. {
  66. var text = Languages[idx];
  67. if (specialization == "Any" || !text.Contains("[i2s_" + specialization + "]"))
  68. {
  69. return;
  70. }
  71. var dict = SpecializationManager.GetSpecializations(text);
  72. dict.Remove(specialization);
  73. Languages[idx] = SpecializationManager.SetSpecializedText(dict);
  74. }
  75. public bool IsAutoTranslated( int idx, bool IsTouch )
  76. {
  77. return (Flags[idx] & (byte)TranslationFlag.AutoTranslated) > 0;
  78. }
  79. public void Validate ()
  80. {
  81. int nLanguages = Mathf.Max(Languages.Length, Flags.Length);
  82. if (Languages.Length != nLanguages) Array.Resize(ref Languages, nLanguages);
  83. if (Flags.Length!=nLanguages) Array.Resize(ref Flags, nLanguages);
  84. if (Languages_Touch != null)
  85. {
  86. for (int i = 0; i < Mathf.Min(Languages_Touch.Length, nLanguages); ++i)
  87. {
  88. if (string.IsNullOrEmpty(Languages[i]) && !string.IsNullOrEmpty(Languages_Touch[i]))
  89. {
  90. Languages[i] = Languages_Touch[i];
  91. Languages_Touch[i] = null;
  92. }
  93. }
  94. Languages_Touch = null;
  95. }
  96. }
  97. public bool IsTerm( string name, bool allowCategoryMistmatch)
  98. {
  99. if (!allowCategoryMistmatch)
  100. return name == Term;
  101. return name == LanguageSourceData.GetKeyFromFullTerm (Term);
  102. }
  103. public bool HasSpecializations()
  104. {
  105. for (int i = 0; i < Languages.Length; ++i)
  106. {
  107. if (!string.IsNullOrEmpty(Languages[i]) && Languages[i].Contains("[i2s_"))
  108. return true;
  109. }
  110. return false;
  111. }
  112. public List<string> GetAllSpecializations()
  113. {
  114. List<string> values = new List<string>();
  115. for (int i = 0; i < Languages.Length; ++i)
  116. SpecializationManager.AppendSpecializations(Languages[i], values);
  117. return values;
  118. }
  119. };
  120. public class TermsPopup : PropertyAttribute
  121. {
  122. public TermsPopup(string filter = "")
  123. {
  124. this.Filter = filter;
  125. }
  126. public string Filter { get; private set; }
  127. }
  128. }