Work on NLLB

This commit is contained in:
niksedk 2023-09-30 12:49:04 +02:00
parent d435c819b9
commit fb7d6d24a2
10 changed files with 12829 additions and 12517 deletions

View File

@ -1044,6 +1044,7 @@ We leverage the intrinsic rhythm of the image.</CreateSimpleChainingToolTip>
<PleaseWait>Please wait... this may take a while</PleaseWait> <PleaseWait>Please wait... this may take a while</PleaseWait>
<PoweredByGoogleTranslate>Powered by Google translate</PoweredByGoogleTranslate> <PoweredByGoogleTranslate>Powered by Google translate</PoweredByGoogleTranslate>
<PoweredByMicrosoftTranslate>Powered by Microsoft translate</PoweredByMicrosoftTranslate> <PoweredByMicrosoftTranslate>Powered by Microsoft translate</PoweredByMicrosoftTranslate>
<PoweredByX>Powered by {0}</PoweredByX>
<MsClientSecretNeeded>Sorry, you need a Cognitive Services 'Translator Text' key from Microsoft to use the latest Microsoft Translator. <MsClientSecretNeeded>Sorry, you need a Cognitive Services 'Translator Text' key from Microsoft to use the latest Microsoft Translator.
Go to "Options -&gt; Settings -&gt; Tools" to enter your key.</MsClientSecretNeeded> Go to "Options -&gt; Settings -&gt; Tools" to enter your key.</MsClientSecretNeeded>

View File

@ -6,10 +6,34 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
{ {
public interface IAutoTranslator public interface IAutoTranslator
{ {
void Initialize(string url); /// <summary>
/// Name of auto translator (can be translated).
/// </summary>
string Name { get; set; }
/// <summary>
/// Url to homepage.
/// </summary>
string Url { get; } string Url { get; }
/// <summary>
/// Initialization before calling translate.
/// </summary>
void Initialize();
/// <summary>
/// Get languages that can be translated from.
/// </summary>
List<TranslationPair> GetSupportedSourceLanguages(); List<TranslationPair> GetSupportedSourceLanguages();
/// <summary>
/// Get languages that can be translated to.
/// </summary>
List<TranslationPair> GetSupportedTargetLanguages(); List<TranslationPair> GetSupportedTargetLanguages();
/// <summary>
/// Do translation.
/// </summary>
Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode); Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode);
} }
} }

View File

@ -0,0 +1,58 @@
using Nikse.SubtitleEdit.Core.Common;
using Nikse.SubtitleEdit.Core.Http;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
using Nikse.SubtitleEdit.Core.Translate;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Nikse.SubtitleEdit.Core.AutoTranslate
{
public class NoLanguageLeftBehindApi : IAutoTranslator
{
private IDownloader _httpClient;
public string Name { get; set; } = "winstxnhdw-nllb-api";
public string Url => "https://github.com/winstxnhdw/nllb-api";
public void Initialize()
{
_httpClient?.Dispose();
_httpClient = DownloaderFactory.MakeHttpClient();
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
_httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateNllbApiUrl);
}
public List<TranslationPair> GetSupportedSourceLanguages()
{
return new NoLanguageLeftBehindServe().GetSupportedSourceLanguages();
}
public List<TranslationPair> GetSupportedTargetLanguages()
{
return new NoLanguageLeftBehindServe().GetSupportedTargetLanguages();
}
public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode)
{
var content = new StringContent("{ \"text\": \"" + Json.EncodeJsonText(text) + "\", \"source\": \"" + sourceLanguageCode + "\", \"target\": \"" + targetLanguageCode + "\" }", Encoding.UTF8, "application/json");
var result = _httpClient.PostAsync("translate", content).Result;
result.EnsureSuccessStatusCode();
var bytes = await result.Content.ReadAsByteArrayAsync();
var resultText = Encoding.UTF8.GetString(bytes).Trim();
// error messages are returned as json, translated text are just returned as normal utf-8 text
var validator = new SeJsonValidator();
var isValidJson = validator.ValidateJson(resultText);
if (isValidJson)
{
SeLogger.Error($"{this.GetType().Name} got json back which is probably an error: {resultText}");
}
return resultText;
}
}
}

