コード例 #1
0
        static void TranslateLanguage(string Key, TermData termdata, Localize localizeCmp, LanguageSource source)
        {
            ClearErrors();
            string mainText = localizeCmp == null?LanguageSource.GetKeyFromFullTerm(Key) : localizeCmp.GetMainTargetsText();

            for (int i = 0; i < source.mLanguages.Count; ++i)
            {
                if (source.mLanguages[i].IsEnabled() && string.IsNullOrEmpty(termdata.Languages[i]))
                {
                    var langIdx  = i;
                    var term     = termdata;
                    var i2source = source;
                    Translate(mainText, ref termdata, source.mLanguages[i].Code,
                              (translation, error) =>
                    {
                        if (error != null)
                        {
                            ShowError(error);
                        }
                        else
                        if (translation != null)
                        {
                            term.Languages[langIdx] = translation;             //SetTranslation(langIdx, translation);
                                        #if UNITY_EDITOR
                            UnityEditor.EditorUtility.SetDirty(i2source);
                                        #endif
                        }
                    }, null);
                }
            }
        }
コード例 #2
0
        static void OnGUI_Keys_Languages(string Key, ref TermData termdata, Localize localizeCmp, bool IsPrimaryKey, LanguageSource source)
        {
            //--[ Languages ]---------------------------
            GUILayout.BeginVertical(EditorStyles.textArea, GUILayout.Height(1));

            OnGUI_Keys_LanguageTranslations(Key, localizeCmp, IsPrimaryKey, ref termdata, source);

            if (termdata.TermType == eTermType.Text)
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Translate All", GUILayout.Width(85)))
                {
                    string mainText = localizeCmp == null?LanguageSource.GetKeyFromFullTerm(Key) : localizeCmp.GetMainTargetsText();

                    for (int i = 0; i < source.mLanguages.Count; ++i)
                    {
                        if (string.IsNullOrEmpty(termdata.Languages[i]) && source.mLanguages[i].IsEnabled())
                        {
                            var languages = (GUI_SelectedInputType == 0 ? termdata.Languages : termdata.Languages_Touch);
                            var langIdx   = i;
                            Translate(mainText, ref termdata, source.mLanguages[i].Code, (translation) => { languages[langIdx] = translation; });
                        }
                    }
                    GUI.FocusControl(string.Empty);
                }
                GUILayout.EndHorizontal();
                OnGUI_TranslatingMessage();
            }
            GUILayout.EndVertical();
        }
コード例 #3
0
 public bool IsTerm(string name, bool allowCategoryMistmatch)
 {
     if (!allowCategoryMistmatch)
     {
         return(name == Term);
     }
     return(name == LanguageSource.GetKeyFromFullTerm(Term));
 }
コード例 #4
0
        void TranslateAllToLanguage(string lanName)
        {
            if (!GoogleTranslation.CanTranslate())
            {
                ShowError("WebService is not set correctly or needs to be reinstalled");
                return;
            }
            ClearErrors();
            int    LanIndex   = mLanguageSource.GetLanguageIndex(lanName);
            string code       = mLanguageSource.mLanguages [LanIndex].Code;
            string googleCode = GoogleLanguages.GetGoogleLanguageCode(code);

            if (string.IsNullOrEmpty(googleCode))
            {
                ShowError("Language '" + code + "' is not supported by google translate");
                return;
            }
            googleCode = code;

            mTranslationTerms.Clear();
            mTranslationRequests.Clear();
            foreach (var termData in mLanguageSource.mTerms)
            {
                if (termData.TermType != eTermType.Text)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty((GUI_SelectedInputType == 0 ? termData.Languages : termData.Languages_Touch)[LanIndex]))
                {
                    continue;
                }

                string sourceCode, sourceText;
                FindTranslationSource(LanguageSource.GetKeyFromFullTerm(termData.Term), termData, code, out sourceText, out sourceCode);

                mTranslationTerms.Add(termData.Term);

                GoogleTranslation.CreateQueries(sourceText, sourceCode, googleCode, mTranslationRequests);                   // can split plurals into several queries
            }

            if (mTranslationRequests.Count == 0)
            {
                StopConnectionWWW();
                return;
            }

            mConnection_WWW  = null;
            mConnection_Text = "Translating"; if (mTranslationRequests.Count > 1)
            {
                mConnection_Text += " (" + mTranslationRequests.Count + ")";
            }
            mConnection_Callback = null;
            //EditorApplication.update += CheckForConnection;

            GoogleTranslation.Translate(mTranslationRequests, OnLanguageTranslated);
        }
