예제 #1
0
        public bool TryGetTranslation(string term, out string Translation, string overrideLanguage = null)
        {
            int Index = GetLanguageIndex(overrideLanguage == null ? LocalizationManager.CurrentLanguage : overrideLanguage, SkipDisabled: false);

            if (Index >= 0 /* && mLanguages[Index].IsEnabled()*/)
            {
                TermData data = GetTermData(term);
                if (data != null)
                {
                    Translation = data.GetTranslation(Index);

                    // "---" is a code to define that the translation is meant to be empty
                    if (Translation == "---")
                    {
                        Translation = string.Empty;
                        return(true);
                    }
                    else
                    if (!string.IsNullOrEmpty(Translation))
                    {
                        // has a valid translation
                        return(true);
                    }
                    else
                    {
                        Translation = null;
                    }
                }

                if (OnMissingTranslation == MissingTranslationAction.ShowWarning)
                {
                    Translation = string.Format("<!-Missing Translation [{0}]-!>", term);
                    return(true);
                }
                else
                if (OnMissingTranslation == MissingTranslationAction.Fallback && data != null)
                {
                    for (int i = 0; i < mLanguages.Count; ++i)
                    {
                        if (i != Index && mLanguages[i].IsEnabled())
                        {
                            Translation = data.GetTranslation(i);
                            if (!string.IsNullOrEmpty(Translation))
                            {
                                return(true);
                            }
                        }
                    }
                }
                else
                if (OnMissingTranslation == MissingTranslationAction.Empty)
                {
                    Translation = string.Empty;
                    return(true);
                }
            }

            Translation = null;
            return(false);
        }
예제 #2
0
 public static string GetAppName(string languageCode)
 {
     if (!string.IsNullOrEmpty(languageCode))
     {
         for (int i = 0; i < Sources.Count; i++)
         {
             if (string.IsNullOrEmpty(Sources[i].mTerm_AppName))
             {
                 continue;
             }
             int languageIndexFromCode = Sources[i].GetLanguageIndexFromCode(languageCode, exactMatch: false);
             if (languageIndexFromCode < 0)
             {
                 continue;
             }
             TermData termData = Sources[i].GetTermData(Sources[i].mTerm_AppName);
             if (termData != null)
             {
                 string translation = termData.GetTranslation(languageIndexFromCode);
                 if (!string.IsNullOrEmpty(translation))
                 {
                     return(translation);
                 }
             }
         }
     }
     return(Application.productName);
 }
예제 #3
0
        bool TryGetFallbackTranslation(TermData termData, out string Translation, int langIndex, string overrideSpecialization = null, bool skipDisabled = false)
        {
            // Find base Language Code
            string baseLanguage = mLanguages[langIndex].Code;

            if (!string.IsNullOrEmpty(baseLanguage))
            {
                if (baseLanguage.Contains('-'))
                {
                    baseLanguage = baseLanguage.Substring(0, baseLanguage.IndexOf('-'));
                }

                // Try finding in any of the Region of the base language
                for (int i = 0; i < mLanguages.Count; ++i)
                {
                    if (i != langIndex &&
                        mLanguages[i].Code.StartsWith(baseLanguage) &&
                        (!skipDisabled || mLanguages[i].IsEnabled()))
                    {
                        Translation = termData.GetTranslation(i, overrideSpecialization, editMode: true);
                        if (!string.IsNullOrEmpty(Translation))
                        {
                            return(true);
                        }
                    }
                }
            }


            // Otherwise, Try finding the first active language with a valid translation
            for (int i = 0; i < mLanguages.Count; ++i)
            {
                if (i != langIndex &&
                    (!skipDisabled || mLanguages[i].IsEnabled()) &&
                    (baseLanguage == null || !mLanguages[i].Code.StartsWith(baseLanguage)))
                {
                    Translation = termData.GetTranslation(i, overrideSpecialization, editMode: true);
                    if (!string.IsNullOrEmpty(Translation))
                    {
                        return(true);
                    }
                }
            }
            Translation = null;
            return(false);
        }
