private void saveChangesToFile(TranslatedString singleChange = null) { tstProgress.Value = 0; tstLblInfo.Text = $"Saving"; int total = 0; List <TranslatedString> changes = new List <TranslatedString>(); if (singleChange != null) { changes.Add(singleChange); } else { if (lstDest.Items.Count > 0) { changes.AddRange(lstDest.Items.Cast <TranslatedString>()); } } Dictionary <string, List <TranslatedString> > changesPerFile = new Dictionary <string, List <TranslatedString> >(); foreach (TranslatedString str in changes) { if (!changesPerFile.ContainsKey(str.File)) { changesPerFile.Add(str.File, (new TranslatedString[] { str }).ToList()); } else { changesPerFile[str.File].Add(str); } total++; } int cpt; foreach (List <TranslatedString> diffs in changesPerFile.Values) { cpt = 0; string currFile = diffs[0].File; string[] file = currFile.Split('.'); string newFile = $"{file[0] + NEW_FILE_SUFFIX}.{file[1]}"; string newText = File.ReadAllText(currFile, DEFAULT); foreach (TranslatedString str in diffs) { newText = newText.Replace(str.OriginalText, str.Text); cpt++; } File.WriteAllText(newFile, newText, DEFAULT); tstProgress.Value = Convert.ToInt32((diffs.Count / (float)total) * 100); tstLblInfo.Text = $"Saved {cpt}/{total}"; } tstProgress.Value = 100; tstLblInfo.Text = $"Done saving"; }
private void btnTranslateSingle_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; tstProgress.Value = 0; tstLblInfo.Text = "Translating"; TranslatedString result = GetTranslatedStringFromSourceIndex(lstSource.SelectedIndex)[0]; lstDest.Items.Add(result); tstProgress.Value = 50; tstLblInfo.Text = "Saving"; saveChangesToFile(result); tstProgress.Value = 100; tstLblInfo.Text = "Save complete"; Cursor.Current = Cursors.Default; }