TermData.cs 4.1 KB

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