private void txtTranslate_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { if (!string.IsNullOrEmpty(txtTranslate.Text) || !string.IsNullOrWhiteSpace(txtTranslate.Text)) { loadedSubs.Rows[currentDialog]["Translation"] = txtTranslate.Text.Trim(); unsavedSubs = true; } if (currentDialog != (loadedSubs.Rows.Count - 1)) { currentDialog++; UpdateCurrentDialog(loadedSubs, currentDialog, fileName); } else { string errorMsg = String.Format("This is the last subtitle in the file. There are not more subtitles availables.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "No more subtitles"; errorDialog.Message = errorMsg; errorDialog.Owner = this; errorDialog.Type = DialogWindow.InfoType; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } } }
private void Menu_Save(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(txtTranslate.Text) || string.IsNullOrWhiteSpace(txtTranslate.Text)) { loadedSubs.Rows[currentDialog]["Translation"] = txtTranslate.Text.Trim(); } string openFile = lblOpenFile.Content.ToString(); SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "Subtitle TranStation Project (*.tra)|*.tra"; saveDialog.AddExtension = true; saveDialog.FileName = Path.GetFileNameWithoutExtension(lblOpenFile.Content.ToString()); if (saveDialog.ShowDialog() == true) { if (SharedClasses.SaveProject(saveDialog.OpenFile(), subScript)) { string message = String.Format("The translation project has been saved successfully.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Exporting Subtitles"; errorDialog.Message = message; errorDialog.Type = DialogWindow.InfoType; errorDialog.Owner = this; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } else { string errorMsg = String.Format("An error ocurred while saving the translation project, please try again.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Exporting Subtitles"; errorDialog.Message = errorMsg; errorDialog.Type = DialogWindow.ErrorType; errorDialog.Owner = this; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } } }
public static DataSet CheckSubFile(string filePath) { string[] supportedSubs = { ".srt", ".ass", ".ssa", ".tra" }; string ext = Path.GetExtension(filePath); if (supportedSubs.Contains(ext)) { DataSet loadedSub = new DataSet(); switch (ext) { case ".tra": loadedSub = OpenSavedProject(filePath); break; case ".srt": var srtFile = File.ReadAllText(filePath); loadedSub = SubtitleParsers.ParseSubRip(srtFile); break; case ".ass": case ".ssa": var ssaFile = File.ReadAllText(filePath); loadedSub = SubtitleParsers.ParseSubStationAlpha(ssaFile); break; } return(loadedSub); } else { string fileName = Path.GetFileName(filePath); string errorMsg = String.Format("Subtitle TranStation could not open \"{0}\" because it is not a supported file type.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Error opening the file"; errorDialog.Message = errorMsg; errorDialog.Type = DialogWindow.ErrorType; errorDialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; errorDialog.Show(); return(null); } }
private void Menu_Export(object sender, RoutedEventArgs e) { string newFile = String.Empty; SaveFileDialog exportDialog = new SaveFileDialog(); exportDialog.Filter = "SubRip Subtitles (*.srt) | *.srt|SubStation Alpha Subtitles (*.ass)|*.ass"; exportDialog.AddExtension = true; exportDialog.FileName = Path.GetFileNameWithoutExtension(lblOpenFile.Content.ToString()); if (exportDialog.ShowDialog() == true) { try { SharedClasses.ExportTranslation(exportDialog.FileName, exportDialog.OpenFile(), subScript); string message = String.Format("The subtitles has been exported successfully.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Exporting Subtitles"; errorDialog.Message = message; errorDialog.Type = DialogWindow.InfoType; errorDialog.Owner = this; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } catch (Exception) { string errorMsg = String.Format("An error ocurred exporting the subtitles, please try again.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Exporting Subtitles"; errorDialog.Message = errorMsg; errorDialog.Type = DialogWindow.ErrorType; errorDialog.Owner = this; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } } }
private void ChangeDialogue(object sender, RoutedEventArgs e) { if (loadedSubs != null && loadedSubs.Rows.Count > 0) { if (!string.IsNullOrEmpty(txtTranslate.Text) || !string.IsNullOrWhiteSpace(txtTranslate.Text)) { loadedSubs.Rows[currentDialog]["Translation"] = txtTranslate.Text.Trim(); unsavedSubs = true; } Button btn = (Button)sender; switch (btn.Name) { case "btFst": currentDialog = 0; UpdateCurrentDialog(loadedSubs, currentDialog, fileName); break; case "btPrv": if (currentDialog != 0) { currentDialog--; UpdateCurrentDialog(loadedSubs, currentDialog, fileName); } else { string errorMsg = String.Format("This is the first subtitle in the file. There are no previous subtitles availables.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "No more subtitles"; errorDialog.Message = errorMsg; errorDialog.Type = DialogWindow.InfoType; errorDialog.Owner = this; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } break; case "btNxt": if (currentDialog != (loadedSubs.Rows.Count - 1)) { currentDialog++; UpdateCurrentDialog(loadedSubs, currentDialog, fileName); } else { string errorMsg = String.Format("This is the last subtitle in the file. There are not more subtitles availables.", fileName); DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "No more subtitles"; errorDialog.Message = errorMsg; errorDialog.Owner = this; errorDialog.Type = DialogWindow.InfoType; errorDialog.Width = 400; errorDialog.Height = 120; errorDialog.Show(); } break; case "btLst": currentDialog = loadedSubs.Rows.Count - 1; UpdateCurrentDialog(loadedSubs, currentDialog, fileName); break; case "btGoogle": string message = "This will generate a suggested translation for the current subtitle using the Google Translate service that it may not be accurate."; DialogWindow dialog = new DialogWindow(); dialog.DialogTitle = "Suggested Translation"; dialog.Message = message; dialog.Type = DialogWindow.GoogleType; dialog.Subtitle = loadedSubs.Rows[currentDialog]["Text"].ToString(); dialog.Owner = this; if (dialog.ShowDialog() == true) { string sugestion = dialog.SuggestedSub; txtTranslate.Text = sugestion; dialog.Close(); } break; } } }
public static DataSet ParseSubStationAlpha(string subFile) { string[] neededSections = { "[Script Info]", "[V4+ Styles]", "[V4 Styles]", "[Events]" }; DataSet subScript = new DataSet(); DataTable info = new DataTable(); DataTable styles = new DataTable(); DataTable events = new DataTable(); try { string[] sections = Regex.Split(subFile, "\r\n\r\n"); if (sections.Length < 2) { sections = Regex.Split(subFile, "\n\n"); } foreach (string section in sections) { string[] elements = section.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (neededSections.Contains(elements[0])) { switch (elements[0]) { case "[Script Info]": if (info.Rows.Count > 0) { throw new Exception(); } for (int i = 1; i < elements.Length; i++) { if (elements[i].StartsWith(";")) { continue; } string[] keys = elements[i].Split(':'); DataColumn column = new DataColumn(keys[0].Trim()); column.DefaultValue = keys[1].Trim(); info.Columns.Add(column); } DataRow infoRow = info.NewRow(); info.Rows.Add(infoRow); break; case "[V4+ Styles]": case "[V4 Styles]": for (int i = 1; i < elements.Length; i++) { if (elements[i].StartsWith(";")) { continue; } string[] keys = elements[i].Split(':'); int fCount = 0; switch (keys[0]) { case "Format": if (fCount > 0) { throw new Exception(); } string[] names = keys[1].Split(','); foreach (string name in names) { styles.Columns.Add(name.Trim()); } fCount++; break; case "Style": string[] values = keys[1].Trim().Split(','); styles.Rows.Add(values); break; } } break; case "[Events]": for (int i = 1; i < elements.Length; i++) { if (elements[i].StartsWith(";")) { continue; } string[] keys = Regex.Split(elements[i], ": "); int fCount = 0; switch (keys[0]) { case "Format": if (fCount > 0) { throw new Exception(); } string[] names = keys[1].Trim().Split(','); foreach (string name in names) { events.Columns.Add(name.Trim()); } fCount++; break; case "Dialogue": case "Comment": string[] values = keys[1].Split(','); DataRow eventRow = events.NewRow(); for (int j = 0; j < 10; j++) { if (j == 9) { string text = string.Join(", ", values.Skip(9)).Replace("\\N", " \\N "); eventRow[j] = text; continue; } eventRow[j] = values[j]; } events.Rows.Add(eventRow); break; } } break; } } } if (!SubStationAlphaVerification(styles, events)) { throw new Exception(); } info.TableName = "Info"; styles.TableName = "Style"; events.Columns.Add("Translation"); events.TableName = "Dialogue"; subScript.Tables.Add(info); subScript.Tables.Add(styles); subScript.Tables.Add(events); return(subScript); } catch (Exception) { string errorMsg = @"This is not a valid SubStation Alpha file. Please try again."; DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Incorrect subtitle format"; errorDialog.Message = errorMsg; errorDialog.Type = DialogWindow.ErrorType; errorDialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; errorDialog.Show(); return(null); } }
public static DataSet ParseSubRip(string subFile) { try { DataTable loadedSubs = new DataTable(); loadedSubs.Columns.Add("Start"); loadedSubs.Columns.Add("End"); loadedSubs.Columns.Add("Text"); loadedSubs.Columns.Add("Translation"); loadedSubs.Columns["Translation"].DefaultValue = String.Empty; var lines = Regex.Split(subFile, "\r\n\r\n"); if (lines.Length < 2) { lines = Regex.Split(subFile, "\n\n"); } foreach (string line in lines) { if (string.IsNullOrWhiteSpace(line)) { continue; } string subPart = line; while (subPart.IndexOf("\n") < 1) { subPart = subPart.Remove(0, 1); } string[] elements = Regex.Split(subPart, "\n"); int x; bool isNumeric = Int32.TryParse(elements[0], out x); if (!isNumeric) { throw new Exception(); } DataRow dialog = loadedSubs.NewRow(); string[] delimiters = { "-->", "- >", "->" }; string[] times = elements[1].Split(delimiters, StringSplitOptions.None); if (ValidateTimeCodes(times)) { throw new Exception(); } dialog[0] = times[0].Trim(); dialog[1] = times[1].Trim(); string text = ""; for (int i = 2; i < elements.Length; i++) { text += elements[i].Trim() + "<br />"; } text = text.Remove(text.Length - 6); dialog[2] = text; loadedSubs.Rows.Add(dialog); } if (loadedSubs.Rows.Count == 0) { throw new Exception(); } loadedSubs.TableName = "Dialogue"; subScript.Tables.Add(loadedSubs); return(subScript); } catch (Exception) { string errorMsg = @"This is not a valid SubRip file. Please try again."; DialogWindow errorDialog = new DialogWindow(); errorDialog.DialogTitle = "Incorrect subtitle format"; errorDialog.Message = errorMsg; errorDialog.Type = DialogWindow.ErrorType; errorDialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; errorDialog.Show(); return(null); } }