using EasyTalk.Controller; using EasyTalk.Display; using EasyTalk.Settings; #if TEXTMESHPRO_INSTALLED using TMPro; #endif using UnityEngine; using UnityEngine.UI; namespace EasyTalk.Localization { /// /// AutoTranslate is a component which can be placed on a GameObject with a text component. When used with an appropriate TranslationLibrary, it /// enables the text to be translated automatically whenever the language is changes on the EasyTalkGameState. /// public class AutoTranslate : MonoBehaviour { #if TEXTMESHPRO_INSTALLED /// /// The text component to translate. /// [Tooltip("The text component to apply automatic translation to. Automatic translations are performed whenever the EasyTalkGameState language is " + "changed, or SetText() is called on this component.")] [SerializeField] private TMP_Text textMeshProText; #endif /// /// The text component to translate. /// [Tooltip("The text component to apply automatic translation to. Automatic translations are performed whenever the EasyTalkGameState language is " + "changed, or SetText() is called on this component.")] [SerializeField] private Text text; /// /// The translation library to use for translations. /// [Tooltip("The translation library to use when translating the text component's text.")] [SerializeField] private TranslationLibrary library; /// /// The set of fonts to use for various languages. /// [Tooltip("(Optional) A set of font overrides to use when the EasyTalkGameState language is changed to various languages. If left null, the AutoTranslate will attempt to pull " + "the language font overrides from an existing instance of EasyTalkDialogueSettings, which should exist if there is an active Dialogue Display.")] [SerializeField] private LanguageFontOverrides languageFontOverrides; /// /// The original text value. /// private string originalText; /// /// INitializes the AutoTranslate component. /// private void Awake() { if (text == null) { #if TEXTMESHPRO_INSTALLED textMeshProText = this.GetComponent(); #else text = this.GetComponent(); #endif } originalText = Text.text; EasyTalkGameState.Instance.onLanguageChanged += LanguageChanged; } /// /// Unregister the LanguageChanged method frm the onLanguageChanged delegate so that the EasyTalkGameState instance will no longer try to call this object. /// private void OnDestroy() { EasyTalkGameState.Instance.onLanguageChanged -= LanguageChanged; } /// /// Called whenever the language is changed on the EasyTalkGameState. This method will attempt to update the text to the translated version and update the /// font of the text element if necessary. /// /// The prior language being used. /// The new language to use. protected void LanguageChanged(string oldLanguage, string newLanguage) { UpdateText(); UpdateFont(); } /// /// Updates the font of the text element to a font compatible with the current language (based on the language font overrides of the EasyTalkGameState). /// private void UpdateFont() { DialogueDisplay parentDisplay = DialogueDisplay.GetParentDialogueDisplay(this.gameObject); if (parentDisplay != null && parentDisplay.DialogueSettings != null && parentDisplay.DialogueSettings.LanguageFontOverrides != null) { LanguageFontOverride fontOverride = null; if (languageFontOverrides != null) { fontOverride = languageFontOverrides.GetOverrideForLanguage(EasyTalkGameState.Instance.Language); } else { EasyTalkDialogueSettings dialogueSettings = parentDisplay.DialogueSettings; if (dialogueSettings != null) { LanguageFontOverrides fontOverrides = dialogueSettings.LanguageFontOverrides; if (fontOverrides != null) { languageFontOverrides = fontOverrides; fontOverride = languageFontOverrides.GetOverrideForLanguage(EasyTalkGameState.Instance.Language); } } } if (fontOverride != null) { #if TEXTMESHPRO_INSTALLED textMeshProText.font = fontOverride.tmpFont; #else text.font = fontOverride.font; #endif } } } /// /// Updates the text element to a translated version of the original text if a translation can be found for the current EasyTalkGameState language. /// private void UpdateText() { Translation translation = library.GetTranslation(originalText, EasyTalkGameState.Instance.Language); if (translation != null) { Text.text = translation.text; } } /// /// Sets the text value of the text component. The text passed to this method should be in the default/original language and will be translated automatically /// by this method if possible. /// /// The new text value. public void SetText(string text) { Text.text = text; originalText = text; UpdateText(); } #if TEXTMESHPRO_INSTALLED /// /// Gets the text component used by the AutoTranslate. /// public TMP_Text Text { get { return textMeshProText; } } #else public Text Text { get { return text; } } #endif } }