Make export custom text more logical - thx TL-aalive :)

Fix #5941
This commit is contained in:
niksedk 2022-05-09 07:32:37 +02:00
parent 81b80cda7d
commit e8848868c2
3 changed files with 37 additions and 44 deletions

View File

@ -12,7 +12,7 @@ namespace Nikse.SubtitleEdit.Forms
{
private readonly List<string> _templates = new List<string>();
private readonly Subtitle _subtitle;
private readonly Subtitle _translated;
private readonly Subtitle _original;
private readonly string _title;
private bool _batchConvert;
public string LogMessage { get; set; }
@ -24,15 +24,8 @@ namespace Nikse.SubtitleEdit.Forms
InitializeComponent();
UiUtil.FixFonts(this);
if (original?.Paragraphs == null || original.Paragraphs.Count == 0)
{
_subtitle = subtitle;
}
else
{
_subtitle = original;
_translated = subtitle;
}
_subtitle = subtitle;
_original = original;
_title = title;
@ -214,15 +207,15 @@ namespace Nikse.SubtitleEdit.Forms
saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(_title) + fileExt;
}
saveFileDialog1.Filter =
saveFileDialog1.Filter =
fileExt.TrimStart('.') + "|*" + fileExt + "|" +
LanguageSettings.Current.General.AllFiles + "|*.*";
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
FileUtil.WriteAllText(saveFileDialog1.FileName, GenerateText(_subtitle, _translated, _title), GetCurrentEncoding());
FileUtil.WriteAllText(saveFileDialog1.FileName, GenerateText(_subtitle, _original, _title), GetCurrentEncoding());
LogMessage = string.Format(LanguageSettings.Current.ExportCustomText.SubtitleExportedInCustomFormatToX, saveFileDialog1.FileName);
DialogResult = DialogResult.OK;
}
@ -233,7 +226,7 @@ namespace Nikse.SubtitleEdit.Forms
}
}
private string GenerateText(Subtitle subtitle, Subtitle translation, string title)
private string GenerateText(Subtitle subtitle, Subtitle original, string title)
{
if (listViewTemplates.SelectedItems.Count != 1)
{
@ -245,7 +238,7 @@ namespace Nikse.SubtitleEdit.Forms
try
{
int idx = listViewTemplates.SelectedItems[0].Index;
return GenerateCustomText(subtitle, translation, title, _templates[idx]);
return GenerateCustomText(subtitle, original, title, _templates[idx]);
}
catch (Exception exception)
{
@ -275,7 +268,7 @@ namespace Nikse.SubtitleEdit.Forms
return ".txt";
}
internal static string GenerateCustomText(Subtitle subtitle, Subtitle translation, string title, string templateString)
internal static string GenerateCustomText(Subtitle subtitle, Subtitle original, string title, string templateString)
{
var arr = templateString.Split('Æ');
var sb = new StringBuilder();
@ -297,16 +290,16 @@ namespace Nikse.SubtitleEdit.Forms
}
text = ExportCustomTextFormat.GetText(text, arr[4]);
string translationText = string.Empty;
if (translation?.Paragraphs != null && translation.Paragraphs.Count > 0)
string originalText = string.Empty;
if (original?.Paragraphs != null && original.Paragraphs.Count > 0)
{
var trans = Utilities.GetOriginalParagraph(i, p, translation.Paragraphs);
if (trans != null)
var originalParagraph = Utilities.GetOriginalParagraph(i, p, original.Paragraphs);
if (originalParagraph != null)
{
translationText = trans.Text;
originalText = originalParagraph.Text;
}
}
string paragraph = ExportCustomTextFormat.GetParagraph(template, start, end, text, translationText, i, p.Actor, p.Duration, arr[3], p);
string paragraph = ExportCustomTextFormat.GetParagraph(template, start, end, text, originalText, i, p.Actor, p.Duration, arr[3], p);
sb.Append(paragraph);
}
sb.Append(ExportCustomTextFormat.GetHeaderOrFooter(title, subtitle, arr[5]));
@ -315,7 +308,7 @@ namespace Nikse.SubtitleEdit.Forms
private void listViewTemplates_SelectedIndexChanged(object sender, EventArgs e)
{
textBoxPreview.Text = GenerateText(_subtitle, _translated, _title);
textBoxPreview.Text = GenerateText(_subtitle, _original, _title);
buttonSave.Enabled = listViewTemplates.SelectedItems.Count == 1;
}

View File

@ -286,118 +286,118 @@
this.textlengthbr1ToolStripMenuItem,
this.textlengthbr2ToolStripMenuItem});
this.contextMenuStripParagraph.Name = "contextMenuStrip1";
this.contextMenuStripParagraph.Size = new System.Drawing.Size(164, 356);
this.contextMenuStripParagraph.Size = new System.Drawing.Size(181, 378);
this.contextMenuStripParagraph.Text = "{text-length-br1}";
//
// insertHHMMSSMSToolStripMenuItem
//
this.insertHHMMSSMSToolStripMenuItem.Name = "insertHHMMSSMSToolStripMenuItem";
this.insertHHMMSSMSToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.insertHHMMSSMSToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.insertHHMMSSMSToolStripMenuItem.Text = "{start}";
this.insertHHMMSSMSToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// insertendToolStripMenuItem
//
this.insertendToolStripMenuItem.Name = "insertendToolStripMenuItem";
this.insertendToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.insertendToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.insertendToolStripMenuItem.Text = "{end}";
this.insertendToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// insertnumberToolStripMenuItem
//
this.insertnumberToolStripMenuItem.Name = "insertnumberToolStripMenuItem";
this.insertnumberToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.insertnumberToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.insertnumberToolStripMenuItem.Text = "{number}";
this.insertnumberToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// insertdurationToolStripMenuItem
//
this.insertdurationToolStripMenuItem.Name = "insertdurationToolStripMenuItem";
this.insertdurationToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.insertdurationToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.insertdurationToolStripMenuItem.Text = "{duration}";
this.insertdurationToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// textToolStripMenuItem
//
this.textToolStripMenuItem.Name = "textToolStripMenuItem";
this.textToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.textToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.textToolStripMenuItem.Text = "{text}";
this.textToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// translationToolStripMenuItem
//
this.translationToolStripMenuItem.Name = "translationToolStripMenuItem";
this.translationToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.translationToolStripMenuItem.Text = "{translation}";
this.translationToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.translationToolStripMenuItem.Text = "{original-text}";
this.translationToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// toolStripMenuItemActor
//
this.toolStripMenuItemActor.Name = "toolStripMenuItemActor";
this.toolStripMenuItemActor.Size = new System.Drawing.Size(163, 22);
this.toolStripMenuItemActor.Size = new System.Drawing.Size(180, 22);
this.toolStripMenuItemActor.Text = "{actor}";
this.toolStripMenuItemActor.Click += new System.EventHandler(this.InsertTag);
//
// tabToolStripMenuItem
//
this.tabToolStripMenuItem.Name = "tabToolStripMenuItem";
this.tabToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.tabToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.tabToolStripMenuItem.Text = "{tab}";
this.tabToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// textToolStripMenuItem1
//
this.textToolStripMenuItem1.Name = "textToolStripMenuItem1";
this.textToolStripMenuItem1.Size = new System.Drawing.Size(163, 22);
this.textToolStripMenuItem1.Size = new System.Drawing.Size(180, 22);
this.textToolStripMenuItem1.Text = "{text-line-1}";
this.textToolStripMenuItem1.Click += new System.EventHandler(this.InsertTag);
//
// textline2ToolStripMenuItem
//
this.textline2ToolStripMenuItem.Name = "textline2ToolStripMenuItem";
this.textline2ToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.textline2ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.textline2ToolStripMenuItem.Text = "{text-line-2}";
this.textline2ToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// cpsperiodToolStripMenuItem
//
this.cpsperiodToolStripMenuItem.Name = "cpsperiodToolStripMenuItem";
this.cpsperiodToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.cpsperiodToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.cpsperiodToolStripMenuItem.Text = "{cps-period}";
this.cpsperiodToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// cpsToolStripMenuItem
//
this.cpsToolStripMenuItem.Name = "cpsToolStripMenuItem";
this.cpsToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.cpsToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.cpsToolStripMenuItem.Text = "{cps-comma}";
this.cpsToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// textlengthToolStripMenuItem
//
this.textlengthToolStripMenuItem.Name = "textlengthToolStripMenuItem";
this.textlengthToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.textlengthToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.textlengthToolStripMenuItem.Text = "{text-length}";
this.textlengthToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// textlengthnobrToolStripMenuItem
//
this.textlengthnobrToolStripMenuItem.Name = "textlengthnobrToolStripMenuItem";
this.textlengthnobrToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.textlengthnobrToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.textlengthnobrToolStripMenuItem.Text = "{text-length-br0}";
this.textlengthnobrToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// textlengthbr1ToolStripMenuItem
//
this.textlengthbr1ToolStripMenuItem.Name = "textlengthbr1ToolStripMenuItem";
this.textlengthbr1ToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.textlengthbr1ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.textlengthbr1ToolStripMenuItem.Text = "{text-length-br1}";
this.textlengthbr1ToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//
// textlengthbr2ToolStripMenuItem
//
this.textlengthbr2ToolStripMenuItem.Name = "textlengthbr2ToolStripMenuItem";
this.textlengthbr2ToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
this.textlengthbr2ToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.textlengthbr2ToolStripMenuItem.Text = "{text-length-br2}";
this.textlengthbr2ToolStripMenuItem.Click += new System.EventHandler(this.InsertTag);
//

