From 2a4496c80e1b0ab713dd8259f214a17f0c4ffdd0 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Fri, 22 Dec 2023 13:52:15 +0100 Subject: [PATCH] Better remember last auto-translate languages - thx vivodi :) Fix #7761 --- src/libse/Common/Settings.cs | 8 ++++++++ src/ui/Forms/Translate/AutoTranslate.Designer.cs | 6 +++--- src/ui/Forms/Translate/AutoTranslate.cs | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/libse/Common/Settings.cs b/src/libse/Common/Settings.cs index 985674967..78def6ebf 100644 --- a/src/libse/Common/Settings.cs +++ b/src/libse/Common/Settings.cs @@ -151,6 +151,7 @@ namespace Nikse.SubtitleEdit.Core.Common public string GoogleApiV2Key { get; set; } public bool GoogleTranslateNoKeyWarningShow { get; set; } public int GoogleApiV1ChunkSize { get; set; } + public string GoogleTranslateLastSourceLanguage { get; set; } public string GoogleTranslateLastTargetLanguage { get; set; } public string AutoTranslateLastName { get; set; } public string AutoTranslateLastUrl { get; set; } @@ -5136,6 +5137,12 @@ $HorzAlign = Center settings.Tools.GoogleApiV1ChunkSize = Convert.ToInt32(subNode.InnerText, CultureInfo.InvariantCulture); } + subNode = node.SelectSingleNode("GoogleTranslateLastSourceLanguage"); + if (subNode != null) + { + settings.Tools.GoogleTranslateLastSourceLanguage = subNode.InnerText; + } + subNode = node.SelectSingleNode("GoogleTranslateLastTargetLanguage"); if (subNode != null) { @@ -11648,6 +11655,7 @@ $HorzAlign = Center textWriter.WriteElementString("GoogleApiV2Key", settings.Tools.GoogleApiV2Key); textWriter.WriteElementString("GoogleTranslateNoKeyWarningShow", settings.Tools.GoogleTranslateNoKeyWarningShow.ToString(CultureInfo.InvariantCulture)); textWriter.WriteElementString("GoogleApiV1ChunkSize", settings.Tools.GoogleApiV1ChunkSize.ToString(CultureInfo.InvariantCulture)); + textWriter.WriteElementString("GoogleTranslateLastSourceLanguage", settings.Tools.GoogleTranslateLastSourceLanguage); textWriter.WriteElementString("GoogleTranslateLastTargetLanguage", settings.Tools.GoogleTranslateLastTargetLanguage); textWriter.WriteElementString("AutoTranslateLastName", settings.Tools.AutoTranslateLastName); textWriter.WriteElementString("AutoTranslateLastUrl", settings.Tools.AutoTranslateLastUrl); diff --git a/src/ui/Forms/Translate/AutoTranslate.Designer.cs b/src/ui/Forms/Translate/AutoTranslate.Designer.cs index ad0575bfc..f1c9a0b8f 100644 --- a/src/ui/Forms/Translate/AutoTranslate.Designer.cs +++ b/src/ui/Forms/Translate/AutoTranslate.Designer.cs @@ -322,13 +322,13 @@ this.comboBoxSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSource.DropDownWidth = 140; this.comboBoxSource.FormattingEnabled = true; - this.comboBoxSource.Location = new System.Drawing.Point(321, 25); + this.comboBoxSource.Location = new System.Drawing.Point(318, 25); this.comboBoxSource.MaxLength = 32767; this.comboBoxSource.Name = "comboBoxSource"; this.comboBoxSource.SelectedIndex = -1; this.comboBoxSource.SelectedItem = null; this.comboBoxSource.SelectedText = ""; - this.comboBoxSource.Size = new System.Drawing.Size(121, 23); + this.comboBoxSource.Size = new System.Drawing.Size(125, 23); this.comboBoxSource.TabIndex = 94; this.comboBoxSource.UsePopupWindow = false; // @@ -351,7 +351,7 @@ this.comboBoxTarget.SelectedIndex = -1; this.comboBoxTarget.SelectedItem = null; this.comboBoxTarget.SelectedText = ""; - this.comboBoxTarget.Size = new System.Drawing.Size(121, 23); + this.comboBoxTarget.Size = new System.Drawing.Size(125, 23); this.comboBoxTarget.TabIndex = 95; this.comboBoxTarget.UsePopupWindow = false; this.comboBoxTarget.SelectedIndexChanged += new System.EventHandler(this.comboBoxTarget_SelectedIndexChanged); diff --git a/src/ui/Forms/Translate/AutoTranslate.cs b/src/ui/Forms/Translate/AutoTranslate.cs index d4ff8013c..2d9538505 100644 --- a/src/ui/Forms/Translate/AutoTranslate.cs +++ b/src/ui/Forms/Translate/AutoTranslate.cs @@ -368,8 +368,9 @@ namespace Nikse.SubtitleEdit.Forms.Translate private void SetupLanguageSettings() { - FillComboWithLanguages(comboBoxSource, _autoTranslator.GetSupportedSourceLanguages()); - var sourceLanguageIsoCode = EvaluateDefaultSourceLanguageCode(_encoding, _subtitle); + var sourceLanguages = _autoTranslator.GetSupportedSourceLanguages(); + FillComboWithLanguages(comboBoxSource, sourceLanguages); + var sourceLanguageIsoCode = EvaluateDefaultSourceLanguageCode(_encoding, _subtitle, sourceLanguages); SelectLanguageCode(comboBoxSource, sourceLanguageIsoCode); FillComboWithLanguages(comboBoxTarget, _autoTranslator.GetSupportedTargetLanguages()); @@ -381,6 +382,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate { var i = 0; var threeLetterLanguageCode = Iso639Dash2LanguageCode.GetThreeLetterCodeFromTwoLetterCode(languageIsoCode); + foreach (TranslationPair item in comboBox.Items) { if (!string.IsNullOrEmpty(item.TwoLetterIsoLanguageName) && item.TwoLetterIsoLanguageName == languageIsoCode) @@ -447,7 +449,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate } } - public static string EvaluateDefaultSourceLanguageCode(Encoding encoding, Subtitle subtitle) + public static string EvaluateDefaultSourceLanguageCode(Encoding encoding, Subtitle subtitle, List sourceLanguages) { var defaultSourceLanguageCode = LanguageAutoDetect.AutoDetectGoogleLanguage(encoding); // Guess language via encoding if (string.IsNullOrEmpty(defaultSourceLanguageCode)) @@ -455,6 +457,13 @@ namespace Nikse.SubtitleEdit.Forms.Translate defaultSourceLanguageCode = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle); // Guess language based on subtitle contents } + if (!string.IsNullOrEmpty(Configuration.Settings.Tools.GoogleTranslateLastSourceLanguage) && + Configuration.Settings.Tools.GoogleTranslateLastTargetLanguage.StartsWith(defaultSourceLanguageCode) && + sourceLanguages.Any(p=>p.Code == Configuration.Settings.Tools.GoogleTranslateLastSourceLanguage)) + { + return Configuration.Settings.Tools.GoogleTranslateLastSourceLanguage; + } + return defaultSourceLanguageCode; } @@ -607,6 +616,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate TwoLetterIsoTarget = target.TwoLetterIsoLanguageName; var mergeErrorCount = 0; + Configuration.Settings.Tools.GoogleTranslateLastSourceLanguage = source.TwoLetterIsoLanguageName ?? source.Code; Configuration.Settings.Tools.GoogleTranslateLastTargetLanguage = target.TwoLetterIsoLanguageName ?? target.Code; try {