View File

@ -1,28 +1,32 @@
using Nikse.SubtitleEdit.Core.Common; using Nikse.SubtitleEdit.Core.Common;
using Nikse.SubtitleEdit.Core.Http;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
using Nikse.SubtitleEdit.Core.Translate; using Nikse.SubtitleEdit.Core.Translate;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Nikse.SubtitleEdit.Core.Http;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
using System.Net.Http.Headers;
namespace Nikse.SubtitleEdit.Core.AutoTranslate namespace Nikse.SubtitleEdit.Core.AutoTranslate
{ {
public class AutoTranslator : IAutoTranslator public class NoLanguageLeftBehindServe : IAutoTranslator
{ {
private IDownloader _httpClient; private IDownloader _httpClient;
public string Url => "https://winstxnhdw-nllb-api.hf.space/api/v2/"; public string Name { get; set; } = "thammegowda-nllb-serve";
public string Url => "https://github.com/thammegowda/nllb-serve";
public void Initialize(string url) public void Initialize()
{ {
_httpClient?.Dispose(); _httpClient?.Dispose();
_httpClient = DownloaderFactory.MakeHttpClient(); _httpClient = DownloaderFactory.MakeHttpClient();
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
_httpClient.BaseAddress = new Uri(url); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateNllbServeUrl);
} }
public List<TranslationPair> GetSupportedSourceLanguages() public List<TranslationPair> GetSupportedSourceLanguages()
@ -37,21 +41,37 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode) public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode)
{ {
var content = new StringContent("{\n \"text\": \"" + Json.EncodeJsonText(text) + "\",\n \"source\": \"" + sourceLanguageCode+ "\",\n \"target\": \"" + targetLanguageCode + "\"\n}", Encoding.UTF8, "application/json"); var list = text.SplitToLines();
var sb = new StringBuilder();
foreach (var line in list)
{
sb.Append("\"" + Json.EncodeJsonText(text) + "\", ");
}
var src = sb.ToString().TrimEnd().TrimEnd(',').Trim();
var input = "{\"source\":[" + src + "], \"src_lang\": \"" + sourceLanguageCode + "\", \"tgt_lang\": \"" + targetLanguageCode + "\"}";
var content = new StringContent(input, Encoding.UTF8);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = _httpClient.PostAsync("translate", content).Result; var result = _httpClient.PostAsync("translate", content).Result;
result.EnsureSuccessStatusCode(); result.EnsureSuccessStatusCode();
var bytes = await result.Content.ReadAsByteArrayAsync(); var bytes = await result.Content.ReadAsByteArrayAsync();
var resultText = Encoding.UTF8.GetString(bytes).Trim(); var json = Encoding.UTF8.GetString(bytes).Trim();
// error messages are returned as json, translated text are just returned as normal utf-8 text var parser = new SeJsonParser();
var validator = new SeJsonValidator(); var arr = parser.GetArrayElementsByName(json, "translation");
var isValidJson = validator.ValidateJson(resultText); if (arr == null || arr.Count == 0)
if (isValidJson)
{ {
SeLogger.Error($"{this.GetType().Name} got json back which is probably an error: {resultText}"); return string.Empty;
} }
return resultText; var resultText = new StringBuilder();
foreach (var item in arr)
{
resultText.AppendLine(Json.DecodeJsonText(item));
}
return resultText.ToString().Trim();
} }
private static List<TranslationPair> ListLanguages() private static List<TranslationPair> ListLanguages()

File diff suppressed because it is too large Load Diff

View File

@ -36,6 +36,8 @@
this.labelTarget = new System.Windows.Forms.Label(); this.labelTarget = new System.Windows.Forms.Label();
this.labelSource = new System.Windows.Forms.Label(); this.labelSource = new System.Windows.Forms.Label();
this.labelUrl = new System.Windows.Forms.Label(); this.labelUrl = new System.Windows.Forms.Label();
this.linkLabelPoweredBy = new System.Windows.Forms.LinkLabel();
this.nikseComboBoxEngine = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.nikseComboBoxUrl = new Nikse.SubtitleEdit.Controls.NikseComboBox(); this.nikseComboBoxUrl = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.comboBoxSource = new Nikse.SubtitleEdit.Controls.NikseComboBox(); this.comboBoxSource = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.comboBoxTarget = new Nikse.SubtitleEdit.Controls.NikseComboBox(); this.comboBoxTarget = new Nikse.SubtitleEdit.Controls.NikseComboBox();
@ -45,7 +47,7 @@
// //
// progressBar1 // progressBar1
// //
this.progressBar1.Location = new System.Drawing.Point(699, 27); this.progressBar1.Location = new System.Drawing.Point(699, 29);
this.progressBar1.Name = "progressBar1"; this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(192, 16); this.progressBar1.Size = new System.Drawing.Size(192, 16);
this.progressBar1.TabIndex = 100; this.progressBar1.TabIndex = 100;
@ -53,7 +55,7 @@
// labelPleaseWait // labelPleaseWait
// //
this.labelPleaseWait.AutoSize = true; this.labelPleaseWait.AutoSize = true;
this.labelPleaseWait.Location = new System.Drawing.Point(697, 11); this.labelPleaseWait.Location = new System.Drawing.Point(697, 13);
this.labelPleaseWait.Name = "labelPleaseWait"; this.labelPleaseWait.Name = "labelPleaseWait";
this.labelPleaseWait.Size = new System.Drawing.Size(171, 13); this.labelPleaseWait.Size = new System.Drawing.Size(171, 13);
this.labelPleaseWait.TabIndex = 99; this.labelPleaseWait.TabIndex = 99;
@ -84,7 +86,7 @@
// //
// buttonTranslate // buttonTranslate
// //
this.buttonTranslate.Location = new System.Drawing.Point(618, 22); this.buttonTranslate.Location = new System.Drawing.Point(618, 24);
this.buttonTranslate.Name = "buttonTranslate"; this.buttonTranslate.Name = "buttonTranslate";
this.buttonTranslate.Size = new System.Drawing.Size(75, 23); this.buttonTranslate.Size = new System.Drawing.Size(75, 23);
this.buttonTranslate.TabIndex = 96; this.buttonTranslate.TabIndex = 96;
@ -95,7 +97,7 @@
// labelTarget // labelTarget
// //
this.labelTarget.AutoSize = true; this.labelTarget.AutoSize = true;
this.labelTarget.Location = new System.Drawing.Point(462, 27); this.labelTarget.Location = new System.Drawing.Point(462, 29);
this.labelTarget.Name = "labelTarget"; this.labelTarget.Name = "labelTarget";
this.labelTarget.Size = new System.Drawing.Size(23, 13); this.labelTarget.Size = new System.Drawing.Size(23, 13);
this.labelTarget.TabIndex = 104; this.labelTarget.TabIndex = 104;
@ -104,7 +106,7 @@
// labelSource // labelSource
// //
this.labelSource.AutoSize = true; this.labelSource.AutoSize = true;
this.labelSource.Location = new System.Drawing.Point(279, 27); this.labelSource.Location = new System.Drawing.Point(279, 29);
this.labelSource.Name = "labelSource"; this.labelSource.Name = "labelSource";
this.labelSource.Size = new System.Drawing.Size(33, 13); this.labelSource.Size = new System.Drawing.Size(33, 13);
this.labelSource.TabIndex = 103; this.labelSource.TabIndex = 103;
@ -120,6 +122,41 @@
this.labelUrl.TabIndex = 106; this.labelUrl.TabIndex = 106;
this.labelUrl.Text = "Url:"; this.labelUrl.Text = "Url:";
// //
// linkLabelPoweredBy
//
this.linkLabelPoweredBy.AutoSize = true;
this.linkLabelPoweredBy.Location = new System.Drawing.Point(14, 5);
this.linkLabelPoweredBy.Name = "linkLabelPoweredBy";
this.linkLabelPoweredBy.Size = new System.Drawing.Size(73, 13);
this.linkLabelPoweredBy.TabIndex = 109;
this.linkLabelPoweredBy.TabStop = true;
this.linkLabelPoweredBy.Text = "Powered by X";
this.linkLabelPoweredBy.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelPoweredBy_LinkClicked);
//
// nikseComboBoxEngine
//
this.nikseComboBoxEngine.BackColor = System.Drawing.SystemColors.Window;
this.nikseComboBoxEngine.BackColorDisabled = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
this.nikseComboBoxEngine.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(171)))), ((int)(((byte)(173)))), ((int)(((byte)(179)))));
this.nikseComboBoxEngine.BorderColorDisabled = System.Drawing.Color.FromArgb(((int)(((byte)(120)))), ((int)(((byte)(120)))), ((int)(((byte)(120)))));
this.nikseComboBoxEngine.ButtonForeColor = System.Drawing.SystemColors.ControlText;
this.nikseComboBoxEngine.ButtonForeColorDown = System.Drawing.Color.Orange;
this.nikseComboBoxEngine.ButtonForeColorOver = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215)))));
this.nikseComboBoxEngine.DropDownHeight = 400;
this.nikseComboBoxEngine.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.nikseComboBoxEngine.DropDownWidth = 221;
this.nikseComboBoxEngine.FormattingEnabled = true;
this.nikseComboBoxEngine.Location = new System.Drawing.Point(12, 24);
this.nikseComboBoxEngine.MaxLength = 32767;
this.nikseComboBoxEngine.Name = "nikseComboBoxEngine";
this.nikseComboBoxEngine.SelectedIndex = -1;
this.nikseComboBoxEngine.SelectedItem = null;
this.nikseComboBoxEngine.SelectedText = "";
this.nikseComboBoxEngine.Size = new System.Drawing.Size(221, 21);
this.nikseComboBoxEngine.TabIndex = 107;
this.nikseComboBoxEngine.UsePopupWindow = false;
this.nikseComboBoxEngine.SelectedIndexChanged += new System.EventHandler(this.nikseComboBoxEngine_SelectedIndexChanged);
//
// nikseComboBoxUrl // nikseComboBoxUrl
// //
this.nikseComboBoxUrl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.nikseComboBoxUrl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
@ -143,6 +180,7 @@
this.nikseComboBoxUrl.Size = new System.Drawing.Size(280, 21); this.nikseComboBoxUrl.Size = new System.Drawing.Size(280, 21);
this.nikseComboBoxUrl.TabIndex = 105; this.nikseComboBoxUrl.TabIndex = 105;
this.nikseComboBoxUrl.UsePopupWindow = false; this.nikseComboBoxUrl.UsePopupWindow = false;
this.nikseComboBoxUrl.SelectedIndexChanged += new System.EventHandler(this.nikseComboBoxUrl_SelectedIndexChanged);
// //
// comboBoxSource // comboBoxSource
// //
@ -155,9 +193,9 @@
this.comboBoxSource.ButtonForeColorOver = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215))))); this.comboBoxSource.ButtonForeColorOver = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215)))));
this.comboBoxSource.DropDownHeight = 400; this.comboBoxSource.DropDownHeight = 400;
this.comboBoxSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSource.DropDownWidth = 200; this.comboBoxSource.DropDownWidth = 140;
this.comboBoxSource.FormattingEnabled = true; this.comboBoxSource.FormattingEnabled = true;
this.comboBoxSource.Location = new System.Drawing.Point(321, 23); this.comboBoxSource.Location = new System.Drawing.Point(321, 25);
this.comboBoxSource.MaxLength = 32767; this.comboBoxSource.MaxLength = 32767;
this.comboBoxSource.Name = "comboBoxSource"; this.comboBoxSource.Name = "comboBoxSource";
this.comboBoxSource.SelectedIndex = -1; this.comboBoxSource.SelectedIndex = -1;
@ -178,9 +216,9 @@
this.comboBoxTarget.ButtonForeColorOver = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215))))); this.comboBoxTarget.ButtonForeColorOver = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215)))));
this.comboBoxTarget.DropDownHeight = 400; this.comboBoxTarget.DropDownHeight = 400;
this.comboBoxTarget.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxTarget.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxTarget.DropDownWidth = 200; this.comboBoxTarget.DropDownWidth = 140;
this.comboBoxTarget.FormattingEnabled = true; this.comboBoxTarget.FormattingEnabled = true;
this.comboBoxTarget.Location = new System.Drawing.Point(491, 23); this.comboBoxTarget.Location = new System.Drawing.Point(491, 25);
this.comboBoxTarget.MaxLength = 32767; this.comboBoxTarget.MaxLength = 32767;
this.comboBoxTarget.Name = "comboBoxTarget"; this.comboBoxTarget.Name = "comboBoxTarget";
this.comboBoxTarget.SelectedIndex = -1; this.comboBoxTarget.SelectedIndex = -1;
@ -199,10 +237,10 @@
this.subtitleListViewTarget.GridLines = true; this.subtitleListViewTarget.GridLines = true;
this.subtitleListViewTarget.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; this.subtitleListViewTarget.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.subtitleListViewTarget.HideSelection = false; this.subtitleListViewTarget.HideSelection = false;
this.subtitleListViewTarget.Location = new System.Drawing.Point(465, 52); this.subtitleListViewTarget.Location = new System.Drawing.Point(465, 53);
this.subtitleListViewTarget.Name = "subtitleListViewTarget"; this.subtitleListViewTarget.Name = "subtitleListViewTarget";
this.subtitleListViewTarget.OwnerDraw = true; this.subtitleListViewTarget.OwnerDraw = true;
this.subtitleListViewTarget.Size = new System.Drawing.Size(428, 459); this.subtitleListViewTarget.Size = new System.Drawing.Size(428, 458);
this.subtitleListViewTarget.SubtitleFontBold = false; this.subtitleListViewTarget.SubtitleFontBold = false;
this.subtitleListViewTarget.SubtitleFontName = "Tahoma"; this.subtitleListViewTarget.SubtitleFontName = "Tahoma";
this.subtitleListViewTarget.SubtitleFontSize = 8; this.subtitleListViewTarget.SubtitleFontSize = 8;
@ -220,10 +258,10 @@
this.subtitleListViewSource.GridLines = true; this.subtitleListViewSource.GridLines = true;
this.subtitleListViewSource.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; this.subtitleListViewSource.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.subtitleListViewSource.HideSelection = false; this.subtitleListViewSource.HideSelection = false;
this.subtitleListViewSource.Location = new System.Drawing.Point(12, 52); this.subtitleListViewSource.Location = new System.Drawing.Point(12, 53);
this.subtitleListViewSource.Name = "subtitleListViewSource"; this.subtitleListViewSource.Name = "subtitleListViewSource";
this.subtitleListViewSource.OwnerDraw = true; this.subtitleListViewSource.OwnerDraw = true;
this.subtitleListViewSource.Size = new System.Drawing.Size(430, 459); this.subtitleListViewSource.Size = new System.Drawing.Size(430, 458);
this.subtitleListViewSource.SubtitleFontBold = false; this.subtitleListViewSource.SubtitleFontBold = false;
this.subtitleListViewSource.SubtitleFontName = "Tahoma"; this.subtitleListViewSource.SubtitleFontName = "Tahoma";
this.subtitleListViewSource.SubtitleFontSize = 8; this.subtitleListViewSource.SubtitleFontSize = 8;
@ -237,6 +275,8 @@
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1058, 563); this.ClientSize = new System.Drawing.Size(1058, 563);
this.Controls.Add(this.linkLabelPoweredBy);
this.Controls.Add(this.nikseComboBoxEngine);
this.Controls.Add(this.labelUrl); this.Controls.Add(this.labelUrl);
this.Controls.Add(this.nikseComboBoxUrl); this.Controls.Add(this.nikseComboBoxUrl);
this.Controls.Add(this.labelTarget); this.Controls.Add(this.labelTarget);
@ -258,6 +298,7 @@
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "AutoTranslate"; this.Text = "AutoTranslate";
this.ResizeEnd += new System.EventHandler(this.AutoTranslate_ResizeEnd); this.ResizeEnd += new System.EventHandler(this.AutoTranslate_ResizeEnd);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.AutoTranslate_KeyDown);
this.Resize += new System.EventHandler(this.AutoTranslate_Resize); this.Resize += new System.EventHandler(this.AutoTranslate_Resize);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -279,5 +320,7 @@
private System.Windows.Forms.Label labelSource; private System.Windows.Forms.Label labelSource;
private System.Windows.Forms.Label labelUrl; private System.Windows.Forms.Label labelUrl;
private Controls.NikseComboBox nikseComboBoxUrl; private Controls.NikseComboBox nikseComboBoxUrl;
private Controls.NikseComboBox nikseComboBoxEngine;
private System.Windows.Forms.LinkLabel linkLabelPoweredBy;
} }
} }

