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 ### Changed
- Optimized i18n key completion - Optimized i18n key completion
### Fixed
- Locale file pattern configuration
## [1.4.1] ## [1.4.1]
### Added ### Added
- Support for IntelliJ 2021.2 - Support for IntelliJ 2021.2

View File

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

View File

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