View File

@ -117,7 +117,7 @@ namespace Nikse.SubtitleEdit.Forms
var s = template.Replace("{start}", "{0}");
s = s.Replace("{end}", "{1}");
s = s.Replace("{text}", "{2}");
s = s.Replace("{translation}", "{3}");
s = s.Replace("{original-text}", "{3}");
s = s.Replace("{number}", "{4}");
s = s.Replace("{number:", "{4:");
s = s.Replace("{number-1}", "{5}");
@ -324,7 +324,7 @@ namespace Nikse.SubtitleEdit.Forms
return template;
}
internal static string GetParagraph(string template, string start, string end, string text, string translation, int number, string actor, TimeCode duration, string timeCodeTemplate, Paragraph p)
internal static string GetParagraph(string template, string start, string end, string text, string originalText, int number, string actor, TimeCode duration, string timeCodeTemplate, Paragraph p)
{
var cps = Utilities.GetCharactersPerSecond(p);
var d = duration.ToString();
@ -409,7 +409,7 @@ namespace Nikse.SubtitleEdit.Forms
var replaceEnd = GetReplaceChar(s + replaceStart);
s = PreBeginCurly(s, replaceStart);
s = PreEndCurly(s, replaceEnd);
s = string.Format(s, start, end, text, translation, number + 1, number, d, actor, line1, line2,
s = string.Format(s, start, end, text, originalText, number + 1, number, d, actor, line1, line2,
cps.ToString(CultureInfo.InvariantCulture).Replace(".", ","),
cps.ToString(CultureInfo.InvariantCulture),
text.Length,