View File

@ -17,7 +17,8 @@ namespace Nikse.SubtitleEdit.Forms.Translate
public Subtitle TranslatedSubtitle { get; } public Subtitle TranslatedSubtitle { get; }
private readonly Subtitle _subtitle; private readonly Subtitle _subtitle;
private readonly Encoding _encoding; private readonly Encoding _encoding;
private readonly IAutoTranslator _autoTranslator; private IAutoTranslator _autoTranslator;
private List<IAutoTranslator> _autoTranslatorEngines;
private int _translationProgressIndex = -1; private int _translationProgressIndex = -1;
private bool _translationProgressDirty = true; private bool _translationProgressDirty = true;
private bool _breakTranslation; private bool _breakTranslation;
@ -41,6 +42,10 @@ namespace Nikse.SubtitleEdit.Forms.Translate
subtitleListViewTarget.HideColumn(SubtitleListView.SubtitleColumn.WordsPerMinute); subtitleListViewTarget.HideColumn(SubtitleListView.SubtitleColumn.WordsPerMinute);
UiUtil.InitializeSubtitleFont(subtitleListViewSource); UiUtil.InitializeSubtitleFont(subtitleListViewSource);
UiUtil.InitializeSubtitleFont(subtitleListViewTarget); UiUtil.InitializeSubtitleFont(subtitleListViewTarget);
subtitleListViewSource.HideColumn(SubtitleListView.SubtitleColumn.End);
subtitleListViewSource.HideColumn(SubtitleListView.SubtitleColumn.Gap);
subtitleListViewTarget.HideColumn(SubtitleListView.SubtitleColumn.End);
subtitleListViewTarget.HideColumn(SubtitleListView.SubtitleColumn.Gap);
subtitleListViewSource.AutoSizeColumns(); subtitleListViewSource.AutoSizeColumns();
subtitleListViewSource.AutoSizeColumns(); subtitleListViewSource.AutoSizeColumns();
UiUtil.FixLargeFonts(this, buttonOK); UiUtil.FixLargeFonts(this, buttonOK);
@ -51,10 +56,8 @@ namespace Nikse.SubtitleEdit.Forms.Translate
Text = title; Text = title;
} }
nikseComboBoxUrl.Items.Clear(); InitializeAutoTranslatorEngines();
nikseComboBoxUrl.Items.Add("https://winstxnhdw-nllb-api.hf.space/api/v2/");
nikseComboBoxUrl.Items.Add("http://localhost:7860/api/v2/");
nikseComboBoxUrl.SelectedIndex = 0;
nikseComboBoxUrl.UsePopupWindow = true; nikseComboBoxUrl.UsePopupWindow = true;
labelPleaseWait.Visible = false; labelPleaseWait.Visible = false;
@ -80,11 +83,92 @@ namespace Nikse.SubtitleEdit.Forms.Translate
subtitleListViewSource.Fill(_subtitle); subtitleListViewSource.Fill(_subtitle);
AutoTranslate_Resize(null, null); AutoTranslate_Resize(null, null);
_autoTranslator = new AutoTranslator(); _autoTranslator = new NoLanguageLeftBehindApi();
SetupLanguageSettings(); SetupLanguageSettings();
UpdateTranslation(); UpdateTranslation();
} }
private void InitializeAutoTranslatorEngines()
{
_autoTranslatorEngines = new List<IAutoTranslator>
{
new NoLanguageLeftBehindServe(),
new NoLanguageLeftBehindApi(),
};
nikseComboBoxEngine.Items.Clear();
nikseComboBoxEngine.Items.AddRange(_autoTranslatorEngines.Select(p => p.Name).ToArray<object>());
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.AutoTranslateLastName))
{
var lastEngine = _autoTranslatorEngines.FirstOrDefault(p => p.Name == Configuration.Settings.Tools.AutoTranslateLastName);
if (lastEngine != null)
{
nikseComboBoxEngine.SelectedIndex = _autoTranslatorEngines.IndexOf(lastEngine);
}
}
if (nikseComboBoxEngine.SelectedIndex < 0)
{
nikseComboBoxEngine.SelectedIndex = 0;
}
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.AutoTranslateLastUrl))
{
nikseComboBoxUrl.SelectedText = Configuration.Settings.Tools.AutoTranslateLastUrl;
}
}
private void SetAutoTranslatorEngine()
{
var engine = GetCurrentEngine();
linkLabelPoweredBy.Text = string.Format(LanguageSettings.Current.GoogleTranslate.PoweredByX, engine.Name);
var engineType = engine.GetType();
if (engineType == typeof(NoLanguageLeftBehindServe))
{
nikseComboBoxUrl.Items.Clear();
nikseComboBoxUrl.Items.Add("http://127.0.0.1:6060/");
nikseComboBoxUrl.Items.Add("http://192.168.8.127:6060/");
nikseComboBoxUrl.SelectedIndex = 0;
nikseComboBoxUrl.Visible = true;
labelUrl.Visible = true;
return;
}
if (engineType == typeof(NoLanguageLeftBehindApi))
{
nikseComboBoxUrl.Items.Clear();
nikseComboBoxUrl.Items.Add("http://localhost:7860/api/v2/");
nikseComboBoxUrl.SelectedIndex = 0;
nikseComboBoxUrl.Visible = true;
labelUrl.Visible = true;
return;
}
throw new Exception($"Engine {engine.Name} not handled!");
}
private void SetAutoTranslatorUrl(string url)
{
var engine = GetCurrentEngine();
var engineType = engine.GetType();
if (engineType == typeof(NoLanguageLeftBehindApi))
{
Configuration.Settings.Tools.AutoTranslateNllbApiUrl = url;
return;
}
if (engineType == typeof(NoLanguageLeftBehindServe))
{
Configuration.Settings.Tools.AutoTranslateNllbApiUrl = url;
return;
}
throw new Exception($"Engine {engine.Name} not handled!");
}
private void SetupLanguageSettings() private void SetupLanguageSettings()
{ {
FillComboWithLanguages(comboBoxSource, _autoTranslator.GetSupportedSourceLanguages()); FillComboWithLanguages(comboBoxSource, _autoTranslator.GetSupportedSourceLanguages());
@ -256,7 +340,8 @@ namespace Nikse.SubtitleEdit.Forms.Translate
progressBar1.Visible = true; progressBar1.Visible = true;
labelPleaseWait.Visible = true; labelPleaseWait.Visible = true;
_autoTranslator.Initialize(nikseComboBoxUrl.Text); _autoTranslator = GetCurrentEngine();
_autoTranslator.Initialize();
var timerUpdate = new Timer(); var timerUpdate = new Timer();
timerUpdate.Interval = 1500; timerUpdate.Interval = 1500;
@ -600,6 +685,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
_translationProgressDirty = true; _translationProgressDirty = true;
subtitleListViewTarget.SelectIndexAndEnsureVisible(_translationProgressIndex); subtitleListViewTarget.SelectIndexAndEnsureVisible(_translationProgressIndex);
subtitleListViewTarget.EndUpdate(); subtitleListViewTarget.EndUpdate();
subtitleListViewSource.SelectIndexAndEnsureVisible(_translationProgressIndex);
} }
private void AutoTranslate_ResizeEnd(object sender, EventArgs e) private void AutoTranslate_ResizeEnd(object sender, EventArgs e)
@ -609,13 +695,51 @@ namespace Nikse.SubtitleEdit.Forms.Translate
private void buttonOK_Click(object sender, EventArgs e) private void buttonOK_Click(object sender, EventArgs e)
{ {
var engine = GetCurrentEngine();
Configuration.Settings.Tools.AutoTranslateLastName = engine.Name;
Configuration.Settings.Tools.AutoTranslateLastUrl = nikseComboBoxUrl.Text;
var isEmpty = TranslatedSubtitle == null || TranslatedSubtitle.Paragraphs.All(p => string.IsNullOrEmpty(p.Text)); var isEmpty = TranslatedSubtitle == null || TranslatedSubtitle.Paragraphs.All(p => string.IsNullOrEmpty(p.Text));
DialogResult = isEmpty ? DialogResult.Cancel : DialogResult.OK; DialogResult = isEmpty ? DialogResult.Cancel : DialogResult.OK;
} }
private IAutoTranslator GetCurrentEngine()
{
return _autoTranslatorEngines.First(p => p.Name == nikseComboBoxEngine.Text);
}
private void buttonCancel_Click(object sender, EventArgs e) private void buttonCancel_Click(object sender, EventArgs e)
{ {
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
} }
private void nikseComboBoxEngine_SelectedIndexChanged(object sender, EventArgs e)
{
SetAutoTranslatorEngine();
}
private void nikseComboBoxUrl_SelectedIndexChanged(object sender, EventArgs e)
{
SetAutoTranslatorUrl(nikseComboBoxUrl.Text);
}
private void linkLabelPoweredBy_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var engine = _autoTranslatorEngines.First(p => p.Name == nikseComboBoxEngine.Text);
UiUtil.OpenUrl(engine.Url);
}
private void AutoTranslate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
DialogResult = DialogResult.Cancel;
}
else if (e.KeyData == UiUtil.HelpKeys)
{
UiUtil.ShowHelp("#translation");
e.SuppressKeyPress = true;
}
}
} }
} }

