private void DrawEntry(LeanPhrase.Entry entry, bool unexpected)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(entry.Language, EditorStyles.boldLabel);
            if (GUILayout.Button("Remove", EditorStyles.miniButton, GUILayout.Width(55.0f)) == true)
            {
                Undo.RecordObject(Target, "Remove Translation");

                Target.RemoveTranslation(entry.Language);

                Dirty();
            }
            EditorGUILayout.EndHorizontal();

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

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

            EditorGUI.BeginChangeCheck();

            switch (Target.Data)
            {
            case LeanPhrase.DataType.Text:
                if (entry.Language.ToLower().Equals("inversedarabic"))
                {
                    if (!string.IsNullOrEmpty(entry.Text))
                    {
                        fixedArabic = ArabicFixer.Fix(entry.Text, false, false);
                    }
                }

                if (entry.Language.ToLower().Equals("arabic"))
                {
                    if (!string.IsNullOrEmpty(fixedArabic))
                    {
                        entry.Text = fixedArabic;
                    }
                }
                entry.Text = EditorGUILayout.TextArea(entry.Text ?? "", GUILayout.MinHeight(40.0f));
                break;

            case LeanPhrase.DataType.Object:
                entry.Object = EditorGUILayout.ObjectField(entry.Object, typeof(Object), true);
                break;

            case LeanPhrase.DataType.Sprite:
                entry.Object = EditorGUILayout.ObjectField(entry.Object, typeof(Sprite), true);
                break;
            }

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

            EditorGUILayout.Separator();
        }
        public override void Compile(string primaryLanguage, string secondaryLanguage)
        {
            if (entries == null || entries.Count == 0)
            {
                if (Application.isPlaying == true)
                {
                    LoadFromSource();
                }
            }

            if (entries != null)
            {
                for (var i = entries.Count - 1; i >= 0; i--)
                {
                    var entry       = entries[i];
                    var translation = LeanLocalization.RegisterTranslation(entry.Name);

                    translation.Register(Language, this);

                    if (Language == primaryLanguage)
                    {
                        translation.Data    = entry.Text;
                        translation.Primary = true;
                    }
                    else if (Language == secondaryLanguage && translation.Primary == false)
                    {
                        translation.Data = entry.Text;
                    }
                }
            }
        }
        // Reverse lookup the phrases for this language
        private void DrawReverse(LeanLocalization localization, string language)
        {
            for (var i = 0; i < localization.Phrases.Count; i++)
            {
                var phrase      = localization.Phrases[i];
                var labelA      = Reserve();
                var translation = phrase.Translations.Find(t => t.Language == language);

                BeginModifications();
                {
                    EditorGUI.LabelField(labelA, phrase.Name);
                }
                EndModifications();

                if (translation != null)
                {
                    if (reverseIndex == i)
                    {
                        BeginModifications();
                        {
                            phrase.Name = EditorGUI.TextField(labelA, "", phrase.Name);
                        }
                        EndModifications();
                    }

                    if (EditorGUI.Foldout(labelA, reverseIndex == i, reverseIndex == i ? "" : phrase.Name) == true)
                    {
                        reverseIndex = i;

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

                        EditorGUILayout.Separator();
                    }
                    else if (reverseIndex == i)
                    {
                        reverseIndex = -1;
                    }
                }
                else
                {
                    var valueA = Reserve(ref labelA, 120.0f);

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

                        var newTranslation = new LeanTranslation();

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

                        phrase.Translations.Add(newTranslation);
                    }
                }
            }
        }
Example #4
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();
        }
Example #5
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var left  = position; left.xMax -= 40;
            var right = position; right.xMin = left.xMax + 2;
            var color = GUI.color;

            if (LeanLocalization.PhraseExists(property.stringValue) == false)
            {
                GUI.color = Color.red;
            }

            EditorGUI.PropertyField(left, property);

            GUI.color = color;

            if (GUI.Button(right, "List") == true)
            {
                var menu = new GenericMenu();

                foreach (var phraseName in LeanLocalization.CurrentPhrases.Keys)
                {
                    menu.AddItem(new GUIContent(phraseName), property.stringValue == phraseName, () => { property.stringValue = phraseName; property.serializedObject.ApplyModifiedProperties(); });
                }

                if (menu.GetItemCount() > 0)
                {
                    menu.DropDown(right);
                }
                else
                {
                    Debug.LogWarning("Your scene doesn't contain any phrases, so the phrase name list couldn't be created.");
                }
            }
        }
