// This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the Text component attached to this GameObject
            var text = GetComponent <Text>();

            // Use translation?
            if (translation != null)
            {
                if (Args != null)
                {
                    text.text = string.Format(translation.Text, Args);
                }
                else
                {
                    text.text = translation.Text;
                }
            }
            // Use fallback?
            else
            {
                if (Args != null)
                {
                    text.text = string.Format(FallbackText, Args);
                }
                else
                {
                    text.text = FallbackText;
                }
            }
        }
Exemplo n.º 2
0
        private void DrawTranslation(LeanTranslation translation, bool unexpected)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(translation.Language, EditorStyles.boldLabel);
            if (GUILayout.Button("Remove", EditorStyles.miniButton, GUILayout.Width(55.0f)) == true)
            {
                Undo.RecordObject(Target, "Remove Translation");

                Target.RemoveTranslation(translation.Language);

                Dirty();
            }
            EditorGUILayout.EndHorizontal();

            if (unexpected == true)
            {
                EditorGUILayout.HelpBox("Your LeanLocalization component doesn't define the " + translation.Language + " language.", MessageType.Warning);
            }

            Undo.RecordObject(Target, "Modified Translation");

            EditorGUI.BeginChangeCheck();

            translation.Object = EditorGUILayout.ObjectField(translation.Object, typeof(Object), true);
            translation.Text   = EditorGUILayout.TextArea(translation.Text ?? "", GUILayout.MinHeight(40.0f));

            if (EditorGUI.EndChangeCheck() == true)
            {
                Dirty(); LeanLocalization.UpdateTranslations();
            }

            EditorGUILayout.Separator();
        }
Exemplo n.º 3
0
        private static void CalculateTranslation(LeanTranslation translation)
        {
            missing.Clear();
            clashes.Clear();

            foreach (var language in LeanLocalization.CurrentLanguages.Keys)
            {
                if (translation.Entries.Exists(e => e.Language == language) == false)
                {
                    missing.Add(language);
                }
            }

            foreach (var entry in translation.Entries)
            {
                var language = entry.Language;

                if (clashes.Contains(language) == false)
                {
                    if (translation.LanguageCount(language) > 1)
                    {
                        clashes.Add(language);
                    }
                }
            }
        }
        public void UpdateLocalization()
        {
            var dropdown = GetComponent <Dropdown>();
            var dOptions = dropdown.options;

            if (options != null)
            {
                for (var i = 0; i < options.Count; i++)
                {
                    var option  = options[i];
                    var dOption = default(Dropdown.OptionData);

                    if (dOptions.Count == i)
                    {
                        dOption = new Dropdown.OptionData();

                        dOptions.Add(dOption);
                    }
                    else
                    {
                        dOption = dOptions[i];
                    }

                    var stringTranslation = LeanLocalization.GetTranslation(option.StringTranslationName);

                    // Use translation?
                    if (stringTranslation != null && stringTranslation.Data is string)
                    {
                        dOption.text = LeanTranslation.FormatText((string)stringTranslation.Data, dOption.text, this);
                    }
                    // Use fallback?
                    else
                    {
                        dOption.text = LeanTranslation.FormatText(option.FallbackText, dOption.text, this);
                    }

                    var spriteTranslation = LeanLocalization.GetTranslation(option.StringTranslationName);

                    // Use translation?
                    if (spriteTranslation != null && spriteTranslation.Data is Sprite)
                    {
                        dOption.image = (Sprite)spriteTranslation.Data;
                    }
                    // Use fallback?
                    else
                    {
                        dOption.image = option.FallbackSprite;
                    }
                }
            }
            else
            {
                dOptions.Clear();
            }

            dropdown.options = dOptions;
        }
Exemplo n.º 5
0
        private void Register(string path, LeanTranslation translation)
        {
            if (LeanLocalization.CurrentTranslations.Remove(path) == true)
            {
                //Debug.LogWarning("You have multiple registered translations with the exact same path: " + path);
            }

            LeanLocalization.CurrentTranslations.Add(path, translation);
        }
        private void Compile(LeanTranslation translation, object data, bool primary)
        {
            translation.Data = data;

            if (primary == true)
            {
                translation.Primary = true;
            }
        }
        public static bool TryGetTranslation(string path, ref LeanTranslation translation)
        {
            if (path != null)
            {
                if (CurrentTranslations.TryGetValue(path, out translation) == true)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 8
0
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the TextMeshPro component attached to this GameObject
            var text = GetComponent <TextMeshPro>();

            // Use translation?
            if (translation != null && translation.Data is TMP_FontAsset)
            {
                text.font = (TMP_FontAsset)translation.Data;
            }
            // Use fallback?
            else
            {
                text.font = FallbackFont;
            }
        }
Exemplo n.º 9
0
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the Image component attached to this GameObject
            var image = GetComponent <Image>();

            // Use translation?
            if (translation != null)
            {
                image.sprite = translation.Object as Sprite;
            }
            // Use fallback?
            else
            {
                image.sprite = FallbackSprite;
            }
        }
