1
1
package one .edee .babylon .export ;
2
2
3
- import com .deepl .api .DeepLException ;
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 ;
3
+ import lombok .RequiredArgsConstructor ;
8
4
import lombok .extern .apachecommons .CommonsLog ;
5
+ import one .edee .babylon .config .SupportedTranslators ;
6
+ import one .edee .babylon .config .TranslationConfiguration ;
9
7
import one .edee .babylon .db .SnapshotUtils ;
10
8
import one .edee .babylon .export .dto .ExportResult ;
11
9
import one .edee .babylon .export .dto .TranslationSheet ;
10
+ import one .edee .babylon .export .translator .Translator ;
12
11
import one .edee .babylon .sheets .SheetsException ;
13
12
import one .edee .babylon .sheets .gsheets .model .ASheet ;
14
13
import one .edee .babylon .snapshot .TranslationSnapshotWriteContract ;
15
14
import one .edee .babylon .util .AntPathResourceLoader ;
16
15
import one .edee .babylon .util .PathUtils ;
17
16
import org .jetbrains .annotations .NotNull ;
17
+ import org .springframework .context .ApplicationContext ;
18
+ import org .springframework .util .Assert ;
18
19
import org .springframework .util .StringUtils ;
19
20
20
21
import java .io .File ;
25
26
import java .util .function .Function ;
26
27
import java .util .stream .Collectors ;
27
28
28
- import static com .google .cloud .translate .Translate .TranslateOption .sourceLanguage ;
29
- import static com .google .cloud .translate .Translate .TranslateOption .targetLanguage ;
29
+ import static java .util .Optional .ofNullable ;
30
30
31
31
/**
32
32
* Performs the export phase that generates translation sheets.
33
33
*/
34
34
@ CommonsLog
35
+ @ RequiredArgsConstructor
35
36
public class Exporter {
36
37
private static final String COMBINING_SHEET_NAME = "ALL" ;
37
38
38
39
40
+ private final ApplicationContext applicationContext ;
39
41
private final TranslationCollector translationCollector ;
40
42
private final TranslationSnapshotWriteContract snapshot ;
41
43
private final SheetContract gsc ;
42
44
private final AntPathResourceLoader resourceLoader ;
43
- private final PathUtils pu ;
44
-
45
- public Exporter (TranslationCollector translationCollector , TranslationSnapshotWriteContract snapshot , SheetContract gsc , AntPathResourceLoader resourceLoader ) {
46
- this .translationCollector = translationCollector ;
47
- this .snapshot = snapshot ;
48
- this .gsc = gsc ;
49
- this .resourceLoader = resourceLoader ;
50
- this .pu = new PathUtils ();
51
- }
45
+ private final PathUtils pu = new PathUtils ();
52
46
53
- /**
54
- * Walks message file paths, gathering messages and translations, producing translation sheets in given GSheet spreadsheet.
55
- *
56
- * @param patternPaths paths of message files to export
57
- * @param translationLangs languages to translate messages to
58
- * @param spreadsheetId id of GSheets spreadsheet, must be empty
59
- * @param snapshotPath path to the translation snapshot file
60
- */
61
- public void walkPathsAndWriteSheets (List <String > patternPaths ,
62
- List <String > translationLangs ,
63
- String spreadsheetId ,
64
- Path snapshotPath ,
65
- boolean combineSheets ,
66
- String translatorApiKey ) {
67
- walkPathsAndWriteSheets (patternPaths , translationLangs , spreadsheetId , snapshotPath , Collections .emptyList (), combineSheets , translatorApiKey , null );
68
- }
69
47
70
48
/**
71
49
* Walks message file paths, gathering messages and translations, producing translation sheets in given GSheet spreadsheet.
72
50
*
73
- * @param patternPaths paths of message files to export
74
- * @param translationLangs languages to translate messages to
51
+ * @param configuration configuration of translation run
75
52
* @param spreadsheetId id of GSheets spreadsheet, must be empty
76
- * @param snapshotPath path to the translation snapshot file
77
- * @param lockedCellEditors list of Google account emails, these account will have the permission to edit locked cells
78
- * @param translatorApiKey
79
53
*/
80
- public void walkPathsAndWriteSheets (List <String > patternPaths ,
81
- List <String > translationLangs ,
54
+ public void walkPathsAndWriteSheets (TranslationConfiguration configuration ,
82
55
String spreadsheetId ,
83
- Path snapshotPath ,
84
- List <String > lockedCellEditors ,
85
- boolean combineSheets ,
86
- String translatorApiKey ,
87
- String defaultLang ) {
56
+ boolean combineSheets ) {
57
+ List <String > patternPaths = configuration .getPath ();
88
58
warnDuplicatePaths (patternPaths );
89
59
90
60
List <ASheet > prevSheets = listAllSheets (spreadsheetId );
@@ -95,7 +65,7 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
95
65
throw new IllegalArgumentException ("Please fix the message file paths in the configuration file." );
96
66
}
97
67
98
- ExportResult result = translationCollector .walkPathsAndCollectTranslationSheets (allUniquePaths , translationLangs );
68
+ ExportResult result = translationCollector .walkPathsAndCollectTranslationSheets (allUniquePaths , configuration . getMutations () );
99
69
100
70
if (combineSheets ) {
101
71
// only for translation debugging
@@ -116,31 +86,43 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
116
86
original .add (new TranslationSheet (COMBINING_SHEET_NAME ,combine ));
117
87
}
118
88
119
- Map <String , List <String >> changed = translateTextsByExternalTool (translatorApiKey , defaultLang , result );
89
+ Map <String , List <String >> changed = translateTextsByExternalTool (configuration , result );
120
90
121
- uploadTranslations (result , spreadsheetId , lockedCellEditors , changed );
91
+ uploadTranslations (result , spreadsheetId , configuration . getLockedCellEditors () , changed );
122
92
123
- updateSnapshotAndWriteToDisk (this .snapshot , result , snapshotPath );
93
+ updateSnapshotAndWriteToDisk (this .snapshot , result , configuration . getSnapshotPath () );
124
94
125
95
List <Integer > prevSheetIds = prevSheets .stream ().map (ASheet ::getId ).collect (Collectors .toList ());
126
96
deleteOldSheets (prevSheetIds , spreadsheetId );
127
97
}
128
98
129
99
@ NotNull
130
- private static Map <String , List <String >> translateTextsByExternalTool (String translatorApiKey , String defaultLang , ExportResult result ) {
100
+ private Map <String , List <String >> translateTextsByExternalTool (TranslationConfiguration configuration , ExportResult result ) {
131
101
Map <String , List <String >> changed = new HashMap <>();
132
102
133
- if (translatorApiKey != null ) {
103
+ if (configuration .getTranslatorApiKey () != null ) {
104
+ SupportedTranslators translatorType = ofNullable (configuration .getTranslator ()).orElse (SupportedTranslators .GOOGLE );
105
+
106
+ Translator translator = applicationContext .getBeansOfType (Translator .class )
107
+ .values ()
108
+ .stream ()
109
+ .filter (i -> i .getSupportedTranslator ().equals (translatorType ))
110
+ .findFirst ()
111
+ .orElseThrow (() -> new IllegalArgumentException ("Cannot find translator bean for type" + translatorType ));
112
+ translator .init (configuration .getTranslatorApiKey ());
113
+
134
114
try {
135
- // Translator translator = new Translator(translatorApiKey);
136
- //noinspection deprecation
137
- Translate translate = TranslateOptions .newBuilder ().setApiKey (translatorApiKey ).build ().getService ();
138
115
for (TranslationSheet sheet : result .getSheets ()) {
139
116
log .info ("Translating sheet " + sheet .getSheetName ());
140
117
141
118
List <List <String >> rows = sheet .getRows ();
142
119
List <String > header = rows .get (0 );
120
+ List <String > originals = rows .stream ().map (i ->i .get (1 )).map (i ->StringUtils .hasText (i ) ? i : "____DUMMY" ).collect (Collectors .toList ());
121
+ Map <String , List <String >> translations = new HashMap <>();
143
122
123
+ for (String lang : header .stream ().skip (2 ).collect (Collectors .toList ())) {
124
+ translations .put (lang , translator .translate (configuration .getDefaultLang (), originals , lang ));
125
+ }
144
126
145
127
for (int i = 1 ; i < rows .size (); i ++) {
146
128
Map <Integer , String > toChange = new HashMap <>();
@@ -152,17 +134,17 @@ private static Map<String, List<String>> translateTextsByExternalTool(String tra
152
134
153
135
String lang = header .get (l );
154
136
155
- if (lang .equals ("en" )) {
156
- lang = "en-GB" ;
157
- }
158
-
159
137
if (StringUtils .hasText (original )) {
160
- String translatedText = getTranslatedTextByGoogle (defaultLang , translate , original , lang );
161
- toChange .put (l , translatedText );
162
-
163
- changed
164
- .computeIfAbsent (sheet .getSheetName (), key -> new LinkedList <>())
165
- .add (i + "_" + l );
138
+ String transOriginal = originals .get (i );
139
+ if (!Objects .equals (original , "____DUMMY" )){
140
+ Assert .isTrue (Objects .equals (transOriginal , original ), "Originals does not equals!" );
141
+ String translatedText = translations .get (lang ).get (i );
142
+ toChange .put (l , translatedText );
143
+
144
+ changed
145
+ .computeIfAbsent (sheet .getSheetName (), key -> new LinkedList <>())
146
+ .add (i + "_" + l );
147
+ }
166
148
}
167
149
}
168
150
}
@@ -181,20 +163,6 @@ private static Map<String, List<String>> translateTextsByExternalTool(String tra
181
163
return changed ;
182
164
}
183
165
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
- }
187
-
188
- private static String getTranslatedTextByGoogle (String defaultLang , Translate translate , String original , String lang ) {
189
-
190
- return translate .translate (
191
- original ,
192
- sourceLanguage (defaultLang ),
193
- targetLanguage (lang ))
194
- .getTranslatedText ();
195
- }
196
-
197
-
198
166
private void warnDuplicatePaths (List <String > patternPaths ) {
199
167
List <String > duplicatePaths = detectDuplicatePatternPaths (patternPaths );
200
168
if (!duplicatePaths .isEmpty ()) {
0 commit comments