Example #6
0
        public void LoadFromSource()
        {
            if (Source != null && string.IsNullOrEmpty(Language) == false)
            {
                for (var i = Entries.Count - 1; i >= 0; i--)                 // NOTE: Property
                {
                    entryPool.Push(entries[i]);
                }

                entries.Clear();

                // Split file into lines, and loop through them all
                var lines = Source.text.Split(newlineCharacters, System.StringSplitOptions.RemoveEmptyEntries);

                for (var i = 0; i < lines.Length; i++)
                {
                    var line        = lines[i];
                    var equalsIndex = line.IndexOf(Separator);

                    // Only consider lines with the Separator character
                    if (equalsIndex != -1)
                    {
                        var name = line.Substring(0, equalsIndex).Trim();
                        var text = line.Substring(equalsIndex + Separator.Length).Trim();

                        // Does this entry have a comment?
                        if (string.IsNullOrEmpty(Comment) == false)
                        {
                            var commentIndex = text.LastIndexOf(Comment);

                            if (commentIndex != -1)
                            {
                                text = text.Substring(0, commentIndex).Trim();
                            }
                        }

                        // Replace newline markers with actual newlines
                        if (string.IsNullOrEmpty(NewLine) == false)
                        {
                            text = text.Replace(NewLine, System.Environment.NewLine);
                        }

                        var entry = entryPool.Count > 0 ? entryPool.Pop() : new Entry();

                        entry.Name = name;
                        entry.Text = text;

                        entries.Add(entry);
                    }
                }

                // Update translations?
                if (LeanLocalization.CurrentLanguage == Language)
                {
                    LeanLocalization.UpdateTranslations();
                }
            }
        }
        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;
        }
Example #8
0
        public void Register()
        {
            if (node == null)
            {
                node = Instances.AddLast(this);

                LeanLocalization.DelayUpdateTranslations();
            }
        }
Example #9
0
        public void Unregister()
        {
            if (node != null)
            {
                Instances.Remove(node);

                node = null;

                LeanLocalization.DelayUpdateTranslations();
            }
        }
Example #10
0
        public void Clear()
        {
            if (entries != null)
            {
                entries.Clear();

                // Update translations?
                if (LeanLocalization.CurrentLanguage == Language)
                {
                    LeanLocalization.UpdateTranslations();
                }
            }
        }
Example #11
0
        public void LoadFromSource()
        {
            if (Source != null && string.IsNullOrEmpty(Language) == false)
            {
                // Split file into lines, and loop through them all
                var lines = Source.text.Split(newlineCharacters, System.StringSplitOptions.RemoveEmptyEntries);

                for (var i = 0; i < lines.Length; i++)
                {
                    var line        = lines[i];
                    var equalsIndex = line.IndexOf(Separator);

                    // Only consider lines with the Separator character
                    if (equalsIndex != -1)
                    {
                        var title = line.Substring(0, equalsIndex).Trim();
                        var text  = line.Substring(equalsIndex + Separator.Length).Trim();

                        // Does this entry have a comment?
                        if (string.IsNullOrEmpty(Comment) == false)
                        {
                            var commentIndex = text.LastIndexOf(Comment);

                            if (commentIndex != -1)
                            {
                                text = text.Substring(0, commentIndex).Trim();
                            }
                        }

                        // Replace newline markers with actual newlines
                        if (string.IsNullOrEmpty(NewLine) == false)
                        {
                            text = text.Replace(NewLine, System.Environment.NewLine);
                        }

                        // Find or add the translation for this phrase
                        LeanLocalization.AddTranslationToFirst(title, Language, text);
                    }
                }

                // Update translations?
                if (LeanLocalization.CurrentLanguage == Language)
                {
                    LeanLocalization.UpdateTranslations();
                }
            }
        }
