Beispiel #1
0
        AutoTranslation autoTranslation = null;        //currently auto-translating something when this isnt null

        string defaultContents(TranslatedElement elem)
        {
            if (elem.GetComponent <Text>())
            {
                return(elem.GetComponent <Text>().text);
            }
            else if (elem.GetComponent <TextMesh>())
            {
                return(elem.GetComponent <TextMesh>().text);
            }
            else
            {
                return(elem.name);
            }
        }
Beispiel #2
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            TranslatedElement elem = target as TranslatedElement;

            Undo.RecordObject(elem, "Modified translation");

            if (SupportedLanguages.Instance != null)
            {
                //display what this TranslatedElement will translate:
                string willTranslate = "Will translate ";
                if (elem.GetComponent <Text>())
                {
                    willTranslate += "UI Text component on";
                }
                else if (elem.GetComponent <TextMesh>())
                {
                    willTranslate += "Text Mesh component on";
                }
                else
                {
                    willTranslate += "name of";
                }
                willTranslate += "\nthis GameObject.";
                GUILayout.Label(willTranslate);
                GUILayout.Space(10);

                foreach (SupportedLanguages.SupportedLanguage language in SupportedLanguages.Instance.supportedLanguages)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("" + language.language);
                    if (language.font != null)
                    {
                        GUIStyle style = new GUIStyle();
                        style.normal.textColor = new Color(0.5f, 0.5f, 0.5f);
                        GUILayout.Label("(uses font " + language.font.name + ")", style);
                    }
                    GUILayout.EndHorizontal();
                    bool found = false;
                    foreach (TranslatedElement.Translation t in elem.translations)
                    {
                        if (t.language == language.language)
                        {
                            found  = true;
                            t.font = language.font;
                            if (language.language == SupportedLanguages.Instance.defaultLanguage)
                            {
                                if (!EditorApplication.isPlaying)
                                {
                                    t.contents = defaultContents(elem);
                                }
                                GUILayout.Label(t.contents);
                            }
                            else
                            {
                                t.contents = EditorGUILayout.TextField(t.contents);
                                if (language.translate && t.contents == "")
                                {
                                    if (autoTranslation == null && defaultContents(elem) != "")
                                    {
                                        //let's start auto-translating this!
                                        //Attempt to find an active monobehaviour in the scene to handle the coroutine.
                                        Transform coroutineElem = elem.transform;
                                        while (!coroutineElem.gameObject.activeInHierarchy)
                                        {
                                            if (coroutineElem.parent == null)
                                            {
                                                break;
                                            }
                                            coroutineElem = coroutineElem.parent;
                                        }
                                        while (coroutineElem.GetComponent <MonoBehaviour>() == null)
                                        {
                                            //find the first active child with a monobehaviour
                                            if (coroutineElem.childCount <= 0)
                                            {
                                                break;                                                //no luck :'(
                                            }
                                            foreach (Transform child in coroutineElem)
                                            {
                                                if (child.gameObject.activeInHierarchy)
                                                {
                                                    if (child.GetComponent <MonoBehaviour>() != null)
                                                    {
                                                        coroutineElem = child;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        autoTranslation = new AutoTranslation(defaultContents(elem), SupportedLanguages.Instance.defaultLanguage, t.language, coroutineElem.GetComponent <MonoBehaviour>());
                                    }
                                    else if (autoTranslation.Finished && autoTranslation.TargetLanguage == t.language)
                                    {
                                        //done translating this language!
                                        t.contents      = autoTranslation.Translated;
                                        autoTranslation = null;
                                    }
                                }
                            }
                            break;
                        }
                    }
                    if (!found)
                    {
                        TranslatedElement.Translation t = new TranslatedElement.Translation();
                        t.language = language.language;
                        if (language.language == SupportedLanguages.Instance.defaultLanguage)
                        {
                            if (!EditorApplication.isPlaying)
                            {
                                t.contents = defaultContents(elem);
                            }
                            GUILayout.Label(t.contents);
                        }
                        else
                        {
                            t.contents = EditorGUILayout.TextField("");
                        }
                        elem.translations.Add(t);
                    }
                }

                //check whether any translations are provided for unsupported languages, and delete them.
                foreach (TranslatedElement.Translation t in elem.translations)
                {
                    bool found = false;
                    foreach (SupportedLanguages.SupportedLanguage lang in SupportedLanguages.Instance.supportedLanguages)
                    {
                        if (lang.language == t.language)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        elem.translations.Remove(t);
                        Debug.LogWarning("Removed translation " + t.language + " from " + elem.name);
                        break;
                    }
                }

                if (GUILayout.Button("Edit supported languages"))
                {
                    SupportedLanguages.ShowSupportedLanguagesMenu();
                }
            }
            else
            {
                if (GUILayout.Button("Create supported languages"))
                {
                    SupportedLanguages.ShowSupportedLanguagesMenu();
                }
            }

            serializedObject.ApplyModifiedProperties();
        }