I18NCommander/I18N Commander/Processor/DeepL.cs

152 lines
5.7 KiB
C#
Raw Normal View History

2022-09-17 10:30:02 +00:00
using DataModel.Database;
using DeepL;
namespace Processor;
public static class DeepL
{
private static bool DEEPL_NOT_AVAILABLE = false;
public readonly record struct DeepLUsage(bool Enabled, long CharacterCount, long CharacterLimit, bool AuthIssue = false);
public static async Task<DeepLUsage> GetUsage()
{
if(DEEPL_NOT_AVAILABLE)
return new DeepLUsage(false, 0, 1);
if (await AppSettings.GetDeepLMode() == SettingDeepLMode.DISABLED)
return new DeepLUsage(false, 0, 1);
var deepLAPIKey = await AppSettings.GetDeepLAPIKey();
if(string.IsNullOrWhiteSpace(deepLAPIKey))
return new DeepLUsage(false, 0, 1);
try
{
using var deepl = new Translator(deepLAPIKey);
var usage = await deepl.GetUsageAsync();
return new DeepLUsage(true, usage.Character!.Count, usage.Character.Limit);
}
catch (AuthorizationException)
2022-09-17 10:30:02 +00:00
{
DEEPL_NOT_AVAILABLE = true;
return new DeepLUsage(false, 0, 1, true);
}
}
public static async Task<string> Translate(string text, string targetCulture)
{
if (DEEPL_NOT_AVAILABLE)
return string.Empty;
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
2022-09-17 10:30:02 +00:00
if (await AppSettings.GetDeepLMode() == SettingDeepLMode.DISABLED)
return string.Empty;
var deepLAPIKey = await AppSettings.GetDeepLAPIKey();
if(string.IsNullOrWhiteSpace(deepLAPIKey))
return string.Empty;
try
{
var sourceCultureIndex = await AppSettings.GetDeepLSourceCultureIndex();
var sourceCulture = await AppSettings.GetCultureCode(sourceCultureIndex);
2022-09-17 10:30:02 +00:00
using var deepl = new Translator(deepLAPIKey);
2023-02-12 20:20:39 +00:00
var translation = await deepl.TranslateTextAsync(text, TransformSourceCulture(sourceCulture), TransformTargetCulture(targetCulture));
2022-09-17 10:30:02 +00:00
return translation.Text;
}
catch (AuthorizationException e)
{
Console.WriteLine($"DeepL authorization failed: {e.Message}");
DEEPL_NOT_AVAILABLE = true;
return string.Empty;
}
catch (DeepLException e)
2022-09-17 10:30:02 +00:00
{
Console.WriteLine($"DeepL issue: {e.Message}");
2022-09-17 10:30:02 +00:00
DEEPL_NOT_AVAILABLE = true;
return string.Empty;
}
}
public static void ResetState() => DEEPL_NOT_AVAILABLE = false;
2023-02-12 20:20:39 +00:00
private static string TransformSourceCulture(string sourceCulture) => sourceCulture switch
{
['b', 'g', ..] => "BG", // Bulgarian
['c', 's', ..] => "CS", // Czech
['d', 'a', ..] => "DA", // Danish
['d', 'e', ..] => "DE", // German
['e', 'l', ..] => "EL", // Greek
['e', 'n', ..] => "EN", // English
['e', 's', ..] => "ES", // Spanish
['e', 't', ..] => "ET", // Estonian
['f', 'i', ..] => "FI", // Finnish
['f', 'r', ..] => "FR", // French
['h', 'u', ..] => "HU", // Hungarian
['i', 'd', ..] => "ID", // Indonesian
['i', 't', ..] => "IT", // Italian
['j', 'a', ..] => "JA", // Japanese
['k', 'o', ..] => "KO", // Korean
['l', 't', ..] => "LT", // Lithuanian
['l', 'v', ..] => "LV", // Latvian
['n', 'b', ..] => "NB", // Norwegian
['n', 'l', ..] => "NL", // Dutch
['p', 'l', ..] => "PL", // Polish
['p', 't', ..] => "PT", // Portuguese
['r', 'o', ..] => "RO", // Romanian
['r', 'u', ..] => "RU", // Russian
['s', 'k', ..] => "SK", // Slovak
['s', 'l', ..] => "SL", // Slovenian
['s', 'v', ..] => "SV", // Swedish
['t', 'r', ..] => "TR", // Turkish
['u', 'k', ..] => "UK", // Ukrainian
['z', 'h', ..] => "ZH", // Chinese
_ => sourceCulture,
};
private static string TransformTargetCulture(string targetCulture) => targetCulture switch
{
['b', 'g', ..] => "BG", // Bulgarian
['c', 's', ..] => "CS", // Czech
['d', 'a', ..] => "DA", // Danish
['d', 'e', ..] => "DE", // German
['e', 'l', ..] => "EL", // Greek
['e', 'n', '-', 'G', 'B', ..] => "EN-GB", // English (British)
['e', 'n', '-', 'U', 'S', ..] => "EN-US", // English (American)
['e', 'n', ..] => "EN-US", // English (American)
['e', 's', ..] => "ES", // Spanish
['e', 't', ..] => "ET", // Estonian
['f', 'i', ..] => "FI", // Finnish
['f', 'r', ..] => "FR", // French
['h', 'u', ..] => "HU", // Hungarian
['i', 'd', ..] => "ID", // Indonesian
['i', 't', ..] => "IT", // Italian
['j', 'a', ..] => "JA", // Japanese
['k', 'o', ..] => "KO", // Korean
['l', 't', ..] => "LT", // Lithuanian
['l', 'v', ..] => "LV", // Latvian
['n', 'b', ..] => "NB", // Norwegian
['n', 'l', ..] => "NL", // Dutch
['p', 'l', ..] => "PL", // Polish
['p', 't', '-', 'B', 'R', ..] => "PT-BR", // Portuguese (Brazilian)
['p', 't', '-', 'P', 'T', ..] => "PT-PT", // Portuguese (European)
['p', 't', ..] => "PT-PT", // Portuguese (European)
['r', 'o', ..] => "RO", // Romanian
['r', 'u', ..] => "RU", // Russian
['s', 'k', ..] => "SK", // Slovak
['s', 'l', ..] => "SL", // Slovenian
['s', 'v', ..] => "SV", // Swedish
['t', 'r', ..] => "TR", // Turkish
['u', 'k', ..] => "UK", // Ukrainian
['z', 'h', ..] => "ZH", // Chinese
_ => targetCulture,
};
2022-09-17 10:30:02 +00:00
}