using UnityEngine; using System.Collections.Generic; namespace Lean.Localization { /// This contains the translated value for the current language, and other associated data. public class LeanTranslation { public struct Entry { public string Language; public Object Owner; } /// The name of this translation. public string Name { get { return name; } } [SerializeField] private string name; /// The data of this translation (e.g. string or Object). /// NOTE: This is a System.Object, so you must correctly cast it back before use. public object Data; /// If Data has been filled with data for the primary language, then this will be set to true. public bool Primary; /// This stores a list of all LeanSource instances that are currently managing the current value of this translation in the current language. /// NOTE: If this is empty then no LeanSource of this name is localized for the current language. public List Entries { get { return entries; } } private List entries = new List(); private static bool buffering; private static System.Text.StringBuilder current = new System.Text.StringBuilder(); private static System.Text.StringBuilder buffer = new System.Text.StringBuilder(); private static List tokens = new List(); public LeanTranslation(string newName) { name = newName; } public void Register(string language, Object owner) { var entry = new Entry(); entry.Language = language; entry.Owner = owner; entries.Add(entry); } public void Clear() { Data = null; Primary = false; entries.Clear(); } public int LanguageCount(string language) { var total = 0; for (var i = entries.Count - 1; i >= 0; i--) { if (entries[i].Language == language) { total += 1; } } return total; } /// This returns Text with all tokens substituted using the LeanLocalization.Tokens list. public static string FormatText(string rawText, string currentText, LeanLocalizedBehaviour behaviour) { if (string.IsNullOrEmpty(currentText) == true) { currentText = rawText; } if (rawText != null) { current.Length = 0; buffer.Length = 0; tokens.Clear(); for (var i = 0; i < rawText.Length; i++) { var rawChar = rawText[i]; if (rawChar == '{') { if (buffering == true) { buffering = false; buffer.Length = 0; } else { buffering = true; } } else if (rawChar == '}') { if (buffering == true) { if (buffer.Length > 0) { var token = default(LeanToken); if (buffer.Length > 0 && LeanLocalization.CurrentTokens.TryGetValue(buffer.ToString(), out token) == true) // TODO: Avoid ToString here? { current.Append(token.Value); tokens.Add(token); } else { current.Append('{').Append(buffer).Append('}'); } buffer.Length = 0; } buffering = false; } } else { if (buffering == true) { buffer.Append(rawChar); } else { current.Append(rawChar); } } } if (Match(currentText, current) == false) { if (behaviour != null) { behaviour.UnregisterAll(); } for (var i = tokens.Count - 1; i >= 0; i--) { var token = tokens[i]; token.Register(behaviour); behaviour.Register(token); } return current.ToString(); } } return currentText; } private static bool Match(string a, System.Text.StringBuilder b) { if (a == null && b.Length > 0) { return false; } if (a.Length != b.Length) { return false; } for (var i = 0; i < a.Length; i++) { if (a[i] != b[i]) { return false; } } return true; } } }