private void Translate_Batch_Clicked(object sender, RoutedEventArgs e) { if (!(TranslateData.DataContext is Project.TranslationPair[] currentPairs)) { MessageBox.Show(this, "No section is open", "Turansuraetu", MessageBoxButton.OK, MessageBoxImage.Error); return; } ProgressDisplay disp = new ProgressDisplay(); List <ITranslateService> activeServices = ITranslateService.TranslationServices.Where(x => x.IsActive(this)).ToList(); bool overwriteTranslations = DoOverwrite.IsChecked; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (o, args) => { try { foreach (ITranslateService service in activeServices) { int i = 1; foreach (Project.TranslationPair pair in currentPairs) { disp.Update($"{service.GetType().Name} - {i}/{currentPairs.Length}", (double)i / currentPairs.Length); try { service.Translate(Translate.Language.Japanese, Translate.Language.English, pair.Original, ref pair.machine, overwriteTranslations); } catch (Exception e) { Dispatcher.Invoke(() => { MessageBox.Show( $"An error has occured whilst translating with {service.GetType().Name}: {e.GetType().Name} {e.Message}", "Turansuraetu", MessageBoxButton.OK, MessageBoxImage.Stop); }); } i++; } } } finally { disp.Done(); } }; worker.RunWorkerCompleted += (o, args) => { RefreshTable(); }; worker.RunWorkerAsync(); disp.ShowDialog(); }
public static Task <Project> Open(MainWindow window, string file) { file = Path.GetFullPath(file); { // Validate file string header = File.ReadAllLines(file).SingleOrDefault() ?? ""; if (!header.Trim().Equals("> RPGMAKER TRANS PATCH V3")) { MessageBox.Show(window, "Cannot open project, header file is invalid.", "Turansuraetu", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } } ProgressDisplay progress = new ProgressDisplay(); Task <Project> worker = new Task <Project>(() => Open_Do(file, progress)); worker.Start(); progress.ShowDialog(); return(worker); }
private static Project Open_Do(string file, ProgressDisplay progress) { string machinePath = Path.Combine(Path.GetDirectoryName(file) ?? "", "TransuraetuMachine.json"); progress.Update("Loading machine translations...", .0); Dictionary <string, Dictionary <string, TranslationPair.MachineTranslations> > machineTranslations; if (File.Exists(machinePath)) { machineTranslations = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, TranslationPair.MachineTranslations> > >( File.ReadAllText(machinePath)); } else { machineTranslations = new Dictionary <string, Dictionary <string, TranslationPair.MachineTranslations> >(); } progress.Update("Listing files...", .0); Project project = new Project(file); string dir = Path.Combine(Path.GetDirectoryName(file) ?? "", "patch"); string[] files = Directory.GetFiles(dir); int i = 1; // Used purely for progress bar foreach (string f in files) { string fn = Path.GetFileName(f); Dictionary <string, TranslationPair.MachineTranslations> machineTrs; if (!machineTranslations.TryGetValue(fn, out machineTrs)) { machineTrs = new Dictionary <string, TranslationPair.MachineTranslations>(); } progress.Update($"Reading file {fn} ({i}/{f.Length})", (double)i / f.Length); i++; string[] lines = File.ReadAllLines(f); if (!(lines.FirstOrDefault() ?? "").StartsWith("> RPGMAKER TRANS PATCH FILE VERSION 3")) { continue; } string header = lines.First(); lines = lines.Skip(1).ToArray(); List <TranslationPair> pairs = new List <TranslationPair>(); string original = ""; string translation = ""; List <string> context = new List <string>(); bool isTranslation = false; foreach (string line in lines) { if (line.StartsWith('>')) { if (line.StartsWith("> BEGIN STRING")) { isTranslation = false; original = ""; translation = ""; } else if (line.StartsWith("> CONTEXT:")) { isTranslation = true; context.Add(line); } else if (line.StartsWith("> END STRING")) { isTranslation = false; if (!string.IsNullOrWhiteSpace(original?.Trim())) { TranslationPair pair = new TranslationPair(original, translation, context.ToArray()); machineTrs.TryGetValue(pair.Original, out pair.machine); pairs.Add(pair); } original = ""; translation = ""; context.Clear(); } } else { ref string current = ref original; if (isTranslation) { current = ref translation; } if (!string.IsNullOrWhiteSpace(current)) { current += '\n'; } current += line; } } if (pairs.Count > 0) { project.patchFiles.Add(fn, new PatchFile { name = fn, header = header, pairs = pairs.ToArray() }); } }