public void OnGUI(Rect r) { var line = EditorGUIUtility.singleLineHeight + 2; var cHeight = EditorGUIUtility.singleLineHeight; float b = r.y + 5; int i = 0; LocalizationUnitHandle handleToRemove = null; foreach (var h in Handles) { GUI.Label(new Rect(r.x, b + line * i * 2, 150f, cHeight), h.Pack.Name); var oldValue = h.GetText(Key); var newValue = EditorGUI.TextArea(new Rect(r.x + 150f, b + line * i * 2, r.width - 150f - 60f, cHeight * 2), oldValue); if (oldValue != newValue) { h.SetText(Key, newValue); h.SaveUnit(); } if (GUI.Button(new Rect(r.x + r.width - 60f, b + line * i * 2, 60f, cHeight * 2), "Remove")) { handleToRemove = h; } i++; } if (handleToRemove != null) { handleToRemove.RemoveKey(Key); handleToRemove.SaveUnit(); Refresh(); } int selected = EditorGUI.Popup(new Rect(r.x, b + line * i * 2, 100f, cHeight), 0, new string[] { "Add localization" }.Concat( UnaddedPacks.Select(_ => $"{_.Name} ({_.Guid})")).ToArray()) ; if (selected != 0) { var packToAdd = UnaddedPacks[selected - 1]; packToAdd.RequestUnitHandle(UnitPath, true).AddKey(Key); Refresh(); } }
private void AddLocalizationElement(LocalizationUnitHandle handle, string key) { var container = this.Q <Box>("localizations"); var box = new Box(); box.name = "localization-element"; var label = new Label(handle.Pack.Name); label.name = "localization-label"; var field = new TextField(); field.name = "localization-field"; field.value = handle.GetText(key); field.RegisterCallback <ChangeEvent <string> >((s) => { handle.SetText(key, s.newValue); }); field.RegisterCallback <FocusInEvent>((e) => { field.SetValueWithoutNotify(handle.GetText(key)); }); field.RegisterCallback <FocusOutEvent>((e) => { handle.SaveUnit(); }); var removeButton = new Button(); field.multiline = true; removeButton.clickable.clicked += () => { handle.RemoveKey(key); Refresh(); }; removeButton.name = "localization-expand"; removeButton.text = "Remove"; box.Add(label); box.Add(field); box.Add(removeButton); container.Add(box); }
private void ImportFromCsv() { string path = EditorUtility.OpenFilePanelWithFilters("Import csv...", "", new string[] { "CSV", "csv", "All Files", "*" }); if (!EditorUtility.DisplayDialog("Import CSV", "All packs included in the CSV will be possibly modified. If you want to prevent unexpected modifications, remove non-target columns in csv before you import csv. ", "Continue", "Cancel")) { return; } string csv = System.IO.File.ReadAllText(path, Encoding.GetEncoding("Shift_JIS")); csv = csv.Replace("\r\n", "\n"); int c = 0; int lineCount = 0; bool isInLiteral = false; while (c < csv.Length) { int index = csv.IndexOfAny(new char[] { '\n', '\"' }, c); if (index == -1) { break; } if (csv[index] == '\"') { isInLiteral = !isInLiteral; } else if (!isInLiteral && csv[index] == '\n') { lineCount++; } c = index + 1; } string[] lines = new string[lineCount]; isInLiteral = false; c = 0; int lastReturn = -1; int l = 0; while (c < csv.Length) { int index = csv.IndexOfAny(new char[] { '\n', '\"' }, c); if (index == -1) { break; } if (csv[index] == '\"') { isInLiteral = !isInLiteral; } else if (!isInLiteral && csv[index] == '\n') { lines[l] = csv.Substring(lastReturn + 1, index - lastReturn - 1); l++; lastReturn = index; } c = index + 1; } string[][] items = new string[lines.Length - 1][]; LocalizationUnitHandle[] handleCache = new LocalizationUnitHandle[lines.Length - 1]; string[] header = ParseCsvLine(lines[0]); for (int i = 1; i < lines.Length; i++) { items[i - 1] = ParseCsvLine(lines[i]); } LocalizationPack[] packs = new LocalizationPack[header.Length - 1]; for (int i = 1; i < header.Length; i++) { var pack = Localizer.Instance.Packs.FirstOrDefault(_ => _.Name == header[i]); packs[i - 1] = pack; } foreach (var item in items) { if (item.Length <= 1) { continue; } string keyPath = item[0]; string[] keyPathSplitted = keyPath.Split('/'); if (keyPathSplitted.Length != 3) { continue; } string group = keyPathSplitted[0]; string unit = keyPathSplitted[1]; string key = keyPathSplitted[2]; for (int packIndex = 0; packIndex < packs.Length; packIndex++) { if (packs[packIndex] == null) { continue; } var handle = packs[packIndex].RequestUnitHandle(group, unit, true); string text; if (packIndex + 1 >= item.Length) { text = ""; } else { text = item[packIndex + 1]; } if (!handle.ContainsKey(key)) { handle.AddKey(key); } handle.SetText(key, text); var old = handleCache[packIndex]; if (old != null && old.UnitName != handle.UnitName) { old.SaveUnit(); } handleCache[packIndex] = handle; if (old != null) { old.Dispose(); } } } for (int packIndex = 0; packIndex < packs.Length; packIndex++) { handleCache[packIndex]?.SaveUnit(); handleCache[packIndex]?.Dispose(); } }