コード例 #5
0
        List <string> ScriptTool_GetSelectedTermsInCategory(string Category)
        {
            List <string> list = new List <string>();

            foreach (string FullKey in mSelectedKeys)
            {
                string categ = LanguageSource.GetCategoryFromFullTerm(FullKey);
                if (categ == Category && ShouldShowTerm(FullKey))
                {
                    list.Add(LanguageSource.GetKeyFromFullTerm(FullKey));
                }
            }

            return(list);
        }
コード例 #6
0
        void TranslateAllToLanguage(string lanName)
        {
            if (!GoogleTranslation.CanTranslate())
            {
                ShowError("WebService is not set correctly or needs to be reinstalled");
                return;
            }

            int    LanIndex = mLanguageSource.GetLanguageIndex(lanName);
            string code     = mLanguageSource.mLanguages [LanIndex].Code;

            mTranslationRequests.Clear();
            foreach (var termData in mLanguageSource.mTerms)
            {
                if (!string.IsNullOrEmpty((GUI_SelectedInputType == 0 ? termData.Languages : termData.Languages_Touch)[LanIndex]))
                {
                    continue;
                }

                string sourceCode, sourceText;
                FindTranslationSource(LanguageSource.GetKeyFromFullTerm(termData.Term), termData, code, out sourceText, out sourceCode);

                mTranslationRequests.Add(new TranslationRequest()
                {
                    Term                = termData.Term,
                    Text                = sourceText,
                    LanguageCode        = sourceCode,
                    TargetLanguagesCode = new string[] { code }
                });
            }

            mConnection_WWW  = GoogleTranslation.GetTranslationWWW(mTranslationRequests);
            mConnection_Text = "Translating"; if (mTranslationRequests.Count > 1)
            {
                mConnection_Text += " (" + mTranslationRequests.Count + ")";
            }
            mConnection_Callback      = OnLanguageTranslated;
            EditorApplication.update += CheckForConnection;
        }
コード例 #7
0
        void OnLanguageTranslated(Dictionary <string, TranslationQuery> requests, string Error)
        {
            //Debug.Log (Result);

            //if (Result.Contains("Service invoked too many times"))
            //{
            //    TimeStartTranslation = EditorApplication.timeSinceStartup + 1;
            //    EditorApplication.update += DelayedStartTranslation;
            //    mConnection_Text = "Translating (" + mTranslationRequests.Count + ")";
            //    return;
            //}

            //if (!string.IsNullOrEmpty(Error))/* || !Result.Contains("<i2>")*/
            //{
            //    Debug.LogError("WEB ERROR: " + Error);
            //	ShowError ("Unable to access Google or not valid request");
            //	return;
            //}

            ClearErrors();
            StopConnectionWWW();

            if (!string.IsNullOrEmpty(Error))
            {
                ShowError(Error);
                return;
            }

            if (requests.Values.Count == 0)
            {
                return;
            }

            var langCode  = requests.Values.First().TargetLanguagesCode [0];
            int langIndex = mLanguageSource.GetLanguageIndexFromCode(langCode);

            foreach (var term in mTranslationTerms)
            {
                var termData = mLanguageSource.GetTermData(term);
                if (termData == null)
                {
                    continue;
                }

                string sourceCode, sourceText;
                FindTranslationSource(LanguageSource.GetKeyFromFullTerm(termData.Term), termData, langCode, out sourceText, out sourceCode);

                string result = GoogleTranslation.RebuildTranslation(sourceText, mTranslationRequests, langCode);                                               // gets the result from google and rebuilds the text from multiple queries if its is plurals

                if (GUI_SelectedInputType == 0)
                {
                    termData.Languages[langIndex] = result;
                }
                else
                {
                    termData.Languages_Touch[langIndex] = result;
                }
            }

            mTranslationTerms.Clear();
            mTranslationRequests.Clear();
            StopConnectionWWW();
        }
