Beispiel #1
0
        static void OnGUI_Keys_LanguageTranslations(string Key, Localize localizeCmp, bool IsPrimaryKey, ref TermData termdata, LanguageSourceData source)
        {
            bool IsSelect = Event.current.type == EventType.MouseUp;

            for (int i = 0; i < source.mLanguages.Count; ++i)
            {
                bool forcePreview      = false;
                bool isEnabledLanguage = source.mLanguages[i].IsEnabled();

                if (!isEnabledLanguage)
                {
                    if (!GUI_ShowDisabledLanguagesTranslation)
                    {
                        continue;
                    }
                    GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, 0.35f);
                }
                GUILayout.BeginHorizontal();

                if (GUILayout.Button(source.mLanguages[i].Name, EditorStyles.label, GUILayout.Width(100)))
                {
                    forcePreview = true;
                }


                string Translation = termdata.GetTranslation(i, GUI_SelectedSpecialization, editMode: true);
                if (Translation == null)
                {
                    Translation = string.Empty;
                }

//				if (termdata.Languages[i] != termdata.Languages_Touch[i] && !string.IsNullOrEmpty(termdata.Languages[i]) && !string.IsNullOrEmpty(termdata.Languages_Touch[i]))
//					GUI.contentColor = GUITools.LightYellow;

                if (termdata.TermType == eTermType.Text || termdata.TermType == eTermType.Child)
                {
                    EditorGUI.BeginChangeCheck();
                    string CtrName = "TranslatedText" + i;
                    GUI.SetNextControlName(CtrName);

                    EditPluralTranslations(ref Translation, i, source.mLanguages[i].Code);
                    //Translation = EditorGUILayout.TextArea(Translation, Style_WrapTextField, GUILayout.Width(Screen.width - 260 - (autoTranslated ? 20 : 0)));
                    if (EditorGUI.EndChangeCheck())
                    {
                        termdata.SetTranslation(i, Translation, GUI_SelectedSpecialization);
                        source.Editor_SetDirty();
                        forcePreview = true;
                    }

                    if (localizeCmp != null &&
                        (forcePreview || /*GUI.changed || */ (GUI.GetNameOfFocusedControl() == CtrName && IsSelect)))
                    {
                        if (IsPrimaryKey && string.IsNullOrEmpty(localizeCmp.Term))
                        {
                            localizeCmp.mTerm = Key;
                        }

                        if (!IsPrimaryKey && string.IsNullOrEmpty(localizeCmp.SecondaryTerm))
                        {
                            localizeCmp.mTermSecondary = Key;
                        }

                        string PreviousLanguage = LocalizationManager.CurrentLanguage;
                        LocalizationManager.PreviewLanguage(source.mLanguages[i].Name);
                        if (forcePreview || IsSelect)
                        {
                            LocalizationManager.LocalizeAll();
                        }
                        else
                        {
                            localizeCmp.OnLocalize(true);
                        }
                        LocalizationManager.PreviewLanguage(PreviousLanguage);
                        EditorUtility.SetDirty(localizeCmp);
                    }
                    GUI.contentColor = Color.white;

                    //if (autoTranslated)
                    //{
                    //    if (GUILayout.Button(new GUIContent("\u2713"/*"A"*/,"Translated by Google Translator\nClick the button to approve the translation"), EditorStyles.toolbarButton, GUILayout.Width(autoTranslated ? 20 : 0)))
                    //    {
                    //        termdata.Flags[i] &= (byte)(byte.MaxValue ^ (byte)(GUI_SelectedSpecialization==0 ? TranslationFlag.AutoTranslated_Normal : TranslationFlag.AutoTranslated_Touch));
                    //    }
                    //}

                    if (termdata.TermType == eTermType.Text)
                    {
                        if (TestButtonArg(eTest_ActionType.Button_Term_Translate, i, new GUIContent("T", "Translate"), EditorStyles.toolbarButton, GUILayout.Width(20)))
                        {
                            var termData = termdata;
                            var indx     = i;
                            var key      = Key;
                            GUITools.DelayedCall(() => TranslateTerm(key, termData, source, indx));
                            GUI.FocusControl(string.Empty);
                        }
                    }
                }
                else
                {
                    string MultiSpriteName = string.Empty;

                    if (termdata.TermType == eTermType.Sprite && Translation.EndsWith("]", StringComparison.Ordinal))                   // Handle sprites of type (Multiple):   "SpritePath[SpriteName]"
                    {
                        int idx = Translation.LastIndexOf("[", StringComparison.Ordinal);
                        int len = Translation.Length - idx - 2;
                        MultiSpriteName = Translation.Substring(idx + 1, len);
                        Translation     = Translation.Substring(0, idx);
                    }

                    Object Obj = null;

                    // Try getting the asset from the References section
                    if (localizeCmp != null)
                    {
                        Obj = localizeCmp.FindTranslatedObject <Object>(Translation);
                    }
                    if (Obj == null && source != null)
                    {
                        Obj = source.FindAsset(Translation);
                    }

                    // If it wasn't in the references, Load it from Resources
                    if (Obj == null && localizeCmp == null)
                    {
                        Obj = ResourceManager.pInstance.LoadFromResources <Object>(Translation);
                    }

                    Type ObjType = typeof(Object);
                    switch (termdata.TermType)
                    {
                    case eTermType.Font: ObjType = typeof(Font); break;

                    case eTermType.Texture: ObjType = typeof(Texture); break;

                    case eTermType.AudioClip: ObjType = typeof(AudioClip); break;

                    case eTermType.GameObject: ObjType = typeof(GameObject); break;

                    case eTermType.Sprite: ObjType = typeof(Sprite); break;

                    case eTermType.Material: ObjType = typeof(Material); break;

                    case eTermType.Mesh: ObjType = typeof(Mesh); break;

#if NGUI
                    case eTermType.UIAtlas: ObjType = typeof(UIAtlas); break;

                    case eTermType.UIFont: ObjType = typeof(UIFont); break;
#endif
#if TK2D
                    case eTermType.TK2dFont: ObjType = typeof(tk2dFont); break;

                    case eTermType.TK2dCollection: ObjType = typeof(tk2dSpriteCollection); break;
#endif

#if TextMeshPro
                    case eTermType.TextMeshPFont: ObjType = typeof(TMPro.TMP_FontAsset); break;
#endif

#if SVG
                    case eTermType.SVGAsset: ObjType = typeof(SVGImporter.SVGAsset); break;
#endif

                    case eTermType.Object: ObjType = typeof(Object); break;
                    }

                    if (Obj != null && !string.IsNullOrEmpty(MultiSpriteName))
                    {
                        string   sPath = AssetDatabase.GetAssetPath(Obj);
                        Object[] objs  = AssetDatabase.LoadAllAssetRepresentationsAtPath(sPath);
                        Obj = null;
                        for (int j = 0, jmax = objs.Length; j < jmax; ++j)
                        {
                            if (objs[j].name.Equals(MultiSpriteName))
                            {
                                Obj = objs[j];
                                break;
                            }
                        }
                    }

                    bool bShowTranslationLabel = (Obj == null && !string.IsNullOrEmpty(Translation));
                    if (bShowTranslationLabel)
                    {
                        GUI.backgroundColor = GUITools.DarkGray;
                        GUILayout.BeginVertical(EditorStyles.textArea, GUILayout.Height(1));
                        GUILayout.Space(2);

                        GUI.backgroundColor = Color.white;
                    }

                    Object NewObj = EditorGUILayout.ObjectField(Obj, ObjType, true, GUILayout.ExpandWidth(true));
                    if (Obj != NewObj)
                    {
                        string sPath = null;
                        if (NewObj != null)
                        {
                            sPath = AssetDatabase.GetAssetPath(NewObj);

                            mCurrentInspector.serializedObject.ApplyModifiedProperties();
                            foreach (var cmp in mCurrentInspector.serializedObject.targetObjects)
                            {
                                AddObjectPath(ref sPath, cmp as Localize, NewObj);
                            }
                            mCurrentInspector.serializedObject.ApplyModifiedProperties();

                            if (HasObjectInReferences(NewObj, localizeCmp))
                            {
                                sPath = NewObj.name;
                            }
                            else
                            if (termdata.TermType == eTermType.Sprite)
                            {
                                sPath += "[" + NewObj.name + "]";
                            }
                        }

                        termdata.SetTranslation(i, sPath, GUI_SelectedSpecialization);
                        source.Editor_SetDirty();
                    }

                    if (bShowTranslationLabel)
                    {
                        GUILayout.BeginHorizontal();
                        GUI.color = Color.red;
                        GUILayout.FlexibleSpace();
                        GUILayout.Label(Translation, EditorStyles.miniLabel);
                        GUILayout.FlexibleSpace();
                        GUI.color = Color.white;
                        GUILayout.EndHorizontal();
                        GUILayout.EndVertical();
                    }
                }

                GUILayout.EndHorizontal();
                GUI.color = Color.white;
            }
        }
