using System; using UnityEngine; using UnityEngine.Serialization; namespace Unity.Tutorials.Core.Editor { /// /// A serializable string that is localized at run-time. /// [Serializable] public class LocalizableString : ISerializationCallbackReceiver { internal const string PropertyPath = "m_Untranslated"; internal const string OldPropertyPath = "k__BackingField"; /// /// Setting Untranslated string overwrites Translated so make sure to translate again. /// public string Untranslated { get => m_Untranslated; set => Translated = m_Untranslated = value; } [SerializeField, FormerlySerializedAs(OldPropertyPath)] string m_Untranslated; /// /// The localized strings, if it exists. /// public string Translated { get; set; } /// /// The translated string, if exists, untranslated otherwise. /// public string Value => Translated.AsNullIfEmpty() ?? Untranslated; /// /// Default-constructs with empty strings. /// public LocalizableString() : this(string.Empty) {} /// /// Constructs with an untranslated string. /// /// public LocalizableString(string untranslated) { Untranslated = untranslated; } /// /// Implicitly constructs from an untranslated string. /// /// /// public static implicit operator LocalizableString(string untranslated) => new LocalizableString(untranslated); /// /// Implicit conversion to string returns the Value. /// /// /// public static implicit operator string(LocalizableString str) => str.Value; /// /// UnityEngine.ISerializationCallbackReceiver override, do not call. /// public void OnBeforeSerialize() {} /// /// UnityEngine.ISerializationCallbackReceiver override, do not call. /// public void OnAfterDeserialize() { // Replicate the Untranslated setter behaviour upon deserialization. Translated = Untranslated; } } /// /// Same as TextAreaAttribute but used for LocalizableStrings. /// [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)] public class LocalizableTextAreaAttribute : PropertyAttribute { /// /// Minimum number of lines shown in the Inspector. /// public readonly int MinLines; /// /// Maximum number of lines shown in the Inspector. /// public readonly int MaxLines; /// /// Default-constructs with default (3) number lines. /// public LocalizableTextAreaAttribute() { MinLines = 3; MaxLines = 3; } /// /// Constructs with desired number of lines. /// /// /// public LocalizableTextAreaAttribute(int minLines, int maxLines) { MinLines = minLines; MaxLines = maxLines; } } }