turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

PostBuild blacklist wildcards #303

Closed Sshnyari closed 7 years ago

Sshnyari commented 7 years ago

Hi,

Is it possible to add suport for wildcards (*, ?) in i18n.BlackList ?

For the moment, I created a custom app setting (ExtendedBlackList) and a postbuild app that uses it to fill i18n.BlackList with the correct values, before doing the same job as i18n.PostBuild.exe

For example, it takes : "*/subfolder/" from i18n.ExtendedBlackList and puts "folder1/subfolder;folder2/subfolder" into i18n.BlackList

Here is the code portion that does that :

IEnumerable<string> blacklist = blacklistsetting.Split(';'); string editedblacklist = string.Join(";", TransformTokens(blacklist, root)); config.SetSetting("i18n.BlackList", editedblacklist);

` private static string allToken = "*"; private static string oneToken = "?";

    private static IEnumerable<string> TransformTokens(IEnumerable<string> paths, string root)
    {
        return paths.SelectMany(p => TransformToken(p, root));
    }

    private static IEnumerable<string> TransformToken(string path, string root)
    {
        List<string> paths = new List<string>();
        if (HasSearchCharacter(path))
        {
            string[] parts = path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            paths = GetPaths(parts, root).ToList();
        }
        else
        {
            paths.Add(path);
        }
        return paths;
    }

    private static IEnumerable<string> GetPaths(string[] parts, string root)
    {
        if (parts == null || parts.Length == 0)
        {
            return new [] { root };
        }

        List<string> paths = new List<string>();
        if (HasSearchCharacter(parts[0]))
        {
            string[] list = Directory.GetDirectories(root, parts[0]);
            foreach (string path in list)
            {
                paths.AddRange(GetPaths(parts.Skip(1).ToArray(), path));
            }
        }
        else
        {
            return GetPaths(parts.Skip(1).ToArray(), Path.Combine(root, parts[0]));
        }

        return paths;
    }

    private static string GetRootPath(AbstractSettingService settingService)
    {
        var startPath = Path.GetDirectoryName(settingService.GetConfigFileLocation());
        if (startPath != null)
            return Path.GetFullPath(startPath);
        return null;
    }

    private static bool HasSearchCharacter(string s)
    {
        return s.Contains(allToken) || s.Contains(oneToken);
    }

`

turquoiseowl commented 7 years ago

Sounds like a nice feature. If you would care to submit a PR (prefereably with unit tests) then we can add it.

Sshnyari commented 7 years ago

Here is a patch containing changes for 303 and 304 (simpler to apply since there are new files used for both) file.txt