void OnGUI_NewOrExistingTerm() { if (mKeyToExplore == null) { mKeyToExplore = string.Empty; } GUI.backgroundColor = Color.Lerp(Color.gray, Color.white, 0.2f); GUILayout.BeginVertical(EditorStyles.textArea, GUILayout.Height(1)); GUI.backgroundColor = Color.white; GUILayout.Space(5); GUILayout.Label("Replace By:"); GUILayout.EndVertical(); //--[ Create Term ]------------------------ GUILayout.BeginHorizontal(); mKeyToExplore = GUILayout.TextField(mKeyToExplore, EditorStyles.toolbarTextField, GUILayout.ExpandWidth(true)); if (GUILayout.Button("Create", "toolbarbutton", GUILayout.Width(60))) { LanguageSource.ValidateFullTerm(ref mKeyToExplore); EditorApplication.update += ReplaceSelectedTerms; } GUILayout.EndHorizontal(); //--[ Existing Term ]------------------------ int Index = 0; List <string> Terms = mLanguageSource.GetTermsList(); for (int i = 0, imax = Terms.Count; i < imax; ++i) { if (Terms[i].ToLower().Contains(mKeyToExplore.ToLower())) { Index = i; break; } } GUILayout.BeginHorizontal(); int NewIndex = EditorGUILayout.Popup(Index, Terms.ToArray(), EditorStyles.toolbarPopup, GUILayout.ExpandWidth(true)); if (NewIndex != Index) { SelectTerm(Terms [NewIndex]); ClearErrors(); } if (GUILayout.Button("Use", "toolbarbutton", GUILayout.Width(60))) { SelectTerm(Terms[NewIndex]); EditorApplication.update += ReplaceSelectedTerms; } GUILayout.EndHorizontal(); }
void OnGUI_KeysList_AddKey() { GUILayout.BeginHorizontal(); GUI.color = Color.Lerp(Color.gray, Color.white, 0.5f); bool bWasEnabled = (mTermsList_NewTerm != null); bool bEnabled = !GUILayout.Toggle(!bWasEnabled, "+", EditorStyles.toolbarButton, GUILayout.Width(30)); GUI.color = Color.white; if (bWasEnabled && !bEnabled) { mTermsList_NewTerm = null; } if (!bWasEnabled && bEnabled) { mTermsList_NewTerm = string.Empty; } if (bEnabled) { GUILayout.BeginHorizontal(EditorStyles.toolbar); mTermsList_NewTerm = EditorGUILayout.TextField(mTermsList_NewTerm, EditorStyles.toolbarTextField, GUILayout.ExpandWidth(true)); GUILayout.EndHorizontal(); LanguageSource.ValidateFullTerm(ref mTermsList_NewTerm); if (string.IsNullOrEmpty(mTermsList_NewTerm) || mLanguageSource.ContainsTerm(mTermsList_NewTerm)) { GUI.enabled = false; } if (GUILayout.Button("Create Key", "toolbarbutton", GUILayout.ExpandWidth(false))) { AddTerm(mTermsList_NewTerm); SelectTerm(mTermsList_NewTerm); ClearErrors(); mTermsList_NewTerm = null; SetAllTerms_When_InferredTerms_IsInSource(); } GUI.enabled = true; } GUILayout.EndHorizontal(); }
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; } LanguageSource 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; LanguageSource.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 LanguageSource Source = null; #if UNITY_EDITOR if (mLocalize.Source != null) { Source = mLocalize.Source; } #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(); } EditorUtility.SetDirty(Source); 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); }
public string Import_CSV(string Category, List <string[]> CSV, eSpreadsheetUpdateMode UpdateMode = eSpreadsheetUpdateMode.Replace) { string[] Tokens = CSV[0]; int LanguagesStartIdx = 1; int TypeColumnIdx = -1; int DescColumnIdx = -1; var ValidColumnName_Key = new string[] { "Key" }; var ValidColumnName_Type = new string[] { "Type" }; var ValidColumnName_Desc = new string[] { "Desc", "Description" }; if (Tokens.Length > 1 && ArrayContains(Tokens[0], ValidColumnName_Key)) { if (UpdateMode == eSpreadsheetUpdateMode.Replace) { ClearAllData(); } if (Tokens.Length > 2) { if (ArrayContains(Tokens[1], ValidColumnName_Type)) { TypeColumnIdx = 1; LanguagesStartIdx = 2; } if (ArrayContains(Tokens[1], ValidColumnName_Desc)) { DescColumnIdx = 1; LanguagesStartIdx = 2; } } if (Tokens.Length > 3) { if (ArrayContains(Tokens[2], ValidColumnName_Type)) { TypeColumnIdx = 2; LanguagesStartIdx = 3; } if (ArrayContains(Tokens[2], ValidColumnName_Desc)) { DescColumnIdx = 2; LanguagesStartIdx = 3; } } } else { return("Bad Spreadsheet Format.\nFirst columns should be 'Key', 'Type' and 'Desc'"); } int nLanguages = Mathf.Max(Tokens.Length - LanguagesStartIdx, 0); int[] LanIndices = new int[nLanguages]; for (int i = 0; i < nLanguages; ++i) { if (string.IsNullOrEmpty(Tokens[i + LanguagesStartIdx])) { LanIndices [i] = -1; continue; } string langToken = Tokens[i + LanguagesStartIdx]; string LanName, LanCode; bool isLangEnabled = true; if (langToken.StartsWith("$")) { isLangEnabled = false; langToken = langToken.Substring(1); } GoogleLanguages.UnPackCodeFromLanguageName(langToken, out LanName, out LanCode); int LanIdx = -1; if (!string.IsNullOrEmpty(LanCode)) { LanIdx = GetLanguageIndexFromCode(LanCode); } else { LanIdx = GetLanguageIndex(LanName); } if (LanIdx < 0) { LanguageData lanData = new LanguageData(); lanData.Name = LanName; lanData.Code = LanCode; lanData.Flags = (byte)(0 | (isLangEnabled?0:(int)eLanguageDataFlags.DISABLED)); mLanguages.Add(lanData); LanIdx = mLanguages.Count - 1; } LanIndices[i] = LanIdx; } //--[ Update the Languages array in the existing terms]----- nLanguages = mLanguages.Count; for (int i = 0, imax = mTerms.Count; i < imax; ++i) { TermData termData = mTerms[i]; if (termData.Languages.Length < nLanguages) { Array.Resize(ref termData.Languages, nLanguages); Array.Resize(ref termData.Languages_Touch, nLanguages); Array.Resize(ref termData.Flags, nLanguages); } } //--[ Keys ]-------------- for (int i = 1, imax = CSV.Count; i < imax; ++i) { Tokens = CSV[i]; string sKey = string.IsNullOrEmpty(Category) ? Tokens[0] : string.Concat(Category, "/", Tokens[0]); bool isTouch = false; if (sKey.EndsWith("[touch]")) { sKey = sKey.Remove(sKey.Length - "[touch]".Length); isTouch = true; } LanguageSource.ValidateFullTerm(ref sKey); if (string.IsNullOrEmpty(sKey)) { continue; } TermData termData = GetTermData(sKey); // Check to see if its a new term if (termData == null) { termData = new TermData(); termData.Term = sKey; termData.Languages = new string[mLanguages.Count]; termData.Languages_Touch = new string[mLanguages.Count]; termData.Flags = new byte[mLanguages.Count]; for (int j = 0; j < mLanguages.Count; ++j) { termData.Languages[j] = termData.Languages_Touch[j] = string.Empty; } mTerms.Add(termData); mDictionary.Add(sKey, termData); } else // This term already exist if (UpdateMode == eSpreadsheetUpdateMode.AddNewTerms) { continue; } if (TypeColumnIdx > 0) { termData.TermType = GetTermType(Tokens[TypeColumnIdx]); } if (DescColumnIdx > 0) { termData.Description = Tokens[DescColumnIdx]; } for (int j = 0; j < LanIndices.Length && j < Tokens.Length - LanguagesStartIdx; ++j) { if (!string.IsNullOrEmpty(Tokens[j + LanguagesStartIdx])) // Only change the translation if there is a new value { var lanIdx = LanIndices[j]; if (lanIdx < 0) { continue; } var value = Tokens[j + LanguagesStartIdx]; //var isAuto = false;// value.Contains("[i2auto]"); //if (isAuto) //value = value.Replace ("[i2auto]", string.Empty); //if (value=="-") // value = string.Empty; if (isTouch) { termData.Languages_Touch[lanIdx] = value; /*if (isAuto) termData.Flags[lanIdx] |= (byte)TranslationFlag.AutoTranslated_Touch; * else */termData.Flags[lanIdx] &= byte.MaxValue ^ ((byte)TranslationFlag.AutoTranslated_Touch); } else { termData.Languages[lanIdx] = value; /*if (isAuto) termData.Flags[lanIdx] |= (byte)TranslationFlag.AutoTranslated_Normal; * else*/termData.Flags[lanIdx] &= byte.MaxValue ^ ((byte)TranslationFlag.AutoTranslated_Normal); } } } } return(string.Empty); }