Example #1
0
        private void DrawEntry(TARGET tgt, LeanPhrase.Entry entry, bool unexpected)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(entry.Language, EditorStyles.boldLabel);
            if (GUILayout.Button("Modify", EditorStyles.miniButton, GUILayout.Width(65.0f)) == true)
            {
                var menu = new GenericMenu();

                foreach (var otherEntry in tgt.Entries)
                {
                    var title = new GUIContent("Auto Translate/From " + otherEntry.Language);

                    if (entry != otherEntry && string.IsNullOrEmpty(otherEntry.Text) == false)
                    {
                        var textInput      = otherEntry.Text;
                        var languageInput  = default(LeanLanguage);
                        var languageOutput = default(LeanLanguage);

                        if (LeanLocalization.CurrentLanguages.TryGetValue(otherEntry.Language, out languageInput) == true && LeanLocalization.CurrentLanguages.TryGetValue(entry.Language, out languageOutput) == true)
                        {
                            var languageCodeInput  = languageInput.TranslationCode;
                            var languageCodeOutput = languageOutput.TranslationCode;

                            menu.AddItem(title, false, () =>
                            {
                                var textOutput = default(string);

                                if (TryAutoTranslate(languageCodeInput, languageCodeOutput, textInput, ref textOutput) == true)
                                {
                                    Undo.RecordObject(tgt, "Auto Translate");

                                    entry.Text = textOutput;

                                    EditorUtility.SetDirty(tgt);
                                }
                                else
                                {
                                    Debug.LogError("Failed to auto translate text for some reason.");
                                }
                            });
                        }
                        else
                        {
                            menu.AddDisabledItem(title, false);
                        }
                    }
                    else
                    {
                        menu.AddDisabledItem(title, false);
                    }
                }

                menu.AddSeparator("");

                menu.AddItem(new GUIContent("Remove"), false, () =>
                {
                    Undo.RecordObject(tgt, "Remove Translation");

                    tgt.RemoveTranslation(entry.Language);

                    DirtyAndUpdate();
                });

                menu.ShowAsContext();
            }
            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)
            {
                DirtyAndUpdate(); LeanLocalization.UpdateTranslations();
            }

            Separator();
        }