From 36403b7eebbf7b7004a013c6684be47997147fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Ha=C3=9Flinger?= Date: Sat, 24 Apr 2021 22:11:06 +0200 Subject: [PATCH] Add support to filter translation files via regex | Fixes Issue #5 --- .../java/de/marhali/easyi18n/io/TranslatorIO.java | 9 +++++++-- .../io/implementation/JsonTranslatorIO.java | 13 +++++++++++-- .../ModularizedJsonTranslatorIO.java | 13 +++++++++++-- .../io/implementation/PropertiesTranslatorIO.java | 13 +++++++++++-- .../de/marhali/easyi18n/model/SettingsState.java | 10 ++++++++++ .../de/marhali/easyi18n/service/DataStore.java | 6 +++--- .../marhali/easyi18n/ui/dialog/SettingsDialog.java | 14 ++++++++++++-- src/main/java/de/marhali/easyi18n/util/IOUtil.java | 13 +++++++++++++ src/main/resources/messages.properties | 1 + 9 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/marhali/easyi18n/io/TranslatorIO.java b/src/main/java/de/marhali/easyi18n/io/TranslatorIO.java index 1b99550..91a40d1 100644 --- a/src/main/java/de/marhali/easyi18n/io/TranslatorIO.java +++ b/src/main/java/de/marhali/easyi18n/io/TranslatorIO.java @@ -1,5 +1,7 @@ package de.marhali.easyi18n.io; +import com.intellij.openapi.project.Project; + import de.marhali.easyi18n.model.Translations; import org.jetbrains.annotations.NotNull; @@ -15,16 +17,19 @@ public interface TranslatorIO { /** * Reads localized messages from the persistence layer. + * @param project Opened intellij project * @param directoryPath The full path for the directory which holds all locale files * @param callback Contains loaded translations. Will be called after io operation. Content might be null on failure. */ - void read(@NotNull String directoryPath, @NotNull Consumer callback); + void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer callback); /** * Writes the provided messages (translations) to the persistence layer. + * @param project Opened intellij project * @param translations Translations instance to save * @param directoryPath The full path for the directory which holds all locale files * @param callback Will be called after io operation. Can be used to determine if action was successful(true) or not */ - void save(@NotNull Translations translations, @NotNull String directoryPath, @NotNull Consumer callback); + void save(@NotNull Project project, @NotNull Translations translations, + @NotNull String directoryPath, @NotNull Consumer callback); } \ No newline at end of file diff --git a/src/main/java/de/marhali/easyi18n/io/implementation/JsonTranslatorIO.java b/src/main/java/de/marhali/easyi18n/io/implementation/JsonTranslatorIO.java index 9a2840d..40e0eb1 100644 --- a/src/main/java/de/marhali/easyi18n/io/implementation/JsonTranslatorIO.java +++ b/src/main/java/de/marhali/easyi18n/io/implementation/JsonTranslatorIO.java @@ -2,12 +2,14 @@ package de.marhali.easyi18n.io.implementation; import com.google.gson.*; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import de.marhali.easyi18n.io.TranslatorIO; import de.marhali.easyi18n.model.LocalizedNode; import de.marhali.easyi18n.model.Translations; +import de.marhali.easyi18n.util.IOUtil; import de.marhali.easyi18n.util.JsonUtil; import org.jetbrains.annotations.NotNull; @@ -27,7 +29,7 @@ public class JsonTranslatorIO implements TranslatorIO { private static final String FILE_EXTENSION = "json"; @Override - public void read(@NotNull String directoryPath, @NotNull Consumer callback) { + public void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer callback) { ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added) ApplicationManager.getApplication().runReadAction(() -> { @@ -44,6 +46,11 @@ public class JsonTranslatorIO implements TranslatorIO { try { for(VirtualFile file : files) { + + if(!IOUtil.isFileRelevant(project, file)) { // File does not matches pattern + continue; + } + locales.add(file.getNameWithoutExtension()); JsonObject tree = JsonParser.parseReader(new InputStreamReader(file.getInputStream(), @@ -62,7 +69,9 @@ public class JsonTranslatorIO implements TranslatorIO { } @Override - public void save(@NotNull Translations translations, @NotNull String directoryPath, @NotNull Consumer callback) { + public void save(@NotNull Project project, @NotNull Translations translations, + @NotNull String directoryPath, @NotNull Consumer callback) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); ApplicationManager.getApplication().runWriteAction(() -> { diff --git a/src/main/java/de/marhali/easyi18n/io/implementation/ModularizedJsonTranslatorIO.java b/src/main/java/de/marhali/easyi18n/io/implementation/ModularizedJsonTranslatorIO.java index 00c0e37..0cc70d9 100644 --- a/src/main/java/de/marhali/easyi18n/io/implementation/ModularizedJsonTranslatorIO.java +++ b/src/main/java/de/marhali/easyi18n/io/implementation/ModularizedJsonTranslatorIO.java @@ -5,12 +5,14 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import de.marhali.easyi18n.io.TranslatorIO; import de.marhali.easyi18n.model.LocalizedNode; import de.marhali.easyi18n.model.Translations; +import de.marhali.easyi18n.util.IOUtil; import de.marhali.easyi18n.util.JsonUtil; import org.jetbrains.annotations.NotNull; @@ -31,7 +33,7 @@ public class ModularizedJsonTranslatorIO implements TranslatorIO { private static final String FILE_EXTENSION = "json"; @Override - public void read(@NotNull String directoryPath, @NotNull Consumer callback) { + public void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer callback) { ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added) ApplicationManager.getApplication().runReadAction(() -> { @@ -53,6 +55,11 @@ public class ModularizedJsonTranslatorIO implements TranslatorIO { // Read all json modules for(VirtualFile module : localeDir.getChildren()) { + + if(!IOUtil.isFileRelevant(project, module)) { // File does not matches pattern + continue; + } + JsonObject tree = JsonParser.parseReader(new InputStreamReader(module.getInputStream(), module.getCharset())).getAsJsonObject(); @@ -78,7 +85,9 @@ public class ModularizedJsonTranslatorIO implements TranslatorIO { } @Override - public void save(@NotNull Translations translations, @NotNull String directoryPath, @NotNull Consumer callback) { + public void save(@NotNull Project project, @NotNull Translations translations, + @NotNull String directoryPath, @NotNull Consumer callback) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); ApplicationManager.getApplication().runWriteAction(() -> { diff --git a/src/main/java/de/marhali/easyi18n/io/implementation/PropertiesTranslatorIO.java b/src/main/java/de/marhali/easyi18n/io/implementation/PropertiesTranslatorIO.java index c113a44..5dcfd75 100644 --- a/src/main/java/de/marhali/easyi18n/io/implementation/PropertiesTranslatorIO.java +++ b/src/main/java/de/marhali/easyi18n/io/implementation/PropertiesTranslatorIO.java @@ -1,12 +1,14 @@ package de.marhali.easyi18n.io.implementation; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import de.marhali.easyi18n.io.TranslatorIO; import de.marhali.easyi18n.model.LocalizedNode; import de.marhali.easyi18n.model.Translations; +import de.marhali.easyi18n.util.IOUtil; import de.marhali.easyi18n.util.TranslationsUtil; import org.jetbrains.annotations.NotNull; @@ -24,7 +26,7 @@ public class PropertiesTranslatorIO implements TranslatorIO { public static final String FILE_EXTENSION = "properties"; @Override - public void read(@NotNull String directoryPath, @NotNull Consumer callback) { + public void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer callback) { ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added) ApplicationManager.getApplication().runReadAction(() -> { @@ -41,6 +43,11 @@ public class PropertiesTranslatorIO implements TranslatorIO { try { for (VirtualFile file : files) { + + if(!IOUtil.isFileRelevant(project, file)) { // File does not matches pattern + continue; + } + locales.add(file.getNameWithoutExtension()); Properties properties = new Properties(); properties.load(new InputStreamReader(file.getInputStream(), file.getCharset())); @@ -57,7 +64,9 @@ public class PropertiesTranslatorIO implements TranslatorIO { } @Override - public void save(@NotNull Translations translations, @NotNull String directoryPath, @NotNull Consumer callback) { + public void save(@NotNull Project project, @NotNull Translations translations, + @NotNull String directoryPath, @NotNull Consumer callback) { + ApplicationManager.getApplication().runWriteAction(() -> { try { for(String locale : translations.getLocales()) { diff --git a/src/main/java/de/marhali/easyi18n/model/SettingsState.java b/src/main/java/de/marhali/easyi18n/model/SettingsState.java index 6ece3b0..be0b0f2 100644 --- a/src/main/java/de/marhali/easyi18n/model/SettingsState.java +++ b/src/main/java/de/marhali/easyi18n/model/SettingsState.java @@ -10,8 +10,10 @@ import org.jetbrains.annotations.Nullable; public class SettingsState { public static final String DEFAULT_PREVIEW_LOCALE = "en"; + public static final String DEFAULT_FILE_PATTERN = ".*"; private String localesPath; + private String filePattern; private String previewLocale; public SettingsState() {} @@ -24,6 +26,14 @@ public class SettingsState { this.localesPath = localesPath; } + public @NotNull String getFilePattern() { + return filePattern != null ? filePattern : DEFAULT_FILE_PATTERN; + } + + public void setFilePattern(String filePattern) { + this.filePattern = filePattern; + } + public @NotNull String getPreviewLocale() { return previewLocale != null ? previewLocale : DEFAULT_PREVIEW_LOCALE; } diff --git a/src/main/java/de/marhali/easyi18n/service/DataStore.java b/src/main/java/de/marhali/easyi18n/service/DataStore.java index ecdf86f..319b146 100644 --- a/src/main/java/de/marhali/easyi18n/service/DataStore.java +++ b/src/main/java/de/marhali/easyi18n/service/DataStore.java @@ -63,7 +63,7 @@ public class DataStore { } else { TranslatorIO io = IOUtil.determineFormat(localesPath); - io.read(localesPath, (translations) -> { + io.read(project, localesPath, (translations) -> { if(translations != null) { // Read was successful this.translations = translations; @@ -80,7 +80,7 @@ public class DataStore { } /** - * Saves the current translation state to disk. See {@link TranslatorIO#save(Translations, String, Consumer)} + * Saves the current translation state to disk. See {@link TranslatorIO#save(Project, Translations, String, Consumer)} * @param callback Complete callback. Indicates if operation was successful(true) or not */ public void saveToDisk(@NotNull Consumer callback) { @@ -91,7 +91,7 @@ public class DataStore { } TranslatorIO io = IOUtil.determineFormat(localesPath); - io.save(translations, localesPath, callback); + io.save(project, translations, localesPath, callback); } /** diff --git a/src/main/java/de/marhali/easyi18n/ui/dialog/SettingsDialog.java b/src/main/java/de/marhali/easyi18n/ui/dialog/SettingsDialog.java index 87a4221..eeafe30 100644 --- a/src/main/java/de/marhali/easyi18n/ui/dialog/SettingsDialog.java +++ b/src/main/java/de/marhali/easyi18n/ui/dialog/SettingsDialog.java @@ -24,6 +24,7 @@ public class SettingsDialog { private final Project project; private TextFieldWithBrowseButton pathText; + private JBTextField filePatternText; private JBTextField previewText; public SettingsDialog(Project project) { @@ -32,10 +33,12 @@ public class SettingsDialog { public void showAndHandle() { String localesPath = SettingsService.getInstance(project).getState().getLocalesPath(); + String filePattern = SettingsService.getInstance(project).getState().getFilePattern(); String previewLocale = SettingsService.getInstance(project).getState().getPreviewLocale(); - if(prepare(localesPath, previewLocale).show() == DialogWrapper.OK_EXIT_CODE) { // Save changes + if(prepare(localesPath, filePattern, previewLocale).show() == DialogWrapper.OK_EXIT_CODE) { // Save changes SettingsService.getInstance(project).getState().setLocalesPath(pathText.getText()); + SettingsService.getInstance(project).getState().setFilePattern(filePatternText.getText()); SettingsService.getInstance(project).getState().setPreviewLocale(previewText.getText()); // Reload instance @@ -43,7 +46,7 @@ public class SettingsDialog { } } - private DialogBuilder prepare(String localesPath, String previewLocale) { + private DialogBuilder prepare(String localesPath, String filePattern, String previewLocale) { JPanel rootPanel = new JPanel(new GridLayout(0, 1, 2, 2)); JBLabel pathLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.text")); @@ -56,6 +59,13 @@ public class SettingsDialog { rootPanel.add(pathLabel); rootPanel.add(pathText); + JBLabel filePatternLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.path.file-pattern")); + filePatternText = new JBTextField(filePattern); + + rootPanel.add(filePatternLabel); + rootPanel.add(filePatternText); + + JBLabel previewLabel = new JBLabel(ResourceBundle.getBundle("messages").getString("settings.preview")); previewText = new JBTextField(previewLocale); previewLabel.setLabelFor(previewText); diff --git a/src/main/java/de/marhali/easyi18n/util/IOUtil.java b/src/main/java/de/marhali/easyi18n/util/IOUtil.java index bdb8838..76bfa00 100644 --- a/src/main/java/de/marhali/easyi18n/util/IOUtil.java +++ b/src/main/java/de/marhali/easyi18n/util/IOUtil.java @@ -1,5 +1,6 @@ package de.marhali.easyi18n.util; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import de.marhali.easyi18n.io.implementation.JsonTranslatorIO; @@ -7,6 +8,7 @@ import de.marhali.easyi18n.io.implementation.ModularizedJsonTranslatorIO; import de.marhali.easyi18n.io.implementation.PropertiesTranslatorIO; import de.marhali.easyi18n.io.TranslatorIO; +import de.marhali.easyi18n.service.SettingsService; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -54,4 +56,15 @@ public class IOUtil { any.get().getFileType().getDefaultExtension()); } } + + /** + * Checks if the provided file matches the file pattern specified by configuration + * @param project Current intellij project + * @param file File to check + * @return True if relevant otherwise false + */ + public static boolean isFileRelevant(Project project, VirtualFile file) { + String pattern = SettingsService.getInstance(project).getState().getFilePattern(); + return file.getName().matches(pattern); + } } \ No newline at end of file diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 1076225..0c12843 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -13,4 +13,5 @@ translation.key=Key translation.locales=Locales settings.path.title=Locales Directory settings.path.text=Locales directory +settings.path.file-pattern=Translation file pattern settings.preview=Preview locale \ No newline at end of file