public void Unregister(ILocalizationHandler handler) { if (handlers != null) { handlers.Remove(handler); } }
public void Register(ILocalizationHandler handler) { if (handler != null) { if (handlers == null) { handlers = new HashSet <ILocalizationHandler>(); } handlers.Add(handler); } }
/// <summary>This returns Text with all tokens substituted using the LeanLocalization.Tokens list.</summary> public static string FormatText(string rawText, string currentText, ILocalizationHandler handler) { 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 (handler != null) { handler.UnregisterAll(); } for (var i = tokens.Count - 1; i >= 0; i--) { var token = tokens[i]; token.Register(handler); handler.Register(token); } return(current.ToString()); } } return(currentText); }
/// <summary>This returns Text with all tokens substituted using the LeanLocalization.Tokens list. /// NOTE: If you want local tokens to work, then specify the localTokenRoot GameObject.</summary> public static string FormatText(string rawText, string currentText = null, ILocalizationHandler handler = null, GameObject localTokenRoot = null) { 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); // Try and replace local tokens first if (buffer.Length > 0 && localTokenRoot != null && LeanLocalToken.TryGetLocalToken(localTokenRoot, buffer.ToString(), ref token) == true) // TODO: Avoid ToString here? { current.Append(token.Value); tokens.Add(token); } // Try and replace global tokens second else if (buffer.Length > 0 && LeanLocalization.CurrentTokens.TryGetValue(buffer.ToString(), out token) == true) // TODO: Avoid ToString here? { current.Append(token.Value); tokens.Add(token); } // If none found, leave the token text as it was 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 (handler != null) { handler.UnregisterAll(); for (var i = tokens.Count - 1; i >= 0; i--) { var token = tokens[i]; token.Register(handler); handler.Register(token); } } return(current.ToString()); } } return(currentText); }