implement new folder strategies
This commit is contained in:
parent
4d11319217
commit
8b752480db
@ -0,0 +1,41 @@
|
||||
package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a specific translation file directory structure.
|
||||
* @author marhali
|
||||
*/
|
||||
public abstract class FolderStrategy {
|
||||
|
||||
protected final @NotNull SettingsState settings;
|
||||
|
||||
public FolderStrategy(@NotNull SettingsState settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the translation folder for matching files based on the implementing strategy.
|
||||
* The provided directory is already checked as a directory and can be used to query child items.
|
||||
* @param localesDirectory Configured translation file directory
|
||||
* @return translation files which matches the strategy
|
||||
*/
|
||||
public abstract List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory);
|
||||
|
||||
/**
|
||||
* Checks if the provided file is not a directory and matches the configured file pattern
|
||||
* @param file File to check
|
||||
* @return true if file matches and should be processed
|
||||
*/
|
||||
protected boolean isFileRelevant(@NotNull VirtualFile file) {
|
||||
return !file.isDirectory() && FilenameUtils.wildcardMatch(file.getName(), this.settings.getFilePattern());
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Modularized translation folder strategy by locale.
|
||||
* Directory => en dir => user.file / account.file
|
||||
* @author marhali
|
||||
*/
|
||||
public class ModularLocaleFolderStrategy extends FolderStrategy {
|
||||
|
||||
public ModularLocaleFolderStrategy(@NotNull SettingsState settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory) {
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(VirtualFile localeModuleDir : localesDirectory.getChildren()) {
|
||||
if(localeModuleDir.isDirectory()) {
|
||||
String locale = localeModuleDir.getNameWithoutExtension();
|
||||
|
||||
for(VirtualFile namespaceFile : localeModuleDir.getChildren()) {
|
||||
if(super.isFileRelevant(namespaceFile)) {
|
||||
files.add(new TranslationFile(namespaceFile, locale, namespaceFile.getNameWithoutExtension()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Modular translation folder strategy by namespace.
|
||||
* Directory => user dir => en.file / de.file
|
||||
* @author marhali
|
||||
*/
|
||||
public class ModularNamespaceFolderStrategy extends FolderStrategy {
|
||||
|
||||
public ModularNamespaceFolderStrategy(@NotNull SettingsState settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory) {
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(VirtualFile namespaceModuleDir : localesDirectory.getChildren()) {
|
||||
if(namespaceModuleDir.isDirectory()) {
|
||||
String namespace = namespaceModuleDir.getNameWithoutExtension();
|
||||
|
||||
for(VirtualFile localeFile : namespaceModuleDir.getChildren()) {
|
||||
if(super.isFileRelevant(localeFile)) {
|
||||
files.add(new TranslationFile(localeFile, localeFile.getNameWithoutExtension(), namespace));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package de.marhali.easyi18n.ionext.folder;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import de.marhali.easyi18n.model.SettingsState;
|
||||
import de.marhali.easyi18n.model.TranslationFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Single directory translation folder strategy.
|
||||
* Every child is recognized as a file for a specific language.
|
||||
* Directory => en.file, de.file, fr.file
|
||||
* @author marhali
|
||||
*/
|
||||
public class SingleFolderStrategy extends FolderStrategy {
|
||||
|
||||
public SingleFolderStrategy(@NotNull SettingsState settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TranslationFile> findFiles(@NotNull VirtualFile localesDirectory) {
|
||||
List<TranslationFile> files = new ArrayList<>();
|
||||
|
||||
for(VirtualFile file : localesDirectory.getChildren()) {
|
||||
if(super.isFileRelevant(file)) {
|
||||
files.add(new TranslationFile(file, file.getNameWithoutExtension(), null));
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
44
src/main/java/de/marhali/easyi18n/model/TranslationFile.java
Normal file
44
src/main/java/de/marhali/easyi18n/model/TranslationFile.java
Normal file
@ -0,0 +1,44 @@
|
||||
package de.marhali.easyi18n.model;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Represents an existing translation file in a context a specific folder strategy.
|
||||
* @author marhali
|
||||
*/
|
||||
public class TranslationFile {
|
||||
|
||||
private final @NotNull VirtualFile virtualFile;
|
||||
private final @NotNull String locale;
|
||||
private final @Nullable String namespace;
|
||||
|
||||
public TranslationFile(@NotNull VirtualFile virtualFile, @NotNull String locale, @Nullable String namespace) {
|
||||
this.virtualFile = virtualFile;
|
||||
this.locale = locale;
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public @NotNull VirtualFile getVirtualFile() {
|
||||
return virtualFile;
|
||||
}
|
||||
|
||||
public @NotNull String getLocale() {
|
||||
return locale;
|
||||
}
|
||||
|
||||
public @Nullable String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TranslationFile{" +
|
||||
"virtualFile=" + virtualFile +
|
||||
", locale='" + locale + '\'' +
|
||||
", namespace='" + namespace + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user