diff --git a/JsonToolsNppPlugin/Forms/GrepperForm.cs b/JsonToolsNppPlugin/Forms/GrepperForm.cs index 7c9160d..01cbe0c 100644 --- a/JsonToolsNppPlugin/Forms/GrepperForm.cs +++ b/JsonToolsNppPlugin/Forms/GrepperForm.cs @@ -28,6 +28,7 @@ public partial class GrepperForm : Form //private Button progressBarCancelButton; private Label progressLabel; private static object progressReportLock = new object(); + private static Dictionary progressBarTranslatedStrings = null; public GrepperForm() { @@ -96,7 +97,7 @@ private async void SendRequestsButton_Click(object sender, EventArgs e) } catch (Exception ex) { - Translator.ShowTranslatedMessageBox(ex.ToString(), + Translator.ShowTranslatedMessageBox(ex.ToString(), "Error while sending API requests", MessageBoxButtons.OK, MessageBoxIcon.Error @@ -174,13 +175,39 @@ private void CreateProgressReportBuffer(int totalLengthToParse, long totalLength string totLengthToParseMB = (totalLengthToParse / 1e6).ToString("F3", JNode.DOT_DECIMAL_SEP); string totLengthOnHardDriveMB = (totalLengthOnHardDrive / 1e6).ToString("F3", JNode.DOT_DECIMAL_SEP); isParsing = totalLengthOnHardDrive == -1; + string titleIfParsing = "JSON parsing in progress"; + string titleIfReading = "File reading in progress"; + string captionIfParsing = "File reading complete.\r\nNow parsing {0} documents with combined length of about {1} MB"; + string captionIfReading = "Now reading {0} files with a combined length of about {1} MB"; + string progressLabelIfParsing = "0 MB of {0} MB parsed"; + string progressLabelIfReading = "0 of {0} files read"; + if (progressBarTranslatedStrings is null) + { + progressBarTranslatedStrings = new Dictionary + { + ["titleIfParsing"] = titleIfParsing, + ["titleIfReading"] = titleIfReading, + ["captionIfParsing"] = captionIfParsing, + ["captionIfReading"] = captionIfReading, + ["progressLabelIfParsing"] = progressLabelIfParsing, + ["progressLabelIfReading"] = progressLabelIfReading, + }; + string[] keys = progressBarTranslatedStrings.Keys.ToArray(); + if (Translator.TryGetTranslationAtPath(new string[] {"forms", "GrepperFormProgressBar", "controls"}, out JNode progressBarTransNode) && progressBarTransNode is JObject progressBarTrans) + { + foreach (string key in keys) + { + if (progressBarTrans.TryGetValue(key, out JNode val) && val.value is string s) + progressBarTranslatedStrings[key] = s; + } + } + } Label label = new Label { - Name = "title", - Text = isParsing - ? "All JSON documents have been read into memory.\r\n" + - $"Now parsing {grepper.fnameStrings.Count} documents with combined length of about {totLengthToParseMB} MB" - : $"Reading {totalLengthToParse} documents with a combined length of about {totLengthOnHardDriveMB} MB", + Name = "caption", + Text = isParsing + ? Translator.TryTranslateWithFormatting(captionIfParsing, progressBarTranslatedStrings["captionIfParsing"], grepper.fnameStrings.Count, totLengthToParseMB) + : Translator.TryTranslateWithFormatting(captionIfReading, progressBarTranslatedStrings["captionIfReading"], totalLengthToParse, totLengthOnHardDriveMB), TextAlign = ContentAlignment.TopCenter, Top = 20, AutoSize = true, @@ -188,7 +215,9 @@ private void CreateProgressReportBuffer(int totalLengthToParse, long totalLength progressLabel = new Label { Name = "progressLabel", - Text = isParsing ? $"0 MB of {totLengthToParseMB} MB parsed" : $"0 of {totalLengthToParse} files read", + Text = isParsing + ? Translator.TryTranslateWithFormatting(progressLabelIfParsing, progressBarTranslatedStrings["progressLabelIfParsing"], totLengthToParseMB) + : Translator.TryTranslateWithFormatting(progressLabelIfReading, progressBarTranslatedStrings["progressLabelIfReading"], totalLengthToParse), TextAlign = ContentAlignment.TopCenter, Top = 100, AutoSize = true, @@ -218,11 +247,18 @@ private void CreateProgressReportBuffer(int totalLengthToParse, long totalLength progressBarForm = new Form { - Text = isParsing ? "JSON parsing in progress" : "File reading in progress", + Name = "GrepperFormProgressBar", + Text = isParsing ? progressBarTranslatedStrings["titleIfParsing"] : progressBarTranslatedStrings["titleIfReading"], Controls = { label, progressLabel, progressBar }, Width = 500, Height = 300, }; + if (label.Right > progressBarForm.Width) + { + progressBarForm.SuspendLayout(); + progressBarForm.Width = label.Right + 20; + progressBarForm.ResumeLayout(false); + } progressBarForm.Show(); } @@ -335,7 +371,10 @@ private void ViewResultsButton_Click(object sender, EventArgs e) } tv = new TreeViewer(grepper.fnameJsons); AddOwnedForm(tv); - Main.DisplayJsonTree(tv, tv.json, "JSON from files and APIs tree", false, DocumentType.JSON, false); + string treeName = "JSON from files and APIs tree"; + if (Translator.TryGetTranslationAtPath(new string[] { "forms", "TreeViewer", "titleIfGrepperForm" }, out JNode node) && node.value is string s) + treeName = s; + Main.DisplayJsonTree(tv, tv.json, treeName, false, DocumentType.JSON, false); if (Main.openTreeViewer != null && !Main.openTreeViewer.IsDisposed) Npp.notepad.HideDockingForm(Main.openTreeViewer); } diff --git a/JsonToolsNppPlugin/Main.cs b/JsonToolsNppPlugin/Main.cs index 05d521c..b1ae171 100644 --- a/JsonToolsNppPlugin/Main.cs +++ b/JsonToolsNppPlugin/Main.cs @@ -1562,7 +1562,14 @@ public static void OpenJsonTree(DocumentType documentType = DocumentType.JSON) openTreeViewer = new TreeViewer(json); info.tv = openTreeViewer; jsonFileInfos[activeFname] = info; - DisplayJsonTree(openTreeViewer, json, $"Json Tree View for {openTreeViewer.RelativeFilename()}", usesSelections, documentType, parserState == ParserState.FATAL); + DisplayJsonTree(openTreeViewer, json, GetNameForJsonTree(openTreeViewer), usesSelections, documentType, parserState == ParserState.FATAL); + } + + private static string GetNameForJsonTree(TreeViewer tv) + { + string defaultNameFormat = "Json Tree View for {0}"; + string nameFormat = (Translator.TryGetTranslationAtPath(new string[] { "forms", "TreeViewer", "title" }, out JNode node) && node.value is string s) ? s : defaultNameFormat; + return Translator.TryTranslateWithFormatting(defaultNameFormat, nameFormat, tv.RelativeFilename()); } static void OpenGrepperForm() diff --git a/JsonToolsNppPlugin/Properties/AssemblyInfo.cs b/JsonToolsNppPlugin/Properties/AssemblyInfo.cs index bbb62a7..d68a650 100644 --- a/JsonToolsNppPlugin/Properties/AssemblyInfo.cs +++ b/JsonToolsNppPlugin/Properties/AssemblyInfo.cs @@ -28,5 +28,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("8.0.0.14")] -[assembly: AssemblyFileVersion("8.0.0.14")] +[assembly: AssemblyVersion("8.0.0.15")] +[assembly: AssemblyFileVersion("8.0.0.15")] diff --git a/JsonToolsNppPlugin/Utils/Translator.cs b/JsonToolsNppPlugin/Utils/Translator.cs index df888d0..0273379 100644 --- a/JsonToolsNppPlugin/Utils/Translator.cs +++ b/JsonToolsNppPlugin/Utils/Translator.cs @@ -108,6 +108,48 @@ private static bool TryGetTranslationFileName(string langName, out string transl return true; } + /// + /// Find the string at the end of a sequence of keys in translations, returning false if no such string was found.