예제 #4
0
        public bool TryGetTermTranslation(string term, out string Translation)
        {
            int languageIndex = GetLanguageIndex(LocalizationManager.CurrentLanguage, AllowDiscartingRegion: true, SkipDisabled: false);

            if (languageIndex >= 0)
            {
                TermData termData = GetTermData(term);
                if (termData != null)
                {
                    Translation = termData.GetTranslation(languageIndex);
                    if (Translation == "---")
                    {
                        Translation = string.Empty;
                        return(true);
                    }
                    if (!string.IsNullOrEmpty(Translation))
                    {
                        return(true);
                    }
                    Translation = null;
                }
                if (OnMissingTranslation == MissingTranslationAction.ShowWarning)
                {
                    Translation = $"<!-Missing Translation [{term}]-!>";
                    return(false);
                }
                if (OnMissingTranslation == MissingTranslationAction.Fallback && termData != null)
                {
                    for (int i = 0; i < mLanguages.Count; i++)
                    {
                        if (i != languageIndex && mLanguages[i].IsEnabled())
                        {
                            Translation = termData.GetTranslation(i);
                            if (Translation != null)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            Translation = null;
            return(false);
        }
예제 #5
0
        public bool TryGetTranslation(string term, out string Translation, string overrideLanguage = null, string overrideSpecialization = null, bool skipDisabled = false, bool allowCategoryMistmatch = false)
        {
            int Index = GetLanguageIndex(overrideLanguage == null ? LocalizationManager.CurrentLanguage : overrideLanguage, SkipDisabled: false);

            if (Index >= 0 && (!skipDisabled || mLanguages[Index].IsEnabled()))
            {
                TermData data = GetTermData(term, allowCategoryMistmatch: allowCategoryMistmatch);
                if (data != null)
                {
                    Translation = data.GetTranslation(Index, overrideSpecialization, editMode: true);

                    // "---" is a code to define that the translation is meant to be empty
                    if (Translation == "---")
                    {
                        Translation = string.Empty;
                        return(true);
                    }
                    else
                    if (!string.IsNullOrEmpty(Translation))
                    {
                        // has a valid translation
                        return(true);
                    }
                    else
                    {
                        Translation = null;
                    }
                }

                if (OnMissingTranslation == MissingTranslationAction.ShowWarning)
                {
                    Translation = string.Format("<!-Missing Translation [{0}]-!>", term);
                    return(true);
                }
                else
                if (OnMissingTranslation == MissingTranslationAction.Fallback && data != null)
                {
                    return(TryGetFallbackTranslation(data, out Translation, Index, overrideSpecialization, skipDisabled));
                }
                else
                if (OnMissingTranslation == MissingTranslationAction.Empty)
                {
                    Translation = string.Empty;
                    return(true);
                }
                else
                if (OnMissingTranslation == MissingTranslationAction.ShowTerm)
                {
                    Translation = term;
                    return(true);
                }
            }

            Translation = null;
            return(false);
        }
        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 (TestButton(eTest_ActionType.Button_Term_TranslateAll, "Translate All", "Button", GUILayout.Width(85)))
                {
                    ClearErrors();
                    string mainText = localizeCmp == null?LanguageSource.GetKeyFromFullTerm(Key) : localizeCmp.GetMainTargetsText();

                    for (int i = 0; i < source.mLanguages.Count; ++i)
                    {
                        if (string.IsNullOrEmpty(termdata.GetTranslation(i)) && source.mLanguages[i].IsEnabled())
                        {
                            var languages      = (GUI_SelectedInputType == 0 ? termdata.Languages : termdata.Languages_Touch);
                            var languagesOther = (GUI_SelectedInputType == 0 ? termdata.Languages_Touch : termdata.Languages);
                            var langIdx        = i;
                            Translate(mainText, ref termdata, source.mLanguages[i].Code,
                                      (translation, error) =>
                            {
                                if (error != null)
                                {
                                    ShowError(error);
                                }
                                else
                                if (translation != null)
                                {
                                    languages[langIdx]      = translation;
                                    languagesOther[langIdx] = null;
                                }
                            });
                        }
                    }
                    GUI.FocusControl(string.Empty);
                }
                GUILayout.EndHorizontal();
                OnGUI_TranslatingMessage();
            }
            GUILayout.EndVertical();
        }
        static void AppendI2Term(StringBuilder Builder, int nLanguages, string Term, TermData termData, char Separator, string forceSpecialization)
        {
            //--[ Key ] --------------
            AppendI2Text(Builder, Term);
            if (!string.IsNullOrEmpty(forceSpecialization) && forceSpecialization != "Any")
            {
                Builder.Append("[");
                Builder.Append(forceSpecialization);
                Builder.Append("]");
            }
            Builder.Append("[*]");

            //--[ Type and Description ] --------------
            Builder.Append(termData.TermType.ToString());
            Builder.Append("[*]");
            Builder.Append(termData.Description);

            //--[ Languages ] --------------
            for (int i = 0; i < Mathf.Min(nLanguages, termData.Languages.Length); ++i)
            {
                Builder.Append("[*]");

                string translation = termData.Languages[i];
                if (!string.IsNullOrEmpty(forceSpecialization))
                {
                    translation = termData.GetTranslation(i, forceSpecialization);
                }

                //bool isAutoTranslated = ((termData.Flags[i]&FlagBitMask)>0);

                /*if (translation == null)
                 *  translation = string.Empty;
                 * else
                 * if (translation == "")
                 *      translation = "-";*/
                //if (isAutoTranslated) Builder.Append("[i2auto]");
                AppendI2Text(Builder, translation);
            }
        }
        static void AppendTerm(StringBuilder Builder, int nLanguages, string Term, TermData termData, string specialization, char Separator)
        {
            //--[ Key ] --------------
            AppendString(Builder, Term, Separator);

            if (!string.IsNullOrEmpty(specialization) && specialization != "Any")
            {
                Builder.AppendFormat("[{0}]", specialization);
            }

            //--[ Type and Description ] --------------
            Builder.Append(Separator);
            Builder.Append(termData.TermType.ToString());
            Builder.Append(Separator);
            AppendString(Builder, termData.Description, Separator);

            //--[ Languages ] --------------
            for (int i = 0; i < Mathf.Min(nLanguages, termData.Languages.Length); ++i)
            {
                Builder.Append(Separator);

                string translation = termData.Languages[i];
                if (!string.IsNullOrEmpty(specialization))
                {
                    translation = termData.GetTranslation(i, specialization);
                }

                //bool isAutoTranslated = ((termData.Flags[i]&FlagBitMask)>0);

                //if (string.IsNullOrEmpty(s))
                //	s = "-";

                AppendTranslation(Builder, translation, Separator, /*isAutoTranslated ? "[i2auto]" : */ null);
            }
            Builder.Append("\n");
        }
예제 #9
0
        static void FindTranslationSource(string Key, TermData termdata, string TargetLanguageCode, string forceSpecialization, out string sourceText, out string sourceLanguageCode)
        {
            sourceLanguageCode = "auto";
            sourceText         = Key;

            for (int i = 0, imax = termdata.Languages.Length; i < imax; ++i)
            {
                if (mLanguageSource.mLanguages[i].IsEnabled() && !string.IsNullOrEmpty(termdata.Languages[i]))
                {
                    sourceText = forceSpecialization == null ? termdata.Languages[i] : termdata.GetTranslation(i, forceSpecialization, editMode: true);
                    if (mLanguageSource.mLanguages[i].Code != TargetLanguageCode)
                    {
                        sourceLanguageCode = mLanguageSource.mLanguages[i].Code;
                        return;
                    }
                }
            }
        }
예제 #10
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;
            }
        }
