/// <summary> /// Returns whether the specified key is present. /// </summary> public static bool Exists(string key) { Load(); return(LocalisationData.ContainsEntry(key)); }
protected void DrawEntries() { _entriesHelpRect = EditorHelper.ShowHideableHelpBox("GameFramework.LocalisationEditorWindow.Entries", "Entries contain a set of unique tags that identify the text that you want to localise. You can further associate different translations with these tags for the different languages that you have setup.", _entriesHelpRect); // filter EditorGUILayout.BeginHorizontal(); GUI.changed = false; EditorGUI.BeginChangeCheck(); _filter = EditorGUILayout.TextField(_filter, GuiStyles.ToolbarSearchField, GUILayout.ExpandWidth(true)); if (EditorGUI.EndChangeCheck()) { foreach (var entryReference in _entryReferenceList) { entryReference.MatchesFilter = entryReference.Key.IndexOf(_filter, StringComparison.OrdinalIgnoreCase) >= 0; } } if (GUILayout.Button("", string.IsNullOrEmpty(_filter) ? GuiStyles.ToolbarSearchFieldCancelEmpty : GuiStyles.ToolbarSearchFieldCancel, GUILayout.ExpandWidth(false))) { _filter = ""; GUIUtility.keyboardControl = 0; foreach (var entryReference in _entryReferenceList) { entryReference.MatchesFilter = true; } } EditorGUILayout.EndHorizontal(); if (_entryReferenceList.Count == 0) { GUILayout.Space(20); EditorGUILayout.LabelField("Create new localisation entries below.", GuiStyles.CenteredLabelStyle, GUILayout.ExpandWidth(true)); GUILayout.Space(20); } else { _entriesScrollPosition = EditorGUILayout.BeginScrollView(_entriesScrollPosition, false, false, GUILayout.ExpandHeight(false)); //Debug.Log(Event.current.type + ": " +_entriesScrollHeight + ", " + _entriesScrollPosition); // here we avoid using properties due to them being very slow! var indexForDeleting = -1; //var accumulativeHeightDrawn = 0; //var accumulativeSpace = 0; for (var i = 0; i < _entryReferenceList.Count; i++) { var entryReference = _entryReferenceList[i]; if (entryReference.MatchesFilter) { //// if top is below viewport or bottom is above then this item is not visible. For repaint events we need to use the //// previously recorded display state to avoid changing the number of controls drawn. ////if ((Event.current.type == EventType.Repaint && entryData.IsShown == false) || //// (Event.current.type != EventType.Repaint && //if (((Event.current.type != EventType.Layout && entryReference.IsShown == false) || // (Event.current.type == EventType.Layout && // (accumulativeHeightDrawn > _entriesScrollPosition.y + _entriesScrollHeight || // accumulativeHeightDrawn + entryReference.Height < _entriesScrollPosition.y))) && !entryReference.IsExpanded) //{ // accumulativeHeightDrawn += entryReference.Height; // accumulativeSpace += entryReference.Height; // entryReference.IsShown = false; //} //else //{ // // fill space from skipped. // if (accumulativeSpace > 0) GUILayout.Space(accumulativeSpace); // accumulativeSpace = 0; // draw the displayed item entryReference.IsShown = true; var localisationEntry = _targetLocalisationData.GetEntry(_entryReferenceList[i].Key); EditorGUILayout.BeginHorizontal(); EditorGUI.indentLevel++; //var oldExpanded = entryReference.IsExpanded; entryReference.IsExpanded = EditorGUILayout.Foldout(entryReference.IsExpanded, localisationEntry.Key); // if we are not using a static default height (see below) then we need to flag changes to the expanded state, and in OnInspectorGUI // call repaint within a 'if (Event.current.type == EventType.Repaint && _updatePostRepaint)' check. This so that if an entry // decreases in size we correctly draw items that might previously have been hidden. //if (entryReference.IsExpanded != oldExpanded) // force repaint on changes incase new items become visable due to collapsing // _updatePostRepaint = true; EditorGUI.indentLevel--; if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(GuiStyles.RemoveButtonWidth))) { indexForDeleting = i; break; } EditorGUILayout.EndHorizontal(); //if (Event.current.type == EventType.Repaint) //entryData.Height = (int)GUILayoutUtility.GetLastRect().height; // only works on repaint. entryReference.Height = _entriesDefaultRowHeight; // handle expended status if (entryReference.IsExpanded) { EditorGUILayout.BeginVertical(); EditorGUI.indentLevel++; for (var li = 0; li < localisationEntry.Languages.Length; li++) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(_targetLocalisationData.Languages[li].Name, GUILayout.Width(100)); EditorGUI.BeginChangeCheck(); var lang = EditorGUILayout.TextArea(localisationEntry.Languages[li], GuiStyles.WordWrapStyle, GUILayout.Width(Screen.width - 100 - 60 - 50)); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(_targetLocalisationData, "Edit Localisation Entry"); localisationEntry.Languages[li] = lang; _targetChanged = true; } // TODO: Move to a callback so we don't block the UI if (li > 0 && GUILayout.Button("Translate", EditorStyles.miniButton, GUILayout.Width(60))) { var sourceCode = _targetLocalisationData.Languages[0].Code; if (!string.IsNullOrEmpty(sourceCode)) { var targetCode = _targetLocalisationData.Languages[li].Code; if (!string.IsNullOrEmpty(targetCode)) { var sourceText = localisationEntry.Languages[0]; string url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + sourceCode + "&tl=" + targetCode + "&dt=t&q=" + UnityWebRequest.EscapeURL(sourceText); var wwwForm = new WWWForm(); wwwForm.AddField("username", ""); //var headers = new Dictionary<string, string>(); wwwForm.headers.Add("content-type", "application/json"); var www = new WWW(url, wwwForm); while (!www.isDone) { ; } if (www.error != null) { Debug.LogError(www.error); } else { Debug.Log("Google Translate Response:" + www.text); var json = ObjectModel.Internal.SimpleJSON.JSONNode.Parse(www.text); if (json != null) { var translation = ""; for (var lines = 0; lines < json[0].Count; lines++) { // Dig through and take apart the text to get to the good stuff. var translatedText = json[0][lines][0].ToString(); if (translatedText.Length > 2) { translatedText = translatedText.Substring(1, translatedText.Length - 2); } if (translation.Length > 0) { translation += "\n"; } translation += translatedText.Replace("\\n", "").Replace("\\\"", "\""); } Undo.RecordObject(_targetLocalisationData, "Edit Localisation Entry"); localisationEntry.Languages[li] = translation; _targetChanged = true; } else { Debug.LogError("Unable to parse json response"); } } } else { EditorUtility.DisplayDialog("Localisation Import", "There is no code specified for the language '" + _targetLocalisationData.Languages[li].Name + "'.\n\nPlease enter under the languages tab.", "Ok"); } } else { EditorUtility.DisplayDialog("Localisation Translate", "There is no code specified for the language '" + _targetLocalisationData.Languages[0].Name + "'.\n\nPlease enter under the languages tab.", "Ok"); } } EditorGUILayout.EndHorizontal(); } EditorGUI.indentLevel--; EditorGUILayout.EndVertical(); // repaint events will give a new correct size for the last drawn rect so record here. //if (Event.current.type == EventType.Repaint) // entryReference.Height += (int)GUILayoutUtility.GetLastRect().height; // only works on repaint. //} //accumulativeHeightDrawn += entryReference.Height; // Debug.Log(entryData.LocalisationEntry.Key + ", " + accumulativeHeightDrawn); } } } //if (accumulativeSpace > 0) GUILayout.Space(accumulativeSpace); //Debug.Log(accumulativeHeightDrawn); EditorGUILayout.EndScrollView(); //if (Event.current.type == EventType.Repaint) // _entriesScrollHeight = GUILayoutUtility.GetLastRect().height; // only works on repaint. // delay deleting to avoid editor issues. if (indexForDeleting != -1) { var keyToDelete = _entryReferenceList[indexForDeleting].Key; if (EditorUtility.DisplayDialog("Delete Entry?", String.Format("Are you sure you want to delete the entry '{0}?'", keyToDelete), "Yes", "No")) { Undo.RecordObject(_targetLocalisationData, "Delete Localisation Entry"); _targetLocalisationData.RemoveEntry(keyToDelete); _entryReferenceList.RemoveAt(indexForDeleting); _targetChanged = true; } } } // add functionality EditorGUILayout.BeginHorizontal(); bool entryAddPressed = false; if (GUI.GetNameOfFocusedControl() == "EntryAdd" && Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return)) { entryAddPressed = true; Event.current.Use(); } GUI.SetNextControlName("EntryAdd"); _newKey = EditorGUILayout.TextField("", _newKey, GUILayout.ExpandWidth(true)); var isValidEntry = !string.IsNullOrEmpty(_newKey) && !_targetLocalisationData.ContainsEntry(_newKey); GUI.enabled = isValidEntry; if (entryAddPressed || GUILayout.Button(new GUIContent("Add", "Add the specified entry to the list"), EditorStyles.miniButton, GUILayout.Width(100))) { if (isValidEntry) { Undo.RecordObject(_targetLocalisationData, "Add Localisation Entry"); _targetLocalisationData.AddEntry(_newKey); _targetChanged = true; _languagesCount = _targetLocalisationData.Languages.Count; // set incase a first language was autocreated. var insertIndex = 0; var scrollOffset = 0; for (; insertIndex < _entryReferenceList.Count; insertIndex++) { if (_newKey.CompareTo(_entryReferenceList[insertIndex].Key) < 0) { break; } else { scrollOffset += _entryReferenceList[insertIndex].Height; } } _entryReferenceList.Insert(insertIndex, new EntryReference() { Key = _newKey, MatchesFilter = _newKey.IndexOf(_filter + "", StringComparison.OrdinalIgnoreCase) >= 0, IsExpanded = true }); _entriesScrollPosition.y = scrollOffset; var lastDot = _newKey.LastIndexOf(".", StringComparison.Ordinal); if (lastDot == -1) { _newKey = ""; } else { _newKey = _newKey.Substring(0, lastDot + 1); } } GUI.FocusControl("EntryAdd"); } GUI.enabled = true; EditorGUILayout.EndHorizontal(); GUILayout.Space(5); GUI.enabled = _entryReferenceList.Count > 0; if (GUILayout.Button(new GUIContent("Delete All", "Delete All entries"), EditorStyles.miniButton, GUILayout.Width(100))) { Undo.RecordObject(_targetLocalisationData, "Delete Localisation Entry"); _targetLocalisationData.ClearEntries(); _entryReferenceList.Clear(); _targetChanged = true; } GUI.enabled = true; }