|
15 | 15 | import com.thecoderscorner.menu.editorui.generator.EepromSaveMode; |
16 | 16 | import com.thecoderscorner.menu.editorui.generator.ProjectSaveLocation; |
17 | 17 | import com.thecoderscorner.menu.editorui.generator.arduino.CallbackRequirement; |
| 18 | +import com.thecoderscorner.menu.editorui.generator.i18n.I18nCodeGenerator; |
| 19 | +import com.thecoderscorner.menu.editorui.generator.i18n.MultiFileI18nCodeGenerator; |
| 20 | +import com.thecoderscorner.menu.editorui.generator.i18n.SingleFileI18nCodeGenerator; |
18 | 21 | import com.thecoderscorner.menu.editorui.generator.logger.GeneratedFile; |
19 | 22 | import com.thecoderscorner.menu.editorui.generator.logger.UserFeedbackLogger; |
20 | 23 | import com.thecoderscorner.menu.editorui.generator.parameters.CodeGeneratorCapable; |
|
46 | 49 | import static java.time.format.DateTimeFormatter.ISO_INSTANT; |
47 | 50 |
|
48 | 51 | public abstract class CoreCodeGenerator implements CodeGenerator { |
49 | | - public static final String GENERATED_LOCAL_HEADER = "// TcMenu Generated Locale File, do not edit this file."; |
50 | 52 | public static final String LINE_BREAK = System.lineSeparator(); |
51 | 53 | public static final String TWO_LINES = LINE_BREAK + LINE_BREAK; |
52 | 54 | public static final String NO_REMOTE_ID = "2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb"; |
@@ -146,7 +148,13 @@ public boolean startConversion(Path directory, List<CodePluginItem> allPlugins, |
146 | 148 | fileProcessor.dealWithRequiredPlugins(allPlugins, srcDir, directory, psl, previousPluginFiles); |
147 | 149 |
|
148 | 150 | if (localeHandler.isLocalSupportEnabled()) { |
149 | | - processLocale(srcDir, genDir); |
| 151 | + I18nCodeGenerator localeCodeGen; |
| 152 | + if(options.getSaveLocation() == ONE_SINGLE_FILE_MENU_MAIN || options.getSaveLocation() == ONE_SINGLE_FILE) { |
| 153 | + localeCodeGen = new SingleFileI18nCodeGenerator(feedbackLogger, localeHandler); |
| 154 | + } else { |
| 155 | + localeCodeGen = new MultiFileI18nCodeGenerator(feedbackLogger, localeHandler); |
| 156 | + } |
| 157 | + localeCodeGen.processLocale(srcDir, genDir); |
150 | 158 | } |
151 | 159 |
|
152 | 160 | internalConversion(directory, srcDir, callbackFunctions, projectName); |
@@ -184,74 +192,6 @@ private MenuTreeCodeGenerator makeMenuCodeGeneratorForOptions(CodeGeneratorOptio |
184 | 192 | return gen; |
185 | 193 | } |
186 | 194 |
|
187 | | - private void processLocale(Path srcDir, boolean generated) throws IOException { |
188 | | - feedbackLogger.debug("Starting locale processing"); |
189 | | - StringBuilder langSelectText = new StringBuilder(8192); |
190 | | - langSelectText.append(GENERATED_LOCAL_HEADER).append(LINE_BREAK); |
191 | | - langSelectText.append("// This is the header to include. Set TC_LOCAL_?? to a locale").append(LINE_BREAK); |
192 | | - langSelectText.append("// or omit for the default language").append(TWO_LINES); |
193 | | - |
194 | | - var previousLocale = localeHandler.getCurrentLocale(); |
195 | | - |
196 | | - try { |
197 | | - String defaultLocaleFile = toSourceFile(srcDir, "_lang" + ".h", generated); |
198 | | - localeHandler.changeLocale(PropertiesLocaleEnabledHandler.DEFAULT_LOCALE); |
199 | | - var defaultLocaleMap = localeHandler.getUnderlyingMap(); |
200 | | - localeToCpp(defaultLocaleFile, PropertiesLocaleEnabledHandler.DEFAULT_LOCALE, defaultLocaleMap); |
201 | | - boolean useElIf = false; |
202 | | - |
203 | | - for (var locale : localeHandler.getEnabledLocales().stream().filter(l -> !l.getLanguage().isEmpty()).toList()) { |
204 | | - String localeFile = toSourceFile(srcDir, "_lang_" + locale.toString() + ".h", generated); |
205 | | - localeToCpp(localeFile, locale, defaultLocaleMap); |
206 | | - |
207 | | - if (useElIf) { |
208 | | - langSelectText.append("#elif"); |
209 | | - } else { |
210 | | - langSelectText.append("#if"); |
211 | | - useElIf = true; |
212 | | - } |
213 | | - langSelectText.append(" defined(TC_LOCALE_").append(locale.toString().toUpperCase()).append(')') |
214 | | - .append(LINE_BREAK); |
215 | | - langSelectText.append("# include \"").append(Paths.get(localeFile).getFileName()).append("\"").append(LINE_BREAK); |
216 | | - } |
217 | | - |
218 | | - Path defPath = Paths.get(defaultLocaleFile); |
219 | | - if (useElIf) { |
220 | | - langSelectText.append("#else").append(LINE_BREAK).append("#include \"") |
221 | | - .append(defPath.getFileName()).append("\"").append(LINE_BREAK) |
222 | | - .append("#endif").append(LINE_BREAK); |
223 | | - } else { |
224 | | - langSelectText.append("#include \"").append(defPath.getFileName()).append("\"").append(LINE_BREAK); |
225 | | - } |
226 | | - |
227 | | - var selFile = Paths.get(toSourceFile(srcDir, "_langSelect" + ".h", generated)); |
228 | | - Files.writeString(selFile, langSelectText.toString(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
229 | | - feedbackLogger.fileModificiation(GeneratedFile.always(selFile, langSelectText.toString())); |
230 | | - feedbackLogger.info("Wrote out the language selector - " + selFile.getFileName()); |
231 | | - feedbackLogger.debug("Finished locale processing"); |
232 | | - } |
233 | | - finally { |
234 | | - // put back the same locale as was selected before. |
235 | | - localeHandler.changeLocale(previousLocale); |
236 | | - } |
237 | | - } |
238 | | - |
239 | | - private void localeToCpp(String localeOutputFile, Locale locale, Map<String, String> defaultLocaleMap) throws IOException { |
240 | | - StringBuilder sb = new StringBuilder(8192); |
241 | | - sb.append(GENERATED_LOCAL_HEADER).append(" Locale ").append(locale).append(LINE_BREAK); |
242 | | - sb.append("// Never include directly, always include the langSelect header").append(TWO_LINES); |
243 | | - |
244 | | - localeHandler.changeLocale(locale); |
245 | | - var allEntries = localeHandler.getUnderlyingMap(); |
246 | | - for(var entry : defaultLocaleMap.entrySet()) { |
247 | | - var value = (allEntries.containsKey(entry.getKey())) ? allEntries.get(entry.getKey()) : entry.getValue(); |
248 | | - sb.append("#define ").append(asDefine(entry.getKey())).append(" \"").append(value).append("\""); |
249 | | - sb.append(LINE_BREAK); |
250 | | - } |
251 | | - Path path = Path.of(localeOutputFile); |
252 | | - Files.writeString(path, sb.toString(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
253 | | - feedbackLogger.fileModificiation(GeneratedFile.always(path, sb.toString())); |
254 | | - } |
255 | 195 |
|
256 | 196 | public static String asDefine(String key) { |
257 | 197 | var words = key.split("[^A-Za-z0-9]+"); |
|
0 commit comments