Beispiel #2
0
        bool OnGUI_SelectKey(ref string Term, bool Inherited)            // Inherited==true means that the mTerm is empty and we are using the Label.text instead
        {
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();

            GUI.changed       = false;
            mAllowEditKeyName = GUILayout.Toggle(mAllowEditKeyName, "Term:", EditorStyles.foldout, GUILayout.ExpandWidth(false));
            if (GUI.changed && mAllowEditKeyName)
            {
                mNewKeyName = Term;
                mTermsArray = null;
            }

            bool bChanged = false;

            if (mTermsArray == null || (Term != "-" && System.Array.IndexOf(mTermsArray, Term) < 0))
            {
                UpdateTermsList(Term);
            }

            if (Inherited)
            {
                GUI.contentColor = Color.Lerp(Color.gray, Color.yellow, 0.1f);
            }

            int Index = (Term == "-" || Term == "") ? mTermsArray.Length - 1 : System.Array.IndexOf(mTermsArray, Term);

            GUI.changed = false;

            int newIndex = EditorGUILayout.Popup(Index, mTermsArray);

            GUI.contentColor = Color.white;
            if (/*newIndex != Index && newIndex>=0*/ GUI.changed)
            {
                GUI.changed = false;
                if (mLocalize.Source != null && newIndex == mTermsArray.Length - 4)  //< show terms from all sources >
                {
                    mLocalize.Source = null;
                    mTermsArray      = null;
                }
                else
                if (newIndex == mTermsArray.Length - 2)  //<inferred from text>
                {
                    mNewKeyName = Term = string.Empty;
                }
                else
                if (newIndex == mTermsArray.Length - 1)  //<none>
                {
                    mNewKeyName = Term = "-";
                }
                else
                {
                    mNewKeyName = Term = mTermsArray[newIndex];
                }


                if (GUI_SelectedTerm == 0)
                {
                    mLocalize.SetTerm(mNewKeyName);
                }
                else
                {
                    mLocalize.SetTerm(null, mNewKeyName);
                }
                mAllowEditKeyName = false;
                bChanged          = true;
            }

            LanguageSourceData source   = LocalizationManager.GetSourceContaining(Term);
            TermData           termData = source.GetTermData(Term);

            if (termData != null)
            {
                if (Inherited)
                {
                    bChanged = true;                     // if the term its inferred and a matching term its found, then use that
                }
                eTermType NewType = (eTermType)EditorGUILayout.EnumPopup(termData.TermType, GUILayout.Width(90));
                if (termData.TermType != NewType)
                {
                    termData.TermType = NewType;
                }
            }

            GUILayout.EndHorizontal();

            if (mAllowEditKeyName)
            {
                GUILayout.BeginHorizontal(GUILayout.Height(1));
                GUILayout.BeginHorizontal(EditorStyles.toolbar);
                if (mNewKeyName == null)
                {
                    mNewKeyName = string.Empty;
                }

                GUI.changed = false;
                mNewKeyName = EditorGUILayout.TextField(mNewKeyName, new GUIStyle("ToolbarSeachTextField"), GUILayout.ExpandWidth(true));
                if (GUI.changed)
                {
                    mTermsArray = null;                         // regenerate this array to apply filtering
                    GUI.changed = false;
                }

                if (GUILayout.Button(string.Empty, string.IsNullOrEmpty(mNewKeyName) ? "ToolbarSeachCancelButtonEmpty" : "ToolbarSeachCancelButton", GUILayout.ExpandWidth(false)))
                {
                    mTermsArray = null;                         // regenerate this array to apply filtering
                    mNewKeyName = string.Empty;
                }

                GUILayout.EndHorizontal();

                string ValidatedName = mNewKeyName;
                LanguageSourceData.ValidateFullTerm(ref ValidatedName);

                bool CanUseNewName = (source.GetTermData(ValidatedName) == null);
                GUI.enabled = (!string.IsNullOrEmpty(mNewKeyName) && CanUseNewName);
                if (GUILayout.Button("Create", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
                {
                    mNewKeyName = ValidatedName;
                    mTermsArray = null;                         // this recreates that terms array

                    LanguageSourceData Source = null;
                                        #if UNITY_EDITOR
                    if (mLocalize.Source != null)
                    {
                        Source = mLocalize.Source.SourceData;
                    }
                                        #endif

                    if (Source == null)
                    {
                        Source = LocalizationManager.Sources[0];
                    }
                    Term = mNewKeyName;
                    var data = Source.AddTerm(mNewKeyName, eTermType.Text, false);
                    if (data.Languages.Length > 0)
                    {
                        data.Languages[0] = mLocalize.GetMainTargetsText();
                    }
                    Source.Editor_SetDirty();
                    AssetDatabase.SaveAssets();
                    mAllowEditKeyName          = false;
                    bChanged                   = true;
                    GUIUtility.keyboardControl = 0;
                }
                GUI.enabled = (termData != null && !string.IsNullOrEmpty(mNewKeyName) && CanUseNewName);
                if (GUILayout.Button(new GUIContent("Rename", "Renames the term in the source and updates every object using it in the current scene"), EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
                {
                    mNewKeyName       = ValidatedName;
                    Term              = mNewKeyName;
                    mTermsArray       = null;                 // this recreates that terms array
                    mAllowEditKeyName = false;
                    bChanged          = true;
                    LocalizationEditor.TermReplacements = new Dictionary <string, string>(System.StringComparer.Ordinal);
                    LocalizationEditor.TermReplacements[termData.Term] = mNewKeyName;
                    termData.Term = mNewKeyName;
                    source.UpdateDictionary(true);
                    LocalizationEditor.ReplaceTermsInCurrentScene();
                    GUIUtility.keyboardControl = 0;
                    EditorApplication.update  += LocalizationEditor.DoParseTermsInCurrentScene;
                }
                GUI.enabled = true;
                GUILayout.EndHorizontal();

                bChanged |= OnGUI_SelectKey_PreviewTerms(ref Term);
            }

            GUILayout.Space(5);
            return(bChanged);
        }
Beispiel #3
0
        // this method shows the key description and the localization to each language
        public static TermData OnGUI_Keys_Languages(string Key, Localize localizeCmp, bool IsPrimaryKey = true)
        {
            if (Key == null)
            {
                Key = string.Empty;
            }

            TermData termdata = null;

            LanguageSourceData source = mLanguageSource;

            if (localizeCmp != null && localizeCmp.Source != null)
            {
                source = localizeCmp.Source.SourceData;
            }

            if (source == null)
            {
                source = LocalizationManager.GetSourceContaining(Key, false);
            }

            if (source == null)
            {
                if (localizeCmp == null)
                {
                    source = LocalizationManager.Sources[0];
                }
                else
                {
                    source = LocalizationManager.GetSourceContaining(IsPrimaryKey ? localizeCmp.SecondaryTerm : localizeCmp.Term, true);
                }
            }


            if (string.IsNullOrEmpty(Key))
            {
                EditorGUILayout.HelpBox("Select a Term to Localize", MessageType.Info);
                return(null);
            }
            else
            {
                termdata = source.GetTermData(Key);
                if (termdata == null && localizeCmp != null)
                {
                    var realSource = LocalizationManager.GetSourceContaining(Key, false);
                    if (realSource != null)
                    {
                        termdata = realSource.GetTermData(Key);
                        source   = realSource;
                    }
                }
                if (termdata == null)
                {
                    if (Key == "-")
                    {
                        return(null);
                    }
                    EditorGUILayout.HelpBox(string.Format("Key '{0}' is not Localized or it is in a different Language Source", Key), MessageType.Error);
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Add Term to Source"))
                    {
                        var termType = eTermType.Text;
                        if (localizeCmp != null && localizeCmp.mLocalizeTarget != null)
                        {
                            termType = IsPrimaryKey ? localizeCmp.mLocalizeTarget.GetPrimaryTermType(localizeCmp)
                                                    : localizeCmp.mLocalizeTarget.GetSecondaryTermType(localizeCmp);
                        }

                        AddTerm(Key, true, termType);
                    }
                    GUILayout.FlexibleSpace();
                    GUILayout.EndHorizontal();

                    return(null);
                }
            }

            //--[ Type ]----------------------------------
            if (localizeCmp == null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Type:", GUILayout.ExpandWidth(false));
                eTermType NewType = (eTermType)EditorGUILayout.EnumPopup(termdata.TermType, GUILayout.ExpandWidth(true));
                if (termdata.TermType != NewType)
                {
                    termdata.TermType = NewType;
                }
                GUILayout.EndHorizontal();
            }


            //--[ Description ]---------------------------

            mKeysDesc_AllowEdit = GUILayout.Toggle(mKeysDesc_AllowEdit, "Description", EditorStyles.foldout, GUILayout.ExpandWidth(true));

            if (mKeysDesc_AllowEdit)
            {
                string NewDesc = EditorGUILayout.TextArea(termdata.Description, Style_WrapTextField);
                if (NewDesc != termdata.Description)
                {
                    termdata.Description = NewDesc;
                    source.Editor_SetDirty();
                }
            }
            else
            {
                EditorGUILayout.HelpBox(string.IsNullOrEmpty(termdata.Description) ? "No description" : termdata.Description, MessageType.Info);
            }

            OnGUI_Keys_Language_SpecializationsBar(termdata, source);

            OnGUI_Keys_Languages(Key, ref termdata, localizeCmp, IsPrimaryKey, source);
            return(termdata);
        }