コード例 #8
0
        static void OnGUI_Keys_LanguageTranslations(string Key, Localize localizeCmp, bool IsPrimaryKey, ref TermData termdata, LanguageSource source)
        {
            bool IsSelect = Event.current.type == EventType.MouseUp;

            for (int i = 0; i < source.mLanguages.Count; ++i)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(source.mLanguages[i].Name, GUILayout.Width(100));

                string Translation = (GUI_SelectedInputType == 0 ? termdata.Languages[i] : termdata.Languages_Touch[i]) ?? string.Empty;
                if (string.IsNullOrEmpty(Translation))
                {
                    Translation = (GUI_SelectedInputType == 1 ? termdata.Languages[i] : termdata.Languages_Touch[i]) ?? 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)
                {
                    GUI.changed = false;
                    string CtrName = "TranslatedText" + i;
                    GUI.SetNextControlName(CtrName);

                    bool autoTranslated = termdata.IsAutoTranslated(i, GUI_SelectedInputType == 1);

                    Translation = EditorGUILayout.TextArea(Translation, LocalizationEditor.Style_WrapTextField, GUILayout.Width(Screen.width - 260 - (autoTranslated ? 20 : 0)));
                    if (GUI.changed)
                    {
                        if (GUI_SelectedInputType == 0)
                        {
                            termdata.Languages[i] = Translation;
                            termdata.Flags[i]    &= byte.MaxValue ^ (byte)TranslationFlag.AutoTranslated_Normal;
                        }
                        else
                        {
                            termdata.Languages_Touch[i] = Translation;
                            termdata.Flags[i]          &= byte.MaxValue ^ (byte)TranslationFlag.AutoTranslated_Touch;
                        }
                        EditorUtility.SetDirty(source);
                    }

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

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

                        string PreviousLanguage = LocalizationManager.CurrentLanguage;
                        LocalizationManager.PreviewLanguage(source.mLanguages[i].Name);
                        localizeCmp.OnLocalize(true);
                        LocalizationManager.PreviewLanguage(PreviousLanguage);
                    }
                    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_SelectedInputType == 0 ? TranslationFlag.AutoTranslated_Normal : TranslationFlag.AutoTranslated_Touch));
                        }
                    }

                    if (GUILayout.Button("Translate", EditorStyles.toolbarButton, GUILayout.Width(80)))
                    {
                        string mainText = localizeCmp == null?LanguageSource.GetKeyFromFullTerm(Key) : localizeCmp.GetMainTargetsText();

                        if (GUI_SelectedInputType == 0)
                        {
                            Translate(mainText, ref termdata, ref termdata.Languages[i], source.mLanguages[i].Code);
                            termdata.Flags[i] |= (byte)TranslationFlag.AutoTranslated_Normal;
                        }
                        else
                        {
                            Translate(mainText, ref termdata, ref termdata.Languages_Touch[i], source.mLanguages[i].Code);
                            termdata.Flags[i] |= (byte)TranslationFlag.AutoTranslated_Touch;
                        }
                        GUI.FocusControl(string.Empty);
                    }
                }
                else
                {
                    string MultiSpriteName = string.Empty;

                    if (termdata.TermType == eTermType.Sprite && Translation.EndsWith("]"))                     // Handle sprites of type (Multiple):   "SpritePath[SpriteName]"
                    {
                        int idx = Translation.LastIndexOf("[");
                        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);
                    }

                    System.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;

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

                    case eTermType.UIFont: ObjType = typeof(UIFont); break;