Example #12
0
        private void DrawEntry(LeanPhrase.Entry entry, bool unexpected)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(entry.Language, EditorStyles.boldLabel);
            if (GUILayout.Button("Remove", EditorStyles.miniButton, GUILayout.Width(55.0f)) == true)
            {
                Undo.RecordObject(tgt, "Remove Translation");

                tgt.RemoveTranslation(entry.Language);

                Dirty();
            }
            EditorGUILayout.EndHorizontal();

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

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

            EditorGUI.BeginChangeCheck();

            switch (tgt.Data)
            {
            case LeanPhrase.DataType.Text:
                entry.Text = EditorGUILayout.TextArea(entry.Text ?? "", GUILayout.MinHeight(40.0f));
                break;

            case LeanPhrase.DataType.Object:
                entry.Object = EditorGUILayout.ObjectField(entry.Object, typeof(Object), true);
                break;

            case LeanPhrase.DataType.Sprite:
                entry.Object = EditorGUILayout.ObjectField(entry.Object, typeof(Sprite), true);
                break;
            }

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

            EditorGUILayout.Separator();
        }
        public void Clear()
        {
            if (entries != null)
            {
                DoClear();

                // Update translations?
                foreach (var localization in LeanLocalization.Instances)
                {
                    if (localization.CurrentLanguage == Language)
                    {
                        LeanLocalization.UpdateTranslations();

                        break;
                    }
                }
            }
        }
        public void LoadFromSource()
        {
            if (Source != null && string.IsNullOrEmpty(Language) == false)
            {
                DoLoadFromSource();

                // Update translations?
                foreach (var localization in LeanLocalization.Instances)
                {
                    if (localization.CurrentLanguage == Language)
                    {
                        LeanLocalization.UpdateTranslations();

                        break;
                    }
                }
            }
        }
