|
1 | 1 | package one.edee.babylon.export;
|
2 | 2 |
|
| 3 | +import com.deepl.api.TextResult; |
| 4 | +import com.deepl.api.Translator; |
| 5 | +import lombok.extern.apachecommons.CommonsLog; |
3 | 6 | import one.edee.babylon.db.SnapshotUtils;
|
4 | 7 | import one.edee.babylon.export.dto.ExportResult;
|
5 | 8 | import one.edee.babylon.export.dto.TranslationSheet;
|
| 9 | +import one.edee.babylon.sheets.SheetsException; |
6 | 10 | import one.edee.babylon.sheets.gsheets.model.ASheet;
|
7 | 11 | import one.edee.babylon.snapshot.TranslationSnapshotWriteContract;
|
8 | 12 | import one.edee.babylon.util.AntPathResourceLoader;
|
9 |
| -import one.edee.babylon.sheets.SheetsException; |
10 | 13 | import one.edee.babylon.util.PathUtils;
|
11 |
| -import lombok.extern.apachecommons.CommonsLog; |
| 14 | +import org.springframework.util.StringUtils; |
12 | 15 |
|
13 | 16 | import java.io.File;
|
14 | 17 | import java.io.IOException;
|
@@ -52,25 +55,28 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
|
52 | 55 | List<String> translationLangs,
|
53 | 56 | String spreadsheetId,
|
54 | 57 | Path snapshotPath,
|
55 |
| - boolean combineSheets ) { |
56 |
| - walkPathsAndWriteSheets(patternPaths, translationLangs, spreadsheetId, snapshotPath, Collections.emptyList(), combineSheets); |
| 58 | + boolean combineSheets, |
| 59 | + String deeplApiKey) { |
| 60 | + walkPathsAndWriteSheets(patternPaths, translationLangs, spreadsheetId, snapshotPath, Collections.emptyList(), combineSheets, deeplApiKey); |
57 | 61 | }
|
58 | 62 |
|
59 | 63 | /**
|
60 | 64 | * Walks message file paths, gathering messages and translations, producing translation sheets in given GSheet spreadsheet.
|
61 | 65 | *
|
62 |
| - * @param patternPaths paths of message files to export |
63 |
| - * @param translationLangs languages to translate messages to |
64 |
| - * @param spreadsheetId id of GSheets spreadsheet, must be empty |
65 |
| - * @param snapshotPath path to the translation snapshot file |
| 66 | + * @param patternPaths paths of message files to export |
| 67 | + * @param translationLangs languages to translate messages to |
| 68 | + * @param spreadsheetId id of GSheets spreadsheet, must be empty |
| 69 | + * @param snapshotPath path to the translation snapshot file |
66 | 70 | * @param lockedCellEditors list of Google account emails, these account will have the permission to edit locked cells
|
| 71 | + * @param deeplApiKey |
67 | 72 | */
|
68 | 73 | public void walkPathsAndWriteSheets(List<String> patternPaths,
|
69 | 74 | List<String> translationLangs,
|
70 | 75 | String spreadsheetId,
|
71 | 76 | Path snapshotPath,
|
72 | 77 | List<String> lockedCellEditors,
|
73 |
| - boolean combineSheets) { |
| 78 | + boolean combineSheets, |
| 79 | + String deeplApiKey) { |
74 | 80 | warnDuplicatePaths(patternPaths);
|
75 | 81 |
|
76 | 82 | List<ASheet> prevSheets = listAllSheets(spreadsheetId);
|
@@ -102,7 +108,56 @@ public void walkPathsAndWriteSheets(List<String> patternPaths,
|
102 | 108 | original.add(new TranslationSheet(COMBINING_SHEET_NAME,combine));
|
103 | 109 | }
|
104 | 110 |
|
105 |
| - uploadTranslations(result, spreadsheetId, lockedCellEditors); |
| 111 | + Map<String, List<String>> changed = new HashMap<>(); |
| 112 | + |
| 113 | + if (deeplApiKey != null) { |
| 114 | + try { |
| 115 | + Translator translator = new Translator(deeplApiKey); |
| 116 | + for (TranslationSheet sheet : result.getSheets()) { |
| 117 | + log.info("Translating sheet " + sheet.getSheetName()); |
| 118 | + |
| 119 | + List<List<String>> rows = sheet.getRows(); |
| 120 | + List<String> header = rows.get(0); |
| 121 | + |
| 122 | + |
| 123 | + for (int i = 1; i < rows.size(); i++) { |
| 124 | + Map<Integer, String> toChange = new HashMap<>(); |
| 125 | + |
| 126 | + List<String> cells = rows.get(i); |
| 127 | + String original = cells.get(1); |
| 128 | + for (int l = 2; l < cells.size(); l++) { |
| 129 | + if (StringUtils.isEmpty(cells.get(l))) { |
| 130 | + |
| 131 | + String lang = header.get(l); |
| 132 | + |
| 133 | + if (lang.equals("en")) { |
| 134 | + lang = "en-GB"; |
| 135 | + } |
| 136 | + |
| 137 | + if (StringUtils.hasText(original)) { |
| 138 | + TextResult translatedText = translator.translateText(original, null, lang); |
| 139 | + toChange.put(l, translatedText.getText()); |
| 140 | + |
| 141 | + changed |
| 142 | + .computeIfAbsent(sheet.getSheetName(), key -> new LinkedList<>()) |
| 143 | + .add(i + "_" + l); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + for (Entry<Integer, String> entry : toChange.entrySet()) { |
| 149 | + cells.remove((int) entry.getKey()); |
| 150 | + cells.add(entry.getKey(), entry.getValue()); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + } |
| 155 | + } catch (Exception e) { |
| 156 | + log.error(e.getMessage(), e); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + uploadTranslations(result, spreadsheetId, lockedCellEditors, changed); |
106 | 161 |
|
107 | 162 | updateSnapshotAndWriteToDisk(this.snapshot, result, snapshotPath);
|
108 | 163 |
|
@@ -162,13 +217,13 @@ private List<ASheet> listAllSheets(String spreadsheetId) {
|
162 | 217 | }
|
163 | 218 | }
|
164 | 219 |
|
165 |
| - private void uploadTranslations(ExportResult exportResult, String spreadsheetId, List<String> lockedCellEditors) { |
| 220 | + private void uploadTranslations(ExportResult exportResult, String spreadsheetId, List<String> lockedCellEditors, Map<String, List<String>> changed) { |
166 | 221 | exportResult.getSheets().stream()
|
167 | 222 | .filter(sheet -> !sheet.getDataRows().isEmpty())
|
168 | 223 | .forEach(sheet -> {
|
169 | 224 | try {
|
170 | 225 | log.info("Writing " + sheet.getDataRows().size() + " rows into sheet '" + sheet.getSheetName() + "'.");
|
171 |
| - gsc.createSheet(spreadsheetId, sheet.getSheetName(), sheet.getRows(), lockedCellEditors); |
| 226 | + gsc.createSheet(spreadsheetId, sheet.getSheetName(), sheet.getRows(), lockedCellEditors, changed); |
172 | 227 | } catch (SheetsException e) {
|
173 | 228 | String errMsg = "Error when uploading data to spreadsheet '" + spreadsheetId + "'";
|
174 | 229 | throw new RuntimeException(errMsg, e);
|
@@ -227,10 +282,12 @@ public interface SheetContract {
|
227 | 282 | * @param sheetTitle name to use for the new sheet
|
228 | 283 | * @param sheetRows rows with data cells to fill the sheet with
|
229 | 284 | * @param lockedCellEditors list of email accounts that will be able to edit locked cells
|
| 285 | + * @param changed |
230 | 286 | * @throws SheetsException when unable to upload sheets
|
231 | 287 | */
|
232 |
| - void createSheet(String spreadsheetId, String sheetTitle, List<List<String>> sheetRows, List<String> lockedCellEditors) throws SheetsException; |
| 288 | + void createSheet(String spreadsheetId, String sheetTitle, List<List<String>> sheetRows, List<String> lockedCellEditors, Map<String, List<String>> changed) throws SheetsException; |
233 | 289 |
|
234 | 290 | }
|
235 | 291 |
|
| 292 | + |
236 | 293 | }
|
0 commit comments