Exemplo n.º 10
0
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the Text component attached to this GameObject
            var text = GetComponent <Text>();

            // Use translation?
            if (translation != null && translation.Object is Font)
            {
                text.font = (Font)translation.Object;
            }
            // Use fallback?
            else
            {
                text.font = FallbackFont;
            }
        }
Exemplo n.º 11
0
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the TextMeshProUGUI component attached to this GameObject
            var text = GetComponent <TextMeshProUGUI>();

            // Use translation?
            if (translation != null && translation.Data is string)
            {
                text.text = LeanTranslation.FormatText((string)translation.Data, text.text, this, gameObject);
            }
            // Use fallback?
            else
            {
                text.text = LeanTranslation.FormatText(FallbackText, text.text, this, gameObject);
            }
        }
Exemplo n.º 12
0
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the AudioSource component attached to this GameObject
            var audioSource = GetComponent <AudioSource>();

            // Use translation?
            if (translation != null && translation.Object is AudioClip)
            {
                audioSource.clip = (AudioClip)translation.Object;
            }
            // Use fallback?
            else
            {
                audioSource.clip = FallbackAudioClip;
            }
        }
Exemplo n.º 13
0
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the SpriteRenderer component attached to this GameObject
            var spriteRenderer = GetComponent <SpriteRenderer>();

            // Use translation?
            if (translation != null)
            {
                spriteRenderer.sprite = translation.Object as Sprite;
            }
            // Use fallback?
            else
            {
                spriteRenderer.sprite = FallbackSprite;
            }
        }
        /// <summary>This will return the translated string with the specified name, or the fallback if none is found.</summary>
        public static string GetTranslationText(string name, string fallback = null, bool replaceTokens = true)
        {
            var translation = default(LeanTranslation);

            if (string.IsNullOrEmpty(name) == false && CurrentTranslations.TryGetValue(name, out translation) == true && translation.Data is string)
            {
                fallback = (string)translation.Data;
            }

            if (replaceTokens == true)
            {
                fallback = LeanTranslation.FormatText(fallback);
            }

            return(fallback);
        }
Exemplo n.º 15
0
        // Add a new translation to this phrase, or return the current one
        public LeanTranslation AddTranslation(string language)
        {
            var translation = FindTranslation(language);

            // Add it?
            if (translation == null)
            {
                translation = new LeanTranslation();

                translation.Language = language;

                Translations.Add(translation);
            }

            return(translation);
        }
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the Text component attached to this GameObject
            var text = GetComponent <Text>();

            languageAnchors = GetComponents <LanguageAnchor>();
            // Use translation?
            if (translation != null && translation.Data is string)
            {
                if (LeanLocalization.CurrentLanguage.ToLower().Equals("arabic"))
                {
                    if (languageAnchors.Length > 0)
                    {
                        text.gameObject.transform.position =
                            languageAnchors[0].anchorName == AnchorName.ArabicAnchor ?
                            languageAnchors[0].AnchorObject.transform.position :
                            languageAnchors[1].AnchorObject.transform.position;
                        text.alignment = TextAnchor.UpperRight;
                    }
                    else
                    {
                        text.alignment = TextAnchor.MiddleCenter;
                    }
                }
                else if (LeanLocalization.CurrentLanguage.ToLower().Equals("english"))
                {
                    if (languageAnchors.Length > 0)
                    {
                        text.gameObject.transform.position =
                            languageAnchors[0].anchorName == AnchorName.EnglishAnchor ?
                            languageAnchors[0].AnchorObject.transform.position :
                            languageAnchors[1].AnchorObject.transform.position;
                        text.alignment = TextAnchor.UpperLeft;
                    }
                    else
                    {
                        text.alignment = TextAnchor.MiddleCenter;
                    }
                }
                text.text = LeanTranslation.FormatText((string)translation.Data, text.text, this);
            }
            // Use fallback?
            else
            {
                text.text = LeanTranslation.FormatText(FallbackText, text.text, this);
            }
        }
