Skip to content

Commit

Permalink
Better remember last auto-translate languages - thx vivodi :)
Browse files Browse the repository at this point in the history
Fix #7761
  • Loading branch information
niksedk committed Dec 22, 2023
1 parent 7ab7ba2 commit 2a4496c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/libse/Common/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public class ToolsSettings
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; }
Expand Down Expand Up @@ -5136,6 +5137,12 @@ private static Settings CustomDeserialize(string fileName)
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)
{
Expand Down Expand Up @@ -11648,6 +11655,7 @@ public static string CustomSerialize(Settings settings)
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);
Expand Down
6 changes: 3 additions & 3 deletions src/ui/Forms/Translate/AutoTranslate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions src/ui/Forms/Translate/AutoTranslate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ private void SetAutoTranslatorUrl(string url)

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());
Expand All @@ -381,6 +382,7 @@ public static void SelectLanguageCode(NikseComboBox comboBox, string languageIso
{
var i = 0;
var threeLetterLanguageCode = Iso639Dash2LanguageCode.GetThreeLetterCodeFromTwoLetterCode(languageIsoCode);

foreach (TranslationPair item in comboBox.Items)
{
if (!string.IsNullOrEmpty(item.TwoLetterIsoLanguageName) && item.TwoLetterIsoLanguageName == languageIsoCode)
Expand Down Expand Up @@ -447,14 +449,21 @@ public static void FillComboWithLanguages(NikseComboBox comboBox, IEnumerable<Tr
}
}

public static string EvaluateDefaultSourceLanguageCode(Encoding encoding, Subtitle subtitle)
public static string EvaluateDefaultSourceLanguageCode(Encoding encoding, Subtitle subtitle, List<TranslationPair> sourceLanguages)
{
var defaultSourceLanguageCode = LanguageAutoDetect.AutoDetectGoogleLanguage(encoding); // Guess language via encoding
if (string.IsNullOrEmpty(defaultSourceLanguageCode))
{
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;
}

Expand Down Expand Up @@ -607,6 +616,7 @@ private async void buttonTranslate_Click(object sender, EventArgs e)
TwoLetterIsoTarget = target.TwoLetterIsoLanguageName;
var mergeErrorCount = 0;

Configuration.Settings.Tools.GoogleTranslateLastSourceLanguage = source.TwoLetterIsoLanguageName ?? source.Code;
Configuration.Settings.Tools.GoogleTranslateLastTargetLanguage = target.TwoLetterIsoLanguageName ?? target.Code;
try
{
Expand Down

0 comments on commit 2a4496c

Please sign in to comment.