+ /// For example:

+ /// + /// Suppose translations is + /// {"foo": {"bar": "1", "rnq": "2"}, "quz": "3"} + /// - TryGetTranslationAtPath(["foo", "bar"], out JNode result) would set result to "1" and return true

+ /// - TryGetTranslationAtPath(["foo", "rnq"], out JNode result) would set result to "2" and return true

+ /// - TryGetTranslationAtPath(["blah", "rnq"], out JNode result) would set result to null and return false

+ /// - TryGetTranslationAtPath(["foo", "rnq", "b"], out JNode result) would set result to null and return false

+ /// - TryGetTranslationAtPath(["foo", "b"], out JNode result) would set result to null and return false

+ /// - TryGetTranslationAtPath(["quz"], out JNode result) would set result to "3" and return true

+ /// - TryGetTranslationAtPath(["foo"], out JNode result) would set result to {"bar": "1", "rnq": "2"} and return true

+ ///
+ ///
+ /// + /// + /// + public static bool TryGetTranslationAtPath(string[] pathToTrans, out JNode result) + { + result = null; + if (!(translations is JObject trans) || pathToTrans.Length == 0) + return false; + int pathLen = pathToTrans.Length; + JNode child = new JNode(); + for (int ii = 0; ii < pathLen; ii++) + { + string key = pathToTrans[ii]; + if (!trans.TryGetValue(key, out child)) + return false; + if (ii < pathLen - 1) + { + if (child is JObject childObj) + trans = childObj; + else + return false; + } + } + result = child; + return true; + } + public static string GetTranslatedMenuItem(string menuItem) { if (translations is JObject jobj @@ -167,7 +209,7 @@ public static DialogResult ShowTranslatedMessageBox(string text, string caption, return MessageBox.Show(formattedText, formattedCaption, buttons, icon); } - private static string TryTranslateWithFormatting(string untranslated, string translated, params object[] formatParams) + public static string TryTranslateWithFormatting(string untranslated, string translated, params object[] formatParams) { try { diff --git a/translation/english.json5 b/translation/english.json5 index 1fcb21e..330463a 100644 --- a/translation/english.json5 +++ b/translation/english.json5 @@ -118,6 +118,16 @@ "RemoveSelectedFilesButton": "Remove selected files" } }, + "GrepperFormProgressBar": { + "controls": { + "titleIfParsing": "JSON parsing in progress", + "titleIfReading": "File reading in progress", + "captionIfParsing": "File reading complete.\r\nNow parsing {0} documents with combined length of about {1} MB", + "captionIfReading": "Now reading {0} files with a combined length of about {1} MB", + "progressLabelIfParsing": "0 MB of {0} MB parsed", + "progressLabelIfReading": "0 of {0} files read" + } + }, "JsonToCsvForm": { "title": "JSON to CSV", "controls": { @@ -187,8 +197,9 @@ } }, "TreeViewer": { - // the title of this form is programmatically generated - // so we can't translate it. + "title": "Json Tree View for {0}", + // this is the title for the GrepperForm's tree view + "titleIfGrepperForm": "JSON from files and APIs tree", "controls": { "SubmitQueryButton": "Submit query", "QueryToCsvButton": "Query to CSV", diff --git a/translation/italian.json5 b/translation/italian.json5 index a95df20..cbcfbf1 100644 --- a/translation/italian.json5 +++ b/translation/italian.json5 @@ -118,6 +118,16 @@ "RemoveSelectedFilesButton": "Rimuovi i file selezionati" } }, + "GrepperFormProgressBar": { + "controls": { + "titleIfParsing": "Analisi di JSON in corso", + "titleIfReading": "Lettura dei file in corso", + "captionIfParsing": "Lettura dei file completata.\r\nOra sta analizzando {0} documenti con una lunghezza complessiva di circa {1} MB", + "captionIfReading": "Ora sta leggendo {0} file con una lunghezza complessiva di cira {1} MB", + "progressLabelIfParsing": "0 MB di {0} MB analizzati", + "progressLabelIfReading": "0 di {0} file letti" + } + }, "JsonToCsvForm": { "title": "Da JSON a CSV", "controls": { @@ -187,8 +197,9 @@ } }, "TreeViewer": { - // the title of this form is programmatically generated - // so we can't translate it. + "title": "Visualizzazione struttura JSON per {0}", + // this is the title for the GrepperForm's tree view + "titleIfGrepperForm": "Visualizzazione struttura JSON per \"JSON da file e API\"", "controls": { "SubmitQueryButton": "Invia query", "QueryToCsvButton": "Da Query a CSV",