Work on translate selected lines

This commit is contained in:
Nikolaj Olsson 2024-01-09 20:15:46 +01:00
parent 0a8746ea78
commit 84b33dc9e0
8 changed files with 69 additions and 136 deletions

View File

@ -2708,6 +2708,7 @@ can edit in same subtitle file (collaboration)</Information>
<AudioExtractSelectedLines>Extract audio (selected lines)</AudioExtractSelectedLines>
<AudioToTextSelectedLinesX>Audio to text selected lines ({0})</AudioToTextSelectedLinesX>
<VideoToggleBrightness>Toggle brightness (mpv only)</VideoToggleBrightness>
<AutoTranslateSelectedLines>Auto-translate selected lines</AutoTranslateSelectedLines>
<CustomSearch1>Translate, custom search 1</CustomSearch1>
<CustomSearch2>Translate, custom search 2</CustomSearch2>
<CustomSearch3>Translate, custom search 3</CustomSearch3>

View File

@ -2731,6 +2731,7 @@ $HorzAlign = Center
public string MainTranslateGoogleIt { get; set; }
public string MainTranslateGoogleTranslateIt { get; set; }
public string MainTranslateAuto { get; set; }
public string MainTranslateAutoSelectedLines { get; set; }
public string MainTranslateCustomSearch1 { get; set; }
public string MainTranslateCustomSearch2 { get; set; }
public string MainTranslateCustomSearch3 { get; set; }
@ -11249,6 +11250,12 @@ $HorzAlign = Center
shortcuts.MainTranslateAuto = subNode.InnerText;
}
subNode = node.SelectSingleNode("MainTranslateAutoSelectedLines");
if (subNode != null)
{
shortcuts.MainTranslateAutoSelectedLines = subNode.InnerText;
}
subNode = node.SelectSingleNode("MainTranslateCustomSearch1");
if (subNode != null)
{
@ -12869,6 +12876,7 @@ $HorzAlign = Center
textWriter.WriteElementString("MainTranslateGoogleIt", shortcuts.MainTranslateGoogleIt);
textWriter.WriteElementString("MainTranslateGoogleTranslateIt", shortcuts.MainTranslateGoogleTranslateIt);
textWriter.WriteElementString("MainTranslateAuto", shortcuts.MainTranslateAuto);
textWriter.WriteElementString("MainTranslateAutoSelectedLines", shortcuts.MainTranslateAutoSelectedLines);
textWriter.WriteElementString("MainTranslateCustomSearch1", shortcuts.MainTranslateCustomSearch1);
textWriter.WriteElementString("MainTranslateCustomSearch2", shortcuts.MainTranslateCustomSearch2);
textWriter.WriteElementString("MainTranslateCustomSearch3", shortcuts.MainTranslateCustomSearch3);

View File

@ -26238,6 +26238,7 @@ namespace Nikse.SubtitleEdit.Forms
moveTextDownToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainListViewColumnTextDown);
toolStripMenuItemReverseRightToLeftStartEnd.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainEditReverseStartAndEndingForRTL);
autotranslateNLLBToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainTranslateAuto);
genericTranslateToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainTranslateAutoSelectedLines);
applyCustomStylesToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralApplyAssaOverrideTags);
setPositionToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralSetAssaPosition);
colorPickerToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralColorPicker);
@ -34967,138 +34968,13 @@ namespace Nikse.SubtitleEdit.Forms
private void TranslateSelectedLinesToolStripMenuItemClick(object sender, EventArgs e)
{
var onlySelectedLines = true;
Translate(onlySelectedLines);
MakeAutoTranslate(onlySelectedLines);
}
private void TranslateToolStripMenuItemClick(object sender, EventArgs e)
{
var onlySelectedLines = false;
Translate(onlySelectedLines);
}
private void Translate(bool onlySelectedLines)
{
if (!IsSubtitleLoaded)
{
DisplaySubtitleNotLoadedMessage();
return;
}
bool isOriginalVisible = SubtitleListview1.IsOriginalTextColumnVisible;
ReloadFromSourceView();
using (var translateDialog = new GenericTranslate())
{
SaveSubtitleListviewIndices();
string title = "";
if (onlySelectedLines)
{
var selectedLines = new Subtitle();
foreach (int index in SubtitleListview1.SelectedIndices)
{
selectedLines.Paragraphs.Add(new Paragraph(_subtitle.Paragraphs[index], false));
}
title += " - " + _language.SelectedLines;
if (_subtitleOriginal != null)
{
var paragraphs = new List<Paragraph>();
foreach (int index in SubtitleListview1.SelectedIndices)
{
var original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[index], _subtitleOriginal.Paragraphs);
if (original != null)
{
paragraphs.Add(new Paragraph(original, false));
}
}
if (paragraphs.Count == selectedLines.Paragraphs.Count)
{
translateDialog.Initialize(new Subtitle(paragraphs), selectedLines, title, GetCurrentEncoding(), GetCurrentSubtitleFormat());
}
else
{
translateDialog.Initialize(selectedLines, null, title, GetCurrentEncoding(), GetCurrentSubtitleFormat());
}
}
else
{
translateDialog.Initialize(selectedLines, null, title, GetCurrentEncoding(), GetCurrentSubtitleFormat());
}
}
else
{
translateDialog.Initialize(_subtitle, null, title, GetCurrentEncoding(), GetCurrentSubtitleFormat());
}
if (translateDialog.ShowDialog(this) == DialogResult.OK)
{
_subtitleListViewIndex = -1;
string oldFileName = _fileName;
MakeHistoryForUndo(_language.BeforeGoogleTranslation);
if (onlySelectedLines)
{
// we only update selected lines
int i = 0;
foreach (int index in SubtitleListview1.SelectedIndices)
{
_subtitle.Paragraphs[index].Text = translateDialog.TranslatedSubtitle.Paragraphs[i].Text;
i++;
}
ShowStatus(_language.SelectedLinesTranslated);
}
else
{
ShowSubtitleTimer.Stop();
var oldHash = _changeSubtitleHash;
_subtitleOriginal = new Subtitle(_subtitle);
_subtitleOriginalFileName = _fileName;
_fileName = null;
_subtitle.Paragraphs.Clear();
foreach (var p in translateDialog.TranslatedSubtitle.Paragraphs)
{
_subtitle.Paragraphs.Add(new Paragraph(p));
}
SetAssaResolution(_subtitleOriginal);
ShowStatus(_language.SubtitleTranslated);
_changeOriginalSubtitleHash = oldHash;
_changeSubtitleHash = -1;
ShowSubtitleTimer.Start();
}
ShowSource();
if (!onlySelectedLines)
{
SubtitleListview1.ShowOriginalTextColumn(_languageGeneral.OriginalText);
SubtitleListview1.AutoSizeAllColumns(this);
var oldHash = _changeOriginalSubtitleHash;
SetupOriginalEdit();
_changeOriginalSubtitleHash = oldHash;
}
SubtitleListview1.Fill(_subtitle, _subtitleOriginal);
if (!onlySelectedLines)
{
ResetHistory();
_fileName = translateDialog.GetFileNameWithTargetLanguage(oldFileName, _videoFileName, _subtitleOriginal, GetCurrentSubtitleFormat());
_converted = true;
}
RestoreSubtitleListviewIndices();
SetTitle();
SetEncoding(Encoding.UTF8);
if (!isOriginalVisible)
{
toolStripMenuItemShowOriginalInPreview.Checked = false;
Configuration.Settings.General.ShowOriginalAsPreviewIfAvailable = false;
audioVisualizer.Invalidate();
}
}
}
MakeAutoTranslate(onlySelectedLines);
}
private void AutotranslateViaCopypasteToolStripMenuItemClick(object sender, EventArgs e)
@ -36269,23 +36145,65 @@ namespace Nikse.SubtitleEdit.Forms
bool isOriginalVisible = SubtitleListview1.IsOriginalTextColumnVisible;
ReloadFromSourceView();
string title = string.Empty;
using (var autoTranslate = new AutoTranslate(_subtitle, null, title, GetCurrentEncoding()))
var sub = new Subtitle(_subtitle);
Subtitle target = null;
if (onlySelectedLines)
{
title = LanguageSettings.Current.GoogleTranslate.Title + " - " + _language.SelectedLines;
sub = new Subtitle();
foreach (int index in SubtitleListview1.SelectedIndices)
{
sub.Paragraphs.Add(new Paragraph(_subtitle.Paragraphs[index], false));
}
if (_subtitleOriginal != null)
{
var paragraphs = new List<Paragraph>();
var paragraphsToRemove = new List<Paragraph>();
foreach (int index in SubtitleListview1.SelectedIndices)
{
var original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[index], _subtitleOriginal.Paragraphs);
if (original != null)
{
paragraphs.Add(new Paragraph(original, false));
}
else
{
paragraphsToRemove.Add(_subtitle.Paragraphs[index]);
}
}
foreach (var p in paragraphsToRemove)
{
sub.Paragraphs.RemoveAll(x => x.Id == p.Id);
}
target = sub;
sub = new Subtitle(paragraphs);
}
}
using (var autoTranslate = new AutoTranslate(sub, target, title, GetCurrentEncoding()))
{
SaveSubtitleListviewIndices();
if (autoTranslate.ShowDialog(this) == DialogResult.OK)
{
_subtitleListViewIndex = -1;
string oldFileName = _fileName;
var oldFileName = _fileName;
MakeHistoryForUndo(_language.BeforeGoogleTranslation);
if (onlySelectedLines)
{
// we only update selected lines
int i = 0;
foreach (int index in SubtitleListview1.SelectedIndices)
{
_subtitle.Paragraphs[index].Text = autoTranslate.TranslatedSubtitle.Paragraphs[i].Text;
i++;
var p = _subtitle.Paragraphs[index];
var t = autoTranslate.TranslatedSubtitle.Paragraphs.FirstOrDefault(x => x.Id == p.Id);
if (t != null)
{
p.Text = t.Text;
}
}
ShowStatus(_language.SelectedLinesTranslated);

View File

@ -1813,6 +1813,7 @@ namespace Nikse.SubtitleEdit.Forms.Options
AddNode(translateNode, LanguageSettings.Current.Main.VideoControls.GoogleIt, nameof(Configuration.Settings.Shortcuts.MainTranslateGoogleIt));
AddNode(translateNode, LanguageSettings.Current.Main.VideoControls.GoogleTranslate, nameof(Configuration.Settings.Shortcuts.MainTranslateGoogleTranslateIt));
AddNode(translateNode, LanguageSettings.Current.Main.VideoControls.AutoTranslate, nameof(Configuration.Settings.Shortcuts.MainTranslateAuto), true);
AddNode(translateNode, language.AutoTranslateSelectedLines, nameof(Configuration.Settings.Shortcuts.MainTranslateAutoSelectedLines), true);
AddNode(translateNode, language.CustomSearch1, nameof(Configuration.Settings.Shortcuts.MainTranslateCustomSearch1));
AddNode(translateNode, language.CustomSearch2, nameof(Configuration.Settings.Shortcuts.MainTranslateCustomSearch2));
AddNode(translateNode, language.CustomSearch3, nameof(Configuration.Settings.Shortcuts.MainTranslateCustomSearch3));

View File

@ -32,7 +32,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
private bool _translationInProgress;
private CancellationTokenSource _cancellationTokenSource;
public AutoTranslate(Subtitle subtitle, Subtitle selectedLines, string title, Encoding encoding)
public AutoTranslate(Subtitle subtitle, Subtitle targetLines, string title, Encoding encoding)
{
UiUtil.PreInitialize(this);
InitializeComponent();
@ -89,15 +89,15 @@ namespace Nikse.SubtitleEdit.Forms.Translate
labelPleaseWait.Visible = false;
progressBar1.Visible = false;
if (selectedLines != null)
if (targetLines != null)
{
TranslatedSubtitle = new Subtitle(selectedLines);
TranslatedSubtitle = new Subtitle(targetLines, false);
TranslatedSubtitle.Renumber();
subtitleListViewTarget.Fill(TranslatedSubtitle);
}
else
{
TranslatedSubtitle = new Subtitle(_subtitle);
TranslatedSubtitle = new Subtitle(_subtitle, false);
foreach (var paragraph in TranslatedSubtitle.Paragraphs)
{
paragraph.Text = string.Empty;

View File

@ -3066,6 +3066,7 @@ can edit in same subtitle file (collaboration)",
AudioToTextSelectedLinesX = "Audio to text selected lines ({0})",
AudioExtractSelectedLines = "Extract audio (selected lines)",
VideoToggleBrightness = "Toggle brightness (mpv only)",
AutoTranslateSelectedLines = "Auto-translate selected lines",
CustomSearch1 = "Translate, custom search 1",
CustomSearch2 = "Translate, custom search 2",
CustomSearch3 = "Translate, custom search 3",

View File

@ -7411,6 +7411,9 @@ namespace Nikse.SubtitleEdit.Logic
case "Settings/VideoToggleBrightness":
language.Settings.VideoToggleBrightness = reader.Value;
break;
case "Settings/AutoTranslateSelectedLines":
language.Settings.AutoTranslateSelectedLines = reader.Value;
break;
case "Settings/CustomSearch1":
language.Settings.CustomSearch1 = reader.Value;
break;

View File

@ -2884,6 +2884,7 @@
public string AudioExtractSelectedLines { get; set; }
public string AudioToTextSelectedLinesX { get; set; }
public string VideoToggleBrightness { get; set; }
public string AutoTranslateSelectedLines { get; set; }
public string CustomSearch1 { get; set; }
public string CustomSearch2 { get; set; }
public string CustomSearch3 { get; set; }