コード例 #1
0
        public void TestScanRegex()
        {
            var startupDirectory = FileLocations.StartupDirectory;
            var files            = FileLocations.Scan(new[] { startupDirectory }, new Regex(@".*\.xml"));

            Assert.Contains(files, file => file.Item1.EndsWith("Dapplo.Addons.xml"));
        }
コード例 #2
0
        public void Test_Language_Scan()
        {
            var languageConfig = LanguageConfigBuilder.Create()
                                 .WithSpecificDirectories(
                ScanLocations.GenerateScanDirectories(
#if NET471
                    "net471",
#else
                    "netcoreapp3.0",
#endif
                    "Application.Demo",
                    "Application.Demo.Addon",
                    "Application.Demo.MetroAddon",
                    "Application.Demo.OverlayAddon").ToArray()
                )
                                 .BuildLanguageConfig();


            var filePattern = new Regex(languageConfig.FileNamePattern, RegexOptions.Compiled);

            var groupedFiles = FileLocations.Scan(languageConfig.SpecifiedDirectories, filePattern)
                               .GroupBy(x => x.Item2.Groups["IETF"].Value)
                               .ToDictionary(group => group.Key, group => group.Select(x => x.Item1)
                                             .ToList());

            var foundFiles = FileLocations.Scan(languageConfig.SpecifiedDirectories, filePattern).Select(tuple => tuple.Item1).Select(Path.GetFileName).ToList();

            Assert.Contains("language_addon1-de-DE.xml", foundFiles);
            Assert.Contains("language_addon1-en-US.xml", foundFiles);
        }
コード例 #3
0
        public void TestScanFilePatternToRegex()
        {
            var startupDirectory = FileLocations.StartupDirectory;
            var regex            = FileTools.FilenameToRegex("*", new[] { ".xml" });
            var files            = FileLocations.Scan(new[] { startupDirectory }, regex);

            Assert.Contains(files, file => file.Item1.EndsWith("Dapplo.Addons.xml"));
        }
コード例 #4
0
        /// <summary>
        ///     Helper to create the location of a file
        /// </summary>
        private void ScanFiles(LanguageConfig languageConfig)
        {
            var directories = new List <string>();

            if (languageConfig.SpecifiedDirectories != null)
            {
                directories.AddRange(languageConfig.SpecifiedDirectories);
            }
            if (languageConfig.CheckStartupDirectory)
            {
                var startupDirectory = FileLocations.StartupDirectory;
                if (startupDirectory != null)
                {
                    directories.Add(Path.Combine(startupDirectory, "languages"));
                }
            }
            if (languageConfig.CheckAppDataDirectory)
            {
                var appDataDirectory = FileLocations.RoamingAppDataDirectory(languageConfig.ApplicationName);
                if (appDataDirectory != null)
                {
                    directories.Add(Path.Combine(appDataDirectory, "languages"));
                }
            }

            if (Log.IsDebugEnabled())
            {
                Log.Debug().WriteLine("Scanning directories: {0}", string.Join(",", directories));
            }

            Files = FileLocations.Scan(directories, _filePattern)
                    .GroupBy(x => x.Item2.Groups["IETF"].Value)
                    .ToDictionary(group => group.Key, group => group.Select(x => x.Item1)
                                  .ToList());

            if (Log.IsDebugEnabled())
            {
                Log.Debug().WriteLine("Detected language ietfs: {0}", string.Join(",", Files.Keys));
            }

            var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
                              .ToLookup(e => e.IetfLanguageTag, StringComparer.OrdinalIgnoreCase).ToDictionary(x => x.Key, x => x.First());

            // TODO: Create custom culture for all not available, see: https://msdn.microsoft.com/en-us/library/ms172469(v=vs.90).aspx
            // TODO: Add Embedded resources, especially for the assemblies to which ILanguage interfaces belong

            AvailableLanguages = (from ietf in Files.Keys
                                  where allCultures.ContainsKey(ietf)
                                  select ietf).Distinct().ToDictionary(ietf => ietf, ietf => allCultures[ietf].NativeName);

            if (Log.IsVerboseEnabled())
            {
                Log.Verbose().WriteLine("Languages found: {0}", string.Join(",", AvailableLanguages.Keys));
            }
        }