support regex file patterns

Resolves #191
This commit is contained in:
marhali 2022-10-28 17:29:29 +02:00
parent 00c1e49fb4
commit c647489981
5 changed files with 64 additions and 3 deletions

View File

@ -3,6 +3,9 @@
# easy-i18n Changelog # easy-i18n Changelog
## [Unreleased] ## [Unreleased]
### Added
- Regex support for translation file patterns
### Fixed ### Fixed
- Parsing for <kbd>.properties</kbd> files - Parsing for <kbd>.properties</kbd> files

View File

@ -7,8 +7,8 @@ import de.marhali.easyi18n.io.parser.ParserStrategyType;
import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationData;
import de.marhali.easyi18n.model.TranslationFile; import de.marhali.easyi18n.model.TranslationFile;
import de.marhali.easyi18n.settings.ProjectSettings; import de.marhali.easyi18n.settings.ProjectSettings;
import de.marhali.easyi18n.util.WildcardRegexMatcher;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
@ -51,7 +51,8 @@ public abstract class FolderStrategy {
* @return true if file matches and should be processed * @return true if file matches and should be processed
*/ */
protected boolean isFileRelevant(@NotNull VirtualFile file) { protected boolean isFileRelevant(@NotNull VirtualFile file) {
return !file.isDirectory() && FilenameUtils.wildcardMatch(file.getName(), this.settings.getFilePattern()); return !file.isDirectory()
&& WildcardRegexMatcher.matchWildcardRegex(file.getName(), settings.getFilePattern());
} }
/** /**

View File

@ -0,0 +1,23 @@
package de.marhali.easyi18n.util;
import org.apache.commons.io.FilenameUtils;
/**
* Utilities for wildcard / regex matching.
* @author marhali
*/
public class WildcardRegexMatcher {
public static boolean matchWildcardRegex(String string, String pattern) {
boolean wildcardMatch = FilenameUtils.wildcardMatchOnSystem(string, pattern);
if(wildcardMatch) {
return true;
}
try {
return string.matches(pattern);
} catch (Exception e) {
return false;
}
}
}

View File

@ -31,7 +31,7 @@ settings.resource.folder.items=Single Directory;Modularized: Locale / Namespace;
settings.resource.folder.tooltip=What is the folder structure of your translation files? settings.resource.folder.tooltip=What is the folder structure of your translation files?
settings.resource.parser.items=JSON;JSON5;YAML;YML;Properties;ARB settings.resource.parser.items=JSON;JSON5;YAML;YML;Properties;ARB
settings.resource.parser.tooltip=Which file parser should be used to process your translation files? settings.resource.parser.tooltip=Which file parser should be used to process your translation files?
settings.resource.file-pattern.tooltip=Defines a wildcard matcher to filter relevant translation files. For example *.json, *.??? or *.*. settings.resource.file-pattern.tooltip=Defines a wildcard matcher or regex pattern to filter relevant translation files. For example *.json, *.??? or *.*.
settings.resource.nesting.title=Consider subdirectories for modularized translation files settings.resource.nesting.title=Consider subdirectories for modularized translation files
settings.resource.nesting.tooltip=Validates directories within a module and passes them as a submodule. settings.resource.nesting.tooltip=Validates directories within a module and passes them as a submodule.
settings.resource.sorting.title=Sort translation keys alphabetically settings.resource.sorting.title=Sort translation keys alphabetically

View File

@ -0,0 +1,34 @@
package de.marhali.easyi18n;
import de.marhali.easyi18n.util.WildcardRegexMatcher;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit tests for {@link WildcardRegexMatcher}.
* @author marhali
*/
public class WildcardRegexMatcherTest extends WildcardRegexMatcher {
@Test
public void testWildcard() {
Assert.assertTrue(matchWildcardRegex("en.json", "*.json"));
Assert.assertTrue(matchWildcardRegex("de.json", "*.json"));
Assert.assertFalse(matchWildcardRegex("index.html", "*.json"));
Assert.assertTrue(matchWildcardRegex("en.json", "*.*"));
Assert.assertFalse(matchWildcardRegex("file", "*.*"));
Assert.assertTrue(matchWildcardRegex("en.txt", "*.???"));
Assert.assertFalse(matchWildcardRegex("en.json", "*.???"));
}
@Test
public void testRegex() {
Assert.assertTrue(matchWildcardRegex("en.json", "^(en|de)\\.json"));
Assert.assertFalse(matchWildcardRegex("gb.json", "^(en|de)\\.json"));
Assert.assertTrue(matchWildcardRegex("en.jpg", "^.*\\.(jpg|JPG|gif|GIF)$"));
Assert.assertFalse(matchWildcardRegex("en.json", "^.*\\.(jpg|JPG|gif|GIF)$"));
}
}