Exemplo n.º 17
0
        /// <summary>This will return the translation of this phrase for the specified language.</summary>
        public bool TryFindTranslation(string languageName, ref LeanTranslation translation)
        {
            if (translations != null)
            {
                for (var i = translations.Count - 1; i >= 0; i--)
                {
                    translation = translations[i];

                    if (translation.Language == languageName)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        private void Compile(LeanTranslation translation, Entry entry, bool primary)
        {
            switch (data)
            {
            case DataType.Text:
            {
                Compile(translation, entry.Text, primary);
            }
            break;

            case DataType.Object:
            case DataType.Sprite:
            {
                Compile(translation, entry.Object, primary);
            }
            break;
            }
        }
        // This gets called every time the translation needs updating
        public override void UpdateTranslation(LeanTranslation translation)
        {
            // Get the Renderer component attached to this GameObject
            var renderer = GetComponent <Renderer>();

            // Get the shared materials of this component
            var sharedMaterials = renderer.sharedMaterials;

            // Use translation?
            if (translation != null && translation.Object is Material)
            {
                sharedMaterials[Index] = (Material)translation.Object;
            }
            // Use fallback?
            else
            {
                sharedMaterials[Index] = FallbackMaterial;
            }

            renderer.sharedMaterials = sharedMaterials;
        }
Exemplo n.º 20
0
        /// <summary>When rebuilding translations this method is called from any <b>LeanSource</b> components that define a transition.</summary>
        public static LeanTranslation RegisterTranslation(string name)
        {
            var translation = default(LeanTranslation);

            if (string.IsNullOrEmpty(name) == false && CurrentTranslations.TryGetValue(name, out translation) == false)
            {
                if (tempTranslations.TryGetValue(name, out translation) == true)
                {
                    tempTranslations.Remove(name);

                    CurrentTranslations.Add(name, translation);
                }
                else
                {
                    translation = new LeanTranslation(name);

                    CurrentTranslations.Add(name, translation);
                }
            }

            return(translation);
        }
        private void DrawTranslation(LeanTranslation translation)
        {
            BeginModifications();
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("Text", LabelStyle, GUILayout.Width(50.0f));

                    translation.Text = EditorGUILayout.TextArea(translation.Text);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("Object", LabelStyle, GUILayout.Width(50.0f));

                    translation.Object = EditorGUILayout.ObjectField(translation.Object, typeof(Object), true);
                }
                EditorGUILayout.EndHorizontal();
            }
            EndModifications();
        }
Exemplo n.º 22
0
        /// <summary>Add a new translation to this phrase for the specified language, or return the current one.</summary>
        public LeanTranslation AddTranslation(string languageName, string text = null, Object obj = null)
        {
            var translation = default(LeanTranslation);

            if (TryFindTranslation(languageName, ref translation) == false)
            {
                translation = new LeanTranslation();

                translation.Language = languageName;

                if (translations == null)
                {
                    translations = new List <LeanTranslation>();
                }

                translations.Add(translation);
            }

            translation.Text   = text;
            translation.Object = obj;

            return(translation);
        }
Exemplo n.º 23
0
        private void DrawTranslations()
        {
            var rectA = Reserve();
            var rectB = rectA; rectB.xMin += EditorGUIUtility.labelWidth; rectB.xMax -= 37.0f;
            var rectC = rectA; rectC.xMin = rectC.xMax - 35.0f;

            EditorGUI.LabelField(rectA, "Translations", EditorStyles.boldLabel);
            translationFilter = EditorGUI.TextField(rectB, "", translationFilter);
            EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(translationFilter) == true || LeanLocalization.CurrentTranslations.ContainsKey(translationFilter) == true);
            if (GUI.Button(rectC, "Add", EditorStyles.miniButton) == true)
            {
                var phrase = LeanLocalization.AddPhraseToFirst(translationFilter);

                LeanLocalization.UpdateTranslations();

                Selection.activeObject = phrase;

                EditorGUIUtility.PingObject(phrase);
            }
            EditorGUI.EndDisabledGroup();

            if (LeanLocalization.CurrentTranslations.Count == 0 && string.IsNullOrEmpty(translationFilter) == true)
            {
                EditorGUILayout.HelpBox("Type in the name of a translation, and click the 'Add' button. Or, drag and drop a prefab that contains some.", MessageType.Info);
            }
            else
            {
                var total = 0;

                EditorGUI.indentLevel++;
                foreach (var pair in LeanLocalization.CurrentTranslations)
                {
                    var name = pair.Key;

                    if (string.IsNullOrEmpty(translationFilter) == true || name.IndexOf(translationFilter, System.StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        var translation = pair.Value;
                        var rectT       = Reserve();
                        var expand      = EditorGUI.Foldout(new Rect(rectT.x, rectT.y, 20, rectT.height), expandTranslation == translation, "");

                        if (expand == true)
                        {
                            expandTranslation = translation;
                        }
                        else if (expandTranslation == translation)
                        {
                            expandTranslation = null;
                        }

                        CalculateTranslation(pair.Value);

                        var data = translation.Data;

                        total++;

                        EditorGUI.BeginDisabledGroup(true);
                        BeginError(missing.Count > 0 || clashes.Count > 0);
                        if (data is Object)
                        {
                            EditorGUI.ObjectField(rectT, name, (Object)data, typeof(Object), true);
                        }
                        else
                        {
                            EditorGUI.TextField(rectT, name, data != null ? data.ToString() : "");
                        }
                        EndError();

                        if (expand == true)
                        {
                            EditorGUI.indentLevel++;
                            foreach (var entry in translation.Entries)
                            {
                                BeginError(clashes.Contains(entry.Language) == true);
                                EditorGUILayout.ObjectField(entry.Language, entry.Owner, typeof(Object), true);
                                EndError();
                            }
                            EditorGUI.indentLevel--;
                        }
                        EditorGUI.EndDisabledGroup();

                        if (expand == true)
                        {
                            foreach (var language in missing)
                            {
                                EditorGUILayout.HelpBox("This translation isn't defined for the " + language + " language.", MessageType.Warning);
                            }

                            foreach (var language in clashes)
                            {
                                EditorGUILayout.HelpBox("This translation is defined multiple times for the " + language + " language.", MessageType.Warning);
                            }
                        }
                    }
                }
                EditorGUI.indentLevel--;

                if (total == 0)
                {
                    EditorGUILayout.HelpBox("No translation with this name exists, click the 'Add' button to create it.", MessageType.Info);
                }
            }
        }
        private void DrawTranslations(LeanLocalization localization, LeanPhrase phrase)
        {
            existingLanguages.Clear();

            for (var i = 0; i < phrase.Translations.Count; i++)
            {
                var labelA      = Reserve();
                var valueA      = Reserve(ref labelA, 20.0f);
                var translation = phrase.Translations[i];

                if (translationIndex == i)
                {
                    BeginModifications();
                    {
                        translation.Language = EditorGUI.TextField(labelA, "", translation.Language);
                    }
                    EndModifications();

                    if (GUI.Button(valueA, "X") == true)
                    {
                        MarkAsModified();

                        phrase.Translations.RemoveAt(i); translationIndex = -1;
                    }
                }

                if (EditorGUI.Foldout(labelA, translationIndex == i, translationIndex == i ? "" : translation.Language) == true)
                {
                    translationIndex = i;

                    EditorGUI.indentLevel += 1;
                    {
                        DrawTranslation(translation);
                    }
                    EditorGUI.indentLevel -= 1;

                    EditorGUILayout.Separator();
                }
                else if (translationIndex == i)
                {
                    translationIndex = -1;
                }

                if (existingLanguages.Contains(translation.Language) == true)
                {
                    EditorGUILayout.HelpBox("This phrase has already been translated to this language!", MessageType.Warning);
                }
                else
                {
                    existingLanguages.Add(translation.Language);
                }

                if (localization.Languages.Contains(translation.Language) == false)
                {
                    EditorGUILayout.HelpBox("This translation uses a language that hasn't been set in the localization!", MessageType.Warning);
                }
            }

            for (var i = 0; i < localization.Languages.Count; i++)
            {
                var language = localization.Languages[i];

                if (phrase.Translations.Exists(t => t.Language == language) == false)
                {
                    var labelA = Reserve();
                    var valueA = Reserve(ref labelA, 120.0f);

                    EditorGUI.LabelField(labelA, language);

                    if (GUI.Button(valueA, "Create Translation") == true)
                    {
                        MarkAsModified();

                        var newTranslation = new LeanTranslation();

                        newTranslation.Language = language;
                        newTranslation.Text     = phrase.Name;

                        translationIndex = phrase.Translations.Count;

                        phrase.Translations.Add(newTranslation);
                    }
                }
            }
        }
Exemplo n.º 25
0
 // This gets called every time the translation needs updating
 // NOTE: translation may be null if it can't be found
 public abstract void UpdateTranslation(LeanTranslation translation);