forked from lokinmodar/Echoglossian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslationService.cs
134 lines (112 loc) · 3.8 KB
/
TranslationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// <copyright file="TranslationService.cs" company="lokinmodar">
// Copyright (c) lokinmodar. All rights reserved.
// Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License license.
// </copyright>
using System;
using System.Threading.Tasks;
using Dalamud.Game.Text.Sanitizer;
using Dalamud.Plugin.Services;
using static Echoglossian.Echoglossian;
namespace Echoglossian
{
public class TranslationService
{
private readonly ITranslator translator;
private readonly Sanitizer sanitizer;
public TranslationService(Config config, IPluginLog pluginLog, Sanitizer sanitizer)
{
this.sanitizer = sanitizer;
TransEngines chosenEngine = (TransEngines)config.ChosenTransEngine;
switch (chosenEngine)
{
case TransEngines.Google:
this.translator = new GoogleTranslator(pluginLog);
break;
case TransEngines.Deepl:
this.translator = new DeepLTranslator(pluginLog, config.DeeplTranslatorUsingApiKey, config.DeeplTranslatorApiKey);
break;
case TransEngines.ChatGPT:
this.translator = new ChatGPTTranslator(pluginLog, config.ChatGptApiKey);
break;
case TransEngines.Bing:
break;
case TransEngines.Yandex:
break;
case TransEngines.GTranslate:
break;
case TransEngines.Amazon:
break;
case TransEngines.Azure:
break;
case TransEngines.GoogleCloud:
break;
case TransEngines.All:
break;
// ... add cases for other translation engines
default:
throw new NotSupportedException($"Translation engine {chosenEngine} is not supported.");
}
}
public string Translate(string text, string sourceLanguage, string targetLanguage)
{
var (sanitizedText, shouldTranslate) = this.CheckTextToTranslate(text);
if (!shouldTranslate)
{
return sanitizedText;
}
string startingEllipsis = string.Empty;
string parsedText = sanitizedText;
if (text.StartsWith("..."))
{
startingEllipsis = "...";
parsedText = text.Substring(3);
}
string finalDialogueText = this.translator.Translate(parsedText, sourceLanguage, targetLanguage);
finalDialogueText = !string.IsNullOrEmpty(startingEllipsis)
? startingEllipsis + finalDialogueText
: finalDialogueText;
return finalDialogueText;
}
public async Task<string> TranslateAsync(string text, string sourceLanguage, string targetLanguage)
{
var (sanitizedText, shouldTranslate) = this.CheckTextToTranslate(text);
if (!shouldTranslate)
{
return sanitizedText;
}
string startingEllipsis = string.Empty;
string parsedText = sanitizedText;
if (text.StartsWith("..."))
{
startingEllipsis = "...";
parsedText = text.Substring(3);
}
string finalDialogueText = await this.translator.TranslateAsync(parsedText, sourceLanguage, targetLanguage);
finalDialogueText = !string.IsNullOrEmpty(startingEllipsis)
? startingEllipsis + finalDialogueText
: finalDialogueText;
return finalDialogueText;
}
private (string SanitizedText, bool ShouldTranslate) CheckTextToTranslate(string text)
{
if (string.IsNullOrEmpty(text))
{
return (string.Empty, false);
}
string sanitizedString = this.sanitizer.Sanitize(text);
if (string.IsNullOrEmpty(sanitizedString))
{
return (string.Empty, false);
}
if (sanitizedString == "...")
{
return (sanitizedString, false);
}
if (sanitizedString == "???")
{
return (sanitizedString, false);
}
return (sanitizedString, true);
}
}
}