#endif
#if DFGUI
                    case eTermType.dfFont: ObjType = typeof(dfFont); break;

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

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

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

#if TextMeshPro_Pre53
                    case eTermType.TextMeshPFont: ObjType = typeof(TMPro.TextMeshProFont); break;
#elif 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("AS TextArea", GUILayout.Height(1));
                        GUILayout.Space(2);

                        GUI.backgroundColor = Color.white;
                    }

                    Object NewObj = EditorGUILayout.ObjectField(Obj, ObjType, true, GUILayout.ExpandWidth(true));
                    if (Obj != NewObj && NewObj != null)
                    {
                        string sPath = AssetDatabase.GetAssetPath(NewObj);
                        AddObjectPath(ref sPath, localizeCmp, NewObj);
                        if (HasObjectInReferences(NewObj, localizeCmp))
                        {
                            sPath = NewObj.name;
                        }
                        else
                        if (termdata.TermType == eTermType.Sprite)
                        {
                            sPath += "[" + NewObj.name + "]";
                        }

                        if (GUI_SelectedInputType == 0)
                        {
                            termdata.Languages[i] = sPath;
                        }
                        else
                        {
                            termdata.Languages_Touch[i] = sPath;
                        }
                        EditorUtility.SetDirty(source);
                    }

                    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();
            }
        }
コード例 #9
0
        // this method shows the key description and the localization to each language
        public static void OnGUI_Keys_Languages(string Key, Localize localizeCmp, bool IsPrimaryKey = true)
        {
            if (Key == null)
            {
                Key = string.Empty;
            }

            TermData termdata = null;

            LanguageSource source = (localizeCmp == null ? mLanguageSource : localizeCmp.Source);

            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", UnityEditor.MessageType.Info);
                return;
            }
            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)
                {
                    EditorGUILayout.HelpBox(string.Format("Key '{0}' is not Localized or it is in a different Language Source", Key), UnityEditor.MessageType.Error);
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Add Term to Source"))
                    {
                        source.AddTerm(Key, eTermType.Text);
                        GetParsedTerm(Key);
                        SetAllTerms_When_InferredTerms_IsInSource();
                    }
                    GUILayout.FlexibleSpace();
                    GUILayout.EndHorizontal();

                    return;
                }
            }

            //--[ 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, LocalizationEditor.Style_WrapTextField);
                if (NewDesc != termdata.Description)
                {
                    termdata.Description = NewDesc;
                    EditorUtility.SetDirty(source);
                }
            }
            else
            {
                EditorGUILayout.HelpBox(string.IsNullOrEmpty(termdata.Description) ? "No description" : termdata.Description, UnityEditor.MessageType.Info);
            }

            OnGUI_Keys_Language_SpecializationsBar();

            //--[ Languages ]---------------------------
            GUILayout.BeginVertical("AS TextArea", GUILayout.Height(1));

            OnGUI_Keys_LanguageTranslations(Key, localizeCmp, IsPrimaryKey, ref termdata, source);

            if (termdata.TermType == eTermType.Text)
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Translate All", GUILayout.Width(85)))
                {
                    string mainText = localizeCmp == null?LanguageSource.GetKeyFromFullTerm(Key) : localizeCmp.GetMainTargetsText();

                    for (int i = 0; i < source.mLanguages.Count; ++i)
                    {
                        if (string.IsNullOrEmpty(termdata.Languages[i]))
                        {
                            if (GUI_SelectedInputType == 0)
                            {
                                Translate(mainText, ref termdata, ref termdata.Languages[i], source.mLanguages[i].Code);
                                termdata.Flags[i] |= (byte)TranslationFlag.AutoTranslated_Normal;
                            }
                            else
                            {
                                Translate(mainText, ref termdata, ref termdata.Languages_Touch[i], source.mLanguages[i].Code);
                                termdata.Flags[i] |= (byte)TranslationFlag.AutoTranslated_Touch;
                            }
                        }
                    }
                    GUI.FocusControl(string.Empty);
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();
        }