View File

@ -1303,6 +1303,7 @@ namespace Nikse.SubtitleEdit.Logic
PleaseWait = "Please wait... this may take a while", PleaseWait = "Please wait... this may take a while",
PoweredByGoogleTranslate = "Powered by Google translate", PoweredByGoogleTranslate = "Powered by Google translate",
PoweredByMicrosoftTranslate = "Powered by Microsoft translate", PoweredByMicrosoftTranslate = "Powered by Microsoft translate",
PoweredByX = "Powered by {0}",
MsClientSecretNeeded = "Sorry, you need a Cognitive Services 'Translator Text' key from Microsoft to use the latest Microsoft Translator." + Environment.NewLine + MsClientSecretNeeded = "Sorry, you need a Cognitive Services 'Translator Text' key from Microsoft to use the latest Microsoft Translator." + Environment.NewLine +
Environment.NewLine + Environment.NewLine +
"Go to \"Options -> Settings -> Tools\" to enter your key.", "Go to \"Options -> Settings -> Tools\" to enter your key.",

View File

@ -2749,6 +2749,9 @@ namespace Nikse.SubtitleEdit.Logic
case "GoogleTranslate/PoweredByMicrosoftTranslate": case "GoogleTranslate/PoweredByMicrosoftTranslate":
language.GoogleTranslate.PoweredByMicrosoftTranslate = reader.Value; language.GoogleTranslate.PoweredByMicrosoftTranslate = reader.Value;
break; break;
case "GoogleTranslate/PoweredByX":
language.GoogleTranslate.PoweredByX = reader.Value;
break;
case "GoogleTranslate/MsClientSecretNeeded": case "GoogleTranslate/MsClientSecretNeeded":
language.GoogleTranslate.MsClientSecretNeeded = reader.Value; language.GoogleTranslate.MsClientSecretNeeded = reader.Value;
break; break;

View File

@ -1127,6 +1127,7 @@
public string PleaseWait { get; set; } public string PleaseWait { get; set; }
public string PoweredByGoogleTranslate { get; set; } public string PoweredByGoogleTranslate { get; set; }
public string PoweredByMicrosoftTranslate { get; set; } public string PoweredByMicrosoftTranslate { get; set; }
public string PoweredByX { get; set; }
public string MsClientSecretNeeded { get; set; } public string MsClientSecretNeeded { get; set; }
public string GoogleNoApiKeyWarning { get; set; } public string GoogleNoApiKeyWarning { get; set; }
public string Service { get; set; } public string Service { get; set; }