Example #15
0
        public void LoadFromSource()
        {
            if (Source != null && string.IsNullOrEmpty(SourceLanguage) == false)
            {
                var localization = GetComponent <LeanLocalization>();
                var lines        = Source.text.Split(newlineCharacters, System.StringSplitOptions.RemoveEmptyEntries);

                foreach (var line in lines)
                {
                    var equalsIndex = line.IndexOf('=');

                    if (equalsIndex != -1)
                    {
                        var phraseName        = line.Substring(0, equalsIndex).Trim();
                        var phraseTranslation = line.Substring(equalsIndex + 1).Trim();

                        // Replace line markers with actual newlines
                        phraseTranslation = phraseTranslation.Replace(newlineString, System.Environment.NewLine);

                        // Does this entry have a comment?
                        var commentIndex = phraseTranslation.IndexOf("//");

                        if (commentIndex != -1)
                        {
                            phraseTranslation = phraseTranslation.Substring(0, commentIndex).Trim();
                        }

                        // Find or add the translation for this phrase
                        var translation = localization.AddTranslation(SourceLanguage, phraseName);

                        // Set the translation text for this phrase
                        translation.Text = phraseTranslation;
                    }
                }

                // Update translations?
                if (LeanLocalization.CurrentLanguage == SourceLanguage)
                {
                    LeanLocalization.UpdateTranslations();
                }
            }
        }
        // Draw the whole inspector
        public override void OnInspectorGUI()
        {
            var localization = (LeanLocalization)target;

            Validate(localization);

            EditorGUILayout.Separator();

            DrawCurrentLanguage();

            EditorGUILayout.Separator();

            EditorGUILayout.PropertyField(serializedObject.FindProperty("DefaultLanguage"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("DetectLanguage"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("SaveLanguage"));

            EditorGUILayout.Separator();

            DrawLanguages(localization);

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            DrawCultures(localization);

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            DrawPhrases(localization);

            EditorGUILayout.Separator();

            // Update if dirty?
            if (serializedObject.ApplyModifiedProperties() == true || dirty == true)
            {
                dirty = false;

                LeanLocalization.UpdateTranslations();

                SetDirty(localization);
            }
        }
        private static void Validate(LeanLocalization localization)
        {
            if (localization.Languages == null)
            {
                localization.Languages = new List <string>();
            }
            if (localization.Cultures == null)
            {
                localization.Cultures = new List <LeanCulture>();
            }
            if (localization.Phrases == null)
            {
                localization.Phrases = new List <LeanPhrase>();
            }

            for (var i = localization.Cultures.Count - 1; i >= 0; i--)
            {
                var culture = localization.Cultures[i];

                if (culture == null)
                {
                    culture = new LeanCulture();

                    culture.Language = "New Language";
                    culture.Alias    = "new-Alias";

                    localization.Cultures[i] = culture;
                }
            }

            for (var i = localization.Phrases.Count - 1; i >= 0; i--)
            {
                var phrase = localization.Phrases[i];

                if (phrase.Translations == null)
                {
                    phrase.Translations = new List <LeanTranslation>();
                }
            }
        }
        public override void Compile(string primaryLanguage, string secondaryLanguage)
        {
            var translation = LeanLocalization.RegisterTranslation(name);

            if (entries != null)
            {
                for (var i = entries.Count - 1; i >= 0; i--)
                {
                    var entry = entries[i];

                    translation.Register(entry.Language, this);

                    if (entry.Language == primaryLanguage)
                    {
                        Compile(translation, entry, true);
                    }
                    else if (entry.Language == secondaryLanguage && translation.Primary == false)
                    {
                        Compile(translation, entry, false);
                    }
                }
            }
        }
        private void DrawCulture(LeanLocalization localization, LeanCulture culture, bool full)
        {
            var labelA = Reserve();
            var valueA = Reserve(ref labelA, 20.0f);

            BeginModifications();
            {
                //culture.Language = EditorGUI.TextField(labelA, "", culture.Name);
                culture.Alias = EditorGUI.TextField(labelA, "", culture.Alias);

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

                    localization.Cultures.Remove(culture);

                    if (localization.Cultures.Exists(c => c.Language == culture.Language) == false)
                    {
                        cultureIndex = 0;
                    }
                }
            }
            EndModifications();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var left   = position; left.xMax -= 40;
            var right  = position; right.xMin = left.xMax + 2;
            var color  = GUI.color;
            var exists = LeanLocalization.CurrentTranslations.ContainsKey(property.stringValue);

            if (exists == false)
            {
                GUI.color = Color.red;
            }

            EditorGUI.PropertyField(left, property);

            GUI.color = color;

            if (GUI.Button(right, "List") == true)
            {
                var menu = new GenericMenu();

                if (string.IsNullOrEmpty(property.stringValue) == false)
                {
                    if (exists == true)
                    {
                        var translation = default(LeanTranslation);

                        if (LeanLocalization.CurrentTranslations.TryGetValue(property.stringValue, out translation) == true)
                        {
                            foreach (var entry in translation.Entries)
                            {
                                var owner = entry.Owner; menu.AddItem(new GUIContent("Select/" + entry.Language), false, () => { Selection.activeObject = owner; EditorGUIUtility.PingObject(owner); });
                            }
                        }
                    }
                    else
                    {
                        menu.AddItem(new GUIContent("Add: " + property.stringValue.Replace('/', '\\')), false, () => { var phrase = LeanLocalization.AddPhraseToFirst(property.stringValue); LeanLocalization.UpdateTranslations(); Selection.activeObject = phrase; EditorGUIUtility.PingObject(phrase); });
                    }

                    menu.AddItem(GUIContent.none, false, null);
                }

                foreach (var translationName in LeanLocalization.CurrentTranslations.Keys)
                {
                    menu.AddItem(new GUIContent(translationName), property.stringValue == translationName, () => { property.stringValue = translationName; property.serializedObject.ApplyModifiedProperties(); });
                }

                if (menu.GetItemCount() > 0)
                {
                    menu.DropDown(right);
                }
                else
                {
                    Debug.LogWarning("Your scene doesn't contain any phrases, so the phrase name list couldn't be created.");
                }
            }
        }
 public override void Register()
 {
     LeanLocalization.RegisterToken(name, this);
 }
Example #22
0
        public void LoadFromSource()
        {
            if (Source != null)
            {
                var target = Target;

                // Automatically get first LeanLocalization?
                if (target == null && LeanLocalization.AllLocalizations.Count > 0)
                {
                    target = LeanLocalization.AllLocalizations[0];
                }

                // Create a new LeanLocalization?
                if (target == null)
                {
                    target = new GameObject("LeanLocalization").AddComponent <LeanLocalization>();
                }

                // Split file by line and loop through them all
                var lines          = Source.text.Split(newlineCharacters, System.StringSplitOptions.RemoveEmptyEntries);
                var updateLanguage = false;

                for (var i = 0; i < lines.Length; i++)
                {
                    var line        = lines[i];
                    var equalsIndex = line.IndexOf(Separator);

                    if (equalsIndex != -1)
                    {
                        var lineLeft   = line.Substring(0, equalsIndex).Trim();
                        var colonIndex = lineLeft.IndexOf(Language);

                        if (colonIndex != -1)
                        {
                            var phraseName        = lineLeft.Substring(0, colonIndex).Trim();
                            var languageName      = lineLeft.Substring(colonIndex + Language.Length).Trim();
                            var phraseTranslation = line.Substring(equalsIndex + Separator.Length).Trim();

                            // Replace newline markers with actual newlines
                            if (string.IsNullOrEmpty(NewLine) == false)
                            {
                                phraseTranslation = phraseTranslation.Replace(NewLine, System.Environment.NewLine);
                            }

                            // Does this entry have a comment?
                            if (string.IsNullOrEmpty(Comment) == false)
                            {
                                var commentIndex = phraseTranslation.LastIndexOf(Comment);

                                if (commentIndex != -1)
                                {
                                    phraseTranslation = phraseTranslation.Substring(0, commentIndex).Trim();
                                }
                            }

                            // Find or add the translation for this phrase
                            var translation = target.AddTranslation(languageName, phraseName);

                            // Set the translation text for this phrase
                            translation.Text = phraseTranslation;

                            // Update translations later?
                            if (LeanLocalization.CurrentLanguage == languageName)
                            {
                                updateLanguage = true;
                            }
                        }
                    }
                }

                // Update translations?
                if (updateLanguage == true)
                {
                    LeanLocalization.UpdateTranslations();
                }
            }
        }
Example #23
0
 public override void Compile(string primaryLanguage, string secondaryLanguage)
 {
     LeanLocalization.RegisterToken(name, this);
 }
        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);
                    }
                }
            }
        }
        private void DrawPhrases(LeanLocalization localization)
        {
            var labelA = Reserve();
            var valueA = Reserve(ref labelA, 35.0f);

            EditorGUI.LabelField(labelA, "Phrases", EditorStyles.boldLabel);

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

                var newPhrase = localization.AddPhrase("New Phrase");

                phraseIndex = localization.Phrases.IndexOf(newPhrase);
            }

            existingPhrases.Clear();

            for (var i = 0; i < localization.Phrases.Count; i++)
            {
                var phrase = localization.Phrases[i];
                var labelB = Reserve();
                var valueB = Reserve(ref labelB, 20.0f);

                if (phraseIndex == i)
                {
                    BeginModifications();
                    {
                        phrase.Name = EditorGUI.TextField(labelB, "", phrase.Name);
                    }
                    EndModifications();

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

                        localization.Phrases.RemoveAt(i); phraseIndex = -1;
                    }
                }

                if (EditorGUI.Foldout(labelB, phraseIndex == i, phraseIndex == i ? "" : phrase.Name) == true)
                {
                    if (phraseIndex != i)
                    {
                        phraseIndex      = i;
                        translationIndex = -1;
                    }

                    EditorGUI.indentLevel += 1;
                    {
                        DrawTranslations(localization, phrase);
                    }
                    EditorGUI.indentLevel -= 1;

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

                if (existingPhrases.Contains(phrase.Name) == true)
                {
                    EditorGUILayout.HelpBox("This phrase already exists in the list!", MessageType.Warning);
                }
                else
                {
                    existingPhrases.Add(phrase.Name);
                }
            }
        }
        private void DrawCultures(LeanLocalization localization)
        {
            var labelA = Reserve();
            var valueA = Reserve(ref labelA, 35.0f);

            EditorGUI.LabelField(labelA, "Cultures", EditorStyles.boldLabel);

            // Add a new culture?
            if (localization.Languages.Count > 0)
            {
                if (GUI.Button(valueA, "Add") == true)
                {
                    MarkAsModified();

                    var menu = new GenericMenu();

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

                        menu.AddItem(new GUIContent(language), false, () => { var culture = localization.AddCulture(language, ""); cultureIndex = localization.Cultures.IndexOf(culture); });
                    }

                    menu.DropDown(valueA);
                }
            }

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

                if (localization.Cultures.Exists(c => c.Language == language) == true)
                {
                    var labelB = Reserve();

                    if (EditorGUI.Foldout(labelB, cultureIndex == i, language) == true)
                    {
                        if (cultureIndex != i)
                        {
                            cultureIndex = i;
                        }
                        EditorGUI.indentLevel += 1;
                        {
                            for (var j = 0; j < localization.Cultures.Count; j++)
                            {
                                var culture = localization.Cultures[j];

                                if (culture.Language == language)
                                {
                                    DrawCulture(localization, culture, false);
                                }
                            }
                        }
                        EditorGUI.indentLevel -= 1;
                    }
                    else if (cultureIndex == i)
                    {
                        cultureIndex = -1;
                    }
                }
            }

            for (var i = 0; i < localization.Cultures.Count; i++)
            {
                var culture         = localization.Cultures[i];
                var cultureLanguage = culture.Language;

                if (localization.Languages.Contains(cultureLanguage) == false)
                {
                    DrawCulture(localization, culture, true);
                }
            }
        }
