fix translation file pattern configuration #46

This commit is contained in:
Marcel Haßlinger 2021-09-21 16:18:07 +02:00
parent e4410c264c
commit 45cae6e7ae
3 changed files with 31 additions and 26 deletions

View File

@ -10,6 +10,9 @@
### Changed
- Optimized i18n key completion
### Fixed
- Locale file pattern configuration
## [1.4.1]
### Added
- Support for IntelliJ 2021.2

View File

@ -77,7 +77,7 @@ public class DataStore {
synchronize(searchQuery, null);
} else {
TranslatorIO io = IOUtil.determineFormat(localesPath);
TranslatorIO io = IOUtil.determineFormat(project, localesPath);
io.read(project, localesPath, (loadedTranslations) -> {
this.translations = loadedTranslations == null ? Translations.empty() : loadedTranslations;
@ -97,7 +97,7 @@ public class DataStore {
return;
}
TranslatorIO io = IOUtil.determineFormat(localesPath);
TranslatorIO io = IOUtil.determineFormat(project, localesPath);
io.save(project, translations, localesPath, callback);
}

View File

@ -10,8 +10,6 @@ import de.marhali.easyi18n.service.SettingsService;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.Optional;
/**
* IO operations utility.
@ -21,48 +19,52 @@ public class IOUtil {
/**
* Determines the {@link TranslatorIO} which should be used for the specified directoryPath
* @param project Current intellij project
* @param directoryPath The full path to the parent directory which holds the translation files
* @return IO handler to use for file operations
*/
public static TranslatorIO determineFormat(@NotNull String directoryPath) {
public static TranslatorIO determineFormat(@NotNull Project project, @NotNull String directoryPath) {
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(directoryPath));
if(directory == null || directory.getChildren() == null) {
throw new IllegalArgumentException("Specified folder is invalid (" + directoryPath + ")");
}
Optional<VirtualFile> any = Arrays.stream(directory.getChildren()).findAny();
VirtualFile[] children = directory.getChildren();
if(!any.isPresent()) {
throw new IllegalStateException("Could not determine i18n format. At least one locale file must be defined");
}
// Split files - Should be always JSON
if(any.get().isDirectory()) {
for(VirtualFile file : children) {
if(file.isDirectory()) { // Modularized locale files
// ATM we only support modularized JSON files
return new ModularizedJsonTranslatorIO();
}
switch (any.get().getFileType().getDefaultExtension().toLowerCase()) {
if(!isFileRelevant(project, file)) {
continue;
}
switch(file.getFileType().getDefaultExtension().toLowerCase()) {
case "json":
return new JsonTranslatorIO();
case "properties":
return new PropertiesTranslatorIO();
case "yml":
return new YamlTranslatorIO();
default:
throw new UnsupportedOperationException("Unsupported i18n locale file format: " +
any.get().getFileType().getDefaultExtension());
System.err.println("Unsupported i18n locale file format: "
+ file.getFileType().getDefaultExtension());
}
}
throw new IllegalStateException("Could not determine i18n format. At least one locale file must be defined");
}
/**
* 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) {
public static boolean isFileRelevant(@NotNull Project project, @NotNull VirtualFile file) {
String pattern = SettingsService.getInstance(project).getState().getFilePattern();
return file.getName().matches(pattern);
}