diff --git a/CHANGELOG.md b/CHANGELOG.md index 0707971..a88c73e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ # easy-i18n Changelog ## [Unreleased] +### Added +- Regex support for translation file patterns + ### Fixed - Parsing for .properties files diff --git a/src/main/java/de/marhali/easyi18n/io/folder/FolderStrategy.java b/src/main/java/de/marhali/easyi18n/io/folder/FolderStrategy.java index d9161fa..149be8a 100644 --- a/src/main/java/de/marhali/easyi18n/io/folder/FolderStrategy.java +++ b/src/main/java/de/marhali/easyi18n/io/folder/FolderStrategy.java @@ -7,8 +7,8 @@ import de.marhali.easyi18n.io.parser.ParserStrategyType; import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationFile; import de.marhali.easyi18n.settings.ProjectSettings; +import de.marhali.easyi18n.util.WildcardRegexMatcher; -import org.apache.commons.io.FilenameUtils; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -51,7 +51,8 @@ public abstract class FolderStrategy { * @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()); + return !file.isDirectory() + && WildcardRegexMatcher.matchWildcardRegex(file.getName(), settings.getFilePattern()); } /** diff --git a/src/main/java/de/marhali/easyi18n/util/WildcardRegexMatcher.java b/src/main/java/de/marhali/easyi18n/util/WildcardRegexMatcher.java new file mode 100644 index 0000000..0ef4d3d --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/util/WildcardRegexMatcher.java @@ -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; + } + } +} diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 9d46c0e..e7cdeb8 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -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.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.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.tooltip=Validates directories within a module and passes them as a submodule. settings.resource.sorting.title=Sort translation keys alphabetically diff --git a/src/test/java/de/marhali/easyi18n/WildcardRegexMatcherTest.java b/src/test/java/de/marhali/easyi18n/WildcardRegexMatcherTest.java new file mode 100644 index 0000000..f80d7db --- /dev/null +++ b/src/test/java/de/marhali/easyi18n/WildcardRegexMatcherTest.java @@ -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)$")); + } +} \ No newline at end of file