Example #27
0
 protected virtual void OnDisable()
 {
     LeanLocalization.DelayUpdateTranslations();
 }
Example #28
0
 // Call this to force the behaviour to get updated
 public void UpdateLocalization()
 {
     UpdateTranslation(LeanLocalization.GetTranslation(phraseName));
 }
        public override void Register(string primaryLanguage, string defaultLanguage)
        {
            // Lazy load only?
            switch (Cache)
            {
            case CacheType.LazyLoad:
            {
                if (Language != primaryLanguage && Language != defaultLanguage)
                {
                    return;
                }
            }
            break;

            case CacheType.LazyLoadAndUnload:
            {
                if (Language != primaryLanguage && Language != defaultLanguage)
                {
                    DoClear();

                    return;
                }
            }
            break;

            case CacheType.LazyLoadAndUnloadPrimaryOnly:
            {
                if (Language != primaryLanguage)
                {
                    DoClear();

                    return;
                }
            }
            break;
            }

            if (entries == null || entries.Count == 0)
            {
                if (Application.isPlaying == true)
                {
                    DoLoadFromSource();
                }
            }

            if (entries != null)
            {
                for (var i = entries.Count - 1; i >= 0; i--)
                {
                    var entry       = entries[i];
                    var translation = LeanLocalization.RegisterTranslation(entry.Name);

                    translation.Register(Language, this);

                    if (Language == primaryLanguage)
                    {
                        translation.Data    = entry.Text;
                        translation.Primary = true;
                    }
                    else if (Language == defaultLanguage && translation.Primary == false)
                    {
                        translation.Data = entry.Text;
                    }
                }
            }
        }
        private void DrawLanguages(LeanLocalization localization)
        {
            var labelA = Reserve();
            var valueA = Reserve(ref labelA, 35.0f);

            EditorGUI.LabelField(labelA, "Languages", EditorStyles.boldLabel);

            // Add a new language?
            if (GUI.Button(valueA, "Add") == true)
            {
                MarkAsModified();

                var menu = new GenericMenu();

                for (var i = 0; i < presetLanguages.Count; i++)
                {
                    var presetLanguage = presetLanguages[i];

                    menu.AddItem(new GUIContent(presetLanguage.Name), false, () => localization.AddLanguage(presetLanguage.Name, presetLanguage.CultureName));
                }

                menu.DropDown(valueA);
            }

            existingLanguages.Clear();

            // Draw all added languages
            for (var i = 0; i < localization.Languages.Count; i++)
            {
                var language = localization.Languages[i];
                var labelB   = Reserve();
                var valueB   = Reserve(ref labelB, 20.0f);

                // Edit language name or remove
                if (languageIndex == i)
                {
                    BeginModifications();
                    {
                        localization.Languages[i] = EditorGUI.TextField(labelB, language);
                    }
                    EndModifications();

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

                        localization.Languages.RemoveAt(i); languageIndex = -1;
                    }
                }

                // Expand language?
                if (EditorGUI.Foldout(labelB, languageIndex == i, languageIndex == i ? "" : language) == true)
                {
                    if (languageIndex != i)
                    {
                        languageIndex = i;
                        reverseIndex  = -1;
                    }

                    EditorGUI.indentLevel += 1;
                    {
                        DrawReverse(localization, language);
                    }
                    EditorGUI.indentLevel -= 1;

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

                // Already added?
                if (existingLanguages.Contains(language) == true)
                {
                    EditorGUILayout.HelpBox("This language already exists in the list!", MessageType.Warning);
                }
                else
                {
                    existingLanguages.Add(language);
                }
            }
        }