Skip to content

Commit

Permalink
translation of tree title, grepper progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
molsonkiko committed Jul 23, 2024
1 parent 1e4dfe6 commit 4b9f15f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 17 deletions.
57 changes: 48 additions & 9 deletions JsonToolsNppPlugin/Forms/GrepperForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class GrepperForm : Form
//private Button progressBarCancelButton;
private Label progressLabel;
private static object progressReportLock = new object();
private static Dictionary<string, string> progressBarTranslatedStrings = null;

public GrepperForm()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -174,21 +175,49 @@ 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<string, string>
{
["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,
};
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,
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion JsonToolsNppPlugin/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
44 changes: 43 additions & 1 deletion JsonToolsNppPlugin/Utils/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,48 @@ private static bool TryGetTranslationFileName(string langName, out string transl
return true;
}

/// <summary>
/// Find the string at the end of a sequence of keys in translations, returning false if no such string was found.<br></br>
/// For example:<br></br>
/// <example>
/// Suppose translations is
/// <code>{"foo": {"bar": "1", "rnq": "2"}, "quz": "3"}</code>
/// - TryGetTranslationAtPath(["foo", "bar"], out JNode result) would set result to "1" and return <i>true</i><br></br>
/// - TryGetTranslationAtPath(["foo", "rnq"], out JNode result) would set result to "2" and return <i>true</i><br></br>
/// - TryGetTranslationAtPath(["blah", "rnq"], out JNode result) would set result to null and return <i>false</i><br></br>
/// - TryGetTranslationAtPath(["foo", "rnq", "b"], out JNode result) would set result to null and return <i>false</i><br></br>
/// - TryGetTranslationAtPath(["foo", "b"], out JNode result) would set result to null and return <i>false</i><br></br>
/// - TryGetTranslationAtPath(["quz"], out JNode result) would set result to "3" and return <i>true</i><br></br>
/// - TryGetTranslationAtPath(["foo"], out JNode result) would set result to {"bar": "1", "rnq": "2"} and return <i>true</i><br></br>
/// </example>
/// </summary>
/// <param name="pathToTrans"></param>
/// <param name="result"></param>
/// <returns></returns>
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
Expand Down Expand Up @@ -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
{
Expand Down
15 changes: 13 additions & 2 deletions translation/english.json5
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 13 additions & 2 deletions translation/italian.json5
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4b9f15f

Please sign in to comment.