예제 #11
0
        static void OnGUI_Keys_Language_SpecializationsBar(TermData termData, LanguageSourceData source)
        {
            var activeSpecializations = termData.GetAllSpecializations();

            GUILayout.BeginHorizontal();

            var TabStyle = new GUIStyle(GUI.skin.FindStyle("dragtab"));

            TabStyle.fixedHeight = 0;

            //var ss = GUI.skin.FindStyle("TL tab left");
            var TabOpenStyle = new GUIStyle(GUI.skin.FindStyle("minibuttonmid"));

            TabOpenStyle.margin.right = -1;
            var TabCloseStyle = new GUIStyle(EditorStyles.label);

            //var TabCloseStyle = new GUIStyle(GUI.skin.FindStyle("TL tab right"));
            TabCloseStyle.margin.left  = -1;
            TabCloseStyle.padding.left = 4;

            //-- Specialization Tabs -----

            var prevSpecialization = "Any";

            foreach (var specialization in SpecializationManager.Singleton.mSpecializations)
            {
                if (!activeSpecializations.Contains(specialization) && specialization != GUI_SelectedSpecialization)
                {
                    continue;
                }

                bool isActive     = (specialization == GUI_SelectedSpecialization);
                var  labelContent = new GUIContent(specialization, "Specialization of the main translation (i.e. variants that show only on specific platforms or devices)\nThis allows using 'tap' instead of 'click' for touch devices.");

                if (isActive && activeSpecializations.Count > 1)
                {
                    GUILayout.BeginHorizontal(TabOpenStyle);
                    GUILayout.Toggle(isActive, labelContent, TabStyle, GUILayout.Height(20), GUILayout.ExpandWidth(false));
                    //GUILayout.Label(labelContent, TabOpenStyle);
                    if (specialization != "Any" && GUILayout.Button("x", TabCloseStyle, GUILayout.Width(15)))
                    {
                        termData.RemoveSpecialization(specialization);
                        GUI_SelectedSpecialization = prevSpecialization;
                        GUI.FocusControl(null);
                    }
                    GUILayout.EndHorizontal();
                }
                else
                if (GUILayout.Toggle(isActive, labelContent, TabStyle, GUILayout.Height(25), GUILayout.ExpandWidth(false)) && !isActive)
                {
                    GUI_SelectedSpecialization = specialization;
                    GUI.FocusControl(null);
                }
            }


            //-- Add new Specialization -----
            int newIndex = EditorGUILayout.Popup(-1, SpecializationManager.Singleton.mSpecializations, "DropDown", GUILayout.Width(20));

            if (newIndex >= 0)
            {
                string newSpecialization = SpecializationManager.Singleton.mSpecializations[newIndex];
                if (!activeSpecializations.Contains(newSpecialization))
                {
                    for (int iLang = 0; iLang < source.mLanguages.Count; ++iLang)
                    {
                        string Translation = termData.GetTranslation(iLang, GUI_SelectedSpecialization, editMode: true);
                        termData.SetTranslation(iLang, Translation, GUI_SelectedSpecialization);
                    }
                    GUI_SelectedSpecialization = newSpecialization;
                }
            }

            GUILayout.FlexibleSpace();


            GUI_ShowDisabledLanguagesTranslation = GUILayout.Toggle(GUI_ShowDisabledLanguagesTranslation, new GUIContent("L", "Show Disabled Languages"), "Button", GUILayout.ExpandWidth(false));
            GUILayout.EndHorizontal();
            GUILayout.Space(-3);
        }