1
1
package one .edee .babylon .export ;
2
2
3
- import com .deepl .api .TextResult ;
3
+ import com .deepl .api .DeepLException ;
4
4
import com .deepl .api .Translator ;
5
+ import com .google .api .client .http .HttpRequestInitializer ;
6
+ import com .google .cloud .translate .Translate ;
7
+ import com .google .cloud .translate .TranslateOptions ;
5
8
import lombok .extern .apachecommons .CommonsLog ;
6
9
import one .edee .babylon .db .SnapshotUtils ;
7
10
import one .edee .babylon .export .dto .ExportResult ;
11
14
import one .edee .babylon .snapshot .TranslationSnapshotWriteContract ;
12
15
import one .edee .babylon .util .AntPathResourceLoader ;
13
16
import one .edee .babylon .util .PathUtils ;
17
+ import org .jetbrains .annotations .NotNull ;
14
18
import org .springframework .util .StringUtils ;
15
19
16
20
import java .io .File ;
21
25
import java .util .function .Function ;
22
26
import java .util .stream .Collectors ;
23
27
28
+ import static com .google .cloud .translate .Translate .TranslateOption .sourceLanguage ;
29
+ import static com .google .cloud .translate .Translate .TranslateOption .targetLanguage ;
30
+
24
31
/**
25
32
* Performs the export phase that generates translation sheets.
26
33
*/
@@ -56,8 +63,8 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
56
63
String spreadsheetId ,
57
64
Path snapshotPath ,
58
65
boolean combineSheets ,
59
- String deeplApiKey ) {
60
- walkPathsAndWriteSheets (patternPaths , translationLangs , spreadsheetId , snapshotPath , Collections .emptyList (), combineSheets , deeplApiKey , null );
66
+ String translatorApiKey ) {
67
+ walkPathsAndWriteSheets (patternPaths , translationLangs , spreadsheetId , snapshotPath , Collections .emptyList (), combineSheets , translatorApiKey , null );
61
68
}
62
69
63
70
/**
@@ -68,15 +75,15 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
68
75
* @param spreadsheetId id of GSheets spreadsheet, must be empty
69
76
* @param snapshotPath path to the translation snapshot file
70
77
* @param lockedCellEditors list of Google account emails, these account will have the permission to edit locked cells
71
- * @param deeplApiKey
78
+ * @param translatorApiKey
72
79
*/
73
80
public void walkPathsAndWriteSheets (List <String > patternPaths ,
74
81
List <String > translationLangs ,
75
82
String spreadsheetId ,
76
83
Path snapshotPath ,
77
84
List <String > lockedCellEditors ,
78
85
boolean combineSheets ,
79
- String deeplApiKey ,
86
+ String translatorApiKey ,
80
87
String defaultLang ) {
81
88
warnDuplicatePaths (patternPaths );
82
89
@@ -109,11 +116,25 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
109
116
original .add (new TranslationSheet (COMBINING_SHEET_NAME ,combine ));
110
117
}
111
118
119
+ Map <String , List <String >> changed = translateTextsByExternalTool (translatorApiKey , defaultLang , result );
120
+
121
+ uploadTranslations (result , spreadsheetId , lockedCellEditors , changed );
122
+
123
+ updateSnapshotAndWriteToDisk (this .snapshot , result , snapshotPath );
124
+
125
+ List <Integer > prevSheetIds = prevSheets .stream ().map (ASheet ::getId ).collect (Collectors .toList ());
126
+ deleteOldSheets (prevSheetIds , spreadsheetId );
127
+ }
128
+
129
+ @ NotNull
130
+ private static Map <String , List <String >> translateTextsByExternalTool (String translatorApiKey , String defaultLang , ExportResult result ) {
112
131
Map <String , List <String >> changed = new HashMap <>();
113
132
114
- if (deeplApiKey != null ) {
133
+ if (translatorApiKey != null ) {
115
134
try {
116
- Translator translator = new Translator (deeplApiKey );
135
+ // Translator translator = new Translator(translatorApiKey);
136
+ //noinspection deprecation
137
+ Translate translate = TranslateOptions .newBuilder ().setApiKey (translatorApiKey ).build ().getService ();
117
138
for (TranslationSheet sheet : result .getSheets ()) {
118
139
log .info ("Translating sheet " + sheet .getSheetName ());
119
140
@@ -136,8 +157,8 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
136
157
}
137
158
138
159
if (StringUtils .hasText (original )) {
139
- TextResult translatedText = translator . translateText ( original , defaultLang , lang );
140
- toChange .put (l , translatedText . getText () );
160
+ String translatedText = getTranslatedTextByGoogle ( defaultLang , translate , original , lang );
161
+ toChange .put (l , translatedText );
141
162
142
163
changed
143
164
.computeIfAbsent (sheet .getSheetName (), key -> new LinkedList <>())
@@ -157,15 +178,23 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
157
178
log .error (e .getMessage (), e );
158
179
}
159
180
}
181
+ return changed ;
182
+ }
160
183
161
- uploadTranslations (result , spreadsheetId , lockedCellEditors , changed );
184
+ private static String getTranslatedTextByDeepl (String defaultLang , Translator translator , String original , String lang ) throws DeepLException , InterruptedException {
185
+ return translator .translateText (original , defaultLang , lang ).getText ();
186
+ }
162
187
163
- updateSnapshotAndWriteToDisk ( this . snapshot , result , snapshotPath );
188
+ private static String getTranslatedTextByGoogle ( String defaultLang , Translate translate , String original , String lang ) {
164
189
165
- List <Integer > prevSheetIds = prevSheets .stream ().map (ASheet ::getId ).collect (Collectors .toList ());
166
- deleteOldSheets (prevSheetIds , spreadsheetId );
190
+ return translate .translate (
191
+ original ,
192
+ sourceLanguage (defaultLang ),
193
+ targetLanguage (lang ))
194
+ .getTranslatedText ();
167
195
}
168
196
197
+
169
198
private void warnDuplicatePaths (List <String > patternPaths ) {
170
199
List <String > duplicatePaths = detectDuplicatePatternPaths (patternPaths );
171
200
if (!duplicatePaths .isEmpty ()) {
0 commit comments