Add support to filter translation files via regex | Fixes Issue #5

This commit is contained in:
Marcel Haßlinger 2021-04-24 22:11:06 +02:00
parent 5f1d96d91d
commit 36403b7eeb
9 changed files with 79 additions and 13 deletions

View File

@ -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<Translations> callback);
void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer<Translations> 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<Boolean> callback);
void save(@NotNull Project project, @NotNull Translations translations,
@NotNull String directoryPath, @NotNull Consumer<Boolean> callback);
}

View File

@ -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<Translations> callback) {
public void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer<Translations> 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<Boolean> callback) {
public void save(@NotNull Project project, @NotNull Translations translations,
@NotNull String directoryPath, @NotNull Consumer<Boolean> callback) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
ApplicationManager.getApplication().runWriteAction(() -> {

View File

@ -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<Translations> callback) {
public void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer<Translations> 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<Boolean> callback) {
public void save(@NotNull Project project, @NotNull Translations translations,
@NotNull String directoryPath, @NotNull Consumer<Boolean> callback) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
ApplicationManager.getApplication().runWriteAction(() -> {

View File

@ -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<Translations> callback) {
public void read(@NotNull Project project, @NotNull String directoryPath, @NotNull Consumer<Translations> 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<Boolean> callback) {
public void save(@NotNull Project project, @NotNull Translations translations,
@NotNull String directoryPath, @NotNull Consumer<Boolean> callback) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
for(String locale : translations.getLocales()) {

View File

@ -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;
}

View File

@ -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<Boolean> callback) {
@ -91,7 +91,7 @@ public class DataStore {
}
TranslatorIO io = IOUtil.determineFormat(localesPath);
io.save(translations, localesPath, callback);
io.save(project, translations, localesPath, callback);
}
/**

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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