示例#1
0
        /// <summary>
        /// Validates the views.
        /// </summary>
        /// <param name="projectPath">The project path.</param>
        /// <param name="resourcesDirectoryPath">The resources directory path.</param>
        /// <param name="viewsDirectoryPath">The views directory path.</param>
        /// <returns>List of LocalizedFileItems</returns>
        public static List <LocalizedFileItem> Validate(string projectPath, string resourcesDirectoryPath = @"\Resources\Views", string viewsDirectoryPath = @"\Views")
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, viewsDirectoryPath, "", "*.cshtml");

            var localizedFileItems = new List <LocalizedFileItem>();

            var regex = new Regex(@"ViewLocalization\[""(?<name>\S+)("",|""])");

            foreach (var fileItem in fileItems)
            {
                var content = File.ReadAllText(fileItem.GetFullPath());

                var item = new LocalizedFileItem(projectPath, viewsDirectoryPath, fileItem.RelativePath, fileItem.FileName);

                foreach (Match match in regex.Matches(content))
                {
                    item.Names.Add(match.Groups["name"].Value);
                }

                if (item.Names.Any())
                {
                    localizedFileItems.Add(item);
                }
            }

            LocalizationAssert.Validate(projectPath, resourcesDirectoryPath, localizedFileItems);

            return(localizedFileItems);
        }
示例#2
0
        /// <summary>
        /// Gets the resource file items.
        /// </summary>
        /// <param name="projectPath">The project path.</param>
        /// <param name="directoryPath">The directory path.</param>
        /// <param name="cultures">The cultures.</param>
        /// <returns>List of ResourceFileItems</returns>
        /// <exception cref="System.Exception">Throws exception if resource file name is not correct or resource file must be added.</exception>
        public static List <ResourceFileItem> GetResourceFileItems(string projectPath, string directoryPath, ref HashSet <string> cultures)
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, directoryPath, "", "*.resx");

            var list = new List <ResourceFileItem>();

            var regex = new Regex(@"^(?<fileName>[^.]+)(.(?<culture>[^.]+))?.resx");

            var xmlSerializer = new XmlSerializer(typeof(Resx));

            foreach (var fileItem in fileItems)
            {
                var match = regex.Match(fileItem.FileName);

                if (match.Success)
                {
                    var resourceItem = list
                                       .FirstOrDefault(
                        x => x.RelativePath == fileItem.RelativePath &&
                        x.FileName == $"{match.Groups["fileName"].Value}.resx"
                        );

                    if (resourceItem == null)
                    {
                        resourceItem = new ResourceFileItem(projectPath, directoryPath, fileItem.RelativePath, $"{match.Groups["fileName"].Value}.resx");

                        list.Add(resourceItem);
                    }

                    cultures.Add(match.Groups["culture"].Value);

                    Resx resx;

                    using (var stream = new FileStream(fileItem.GetFullPath(), FileMode.Open))
                    {
                        resx = (Resx)xmlSerializer.Deserialize(stream);
                    }

                    resourceItem.Values.Add(match.Groups["culture"].Value, resx);
                }
                else
                {
                    throw new Exception($@"Resource file '{fileItem.GetFullRelativePath()}' has invalid name.");
                }
            }

            foreach (var item in list)
            {
                foreach (var culture in cultures)
                {
                    if (!item.Values.ContainsKey(culture))
                    {
                        throw new Exception($@"Resource file '{item.GetFullRelativePath(culture)}' must be added.");
                    }
                }
            }

            return(list);
        }
        /// <summary>
        /// Validates the models.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="projectPath">The project path.</param>
        /// <param name="resourcesDirectoryPath">The resources directory path.</param>
        /// <param name="modelsDirectoryPath">The models directory path.</param>
        /// <returns>List of LocalizedFileItems</returns>
        public static List <LocalizedFileItem> Validate(Assembly assembly, string projectPath, string resourcesDirectoryPath = @"\Resources\Models", string modelsDirectoryPath = @"\Models")
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, modelsDirectoryPath, "", "*.cs");

            var localizedFileItems = new List <LocalizedFileItem>();

            var namespaceRegex = new Regex(@"namespace (?<namespace>\S+)");
            var classRegex     = new Regex(@"public class (?<class>\S+)");

            foreach (var fileItem in fileItems)
            {
                var content = File.ReadAllText(fileItem.GetFullPath());

                var @namespace = namespaceRegex.Match(content).Groups["namespace"].Value;
                var @class     = classRegex.Match(content).Groups["class"].Value;

                var type = assembly.GetType($"{@namespace}.{@class}");

                var item = new LocalizedFileItem(projectPath, modelsDirectoryPath, fileItem.RelativePath, fileItem.FileName);

                foreach (var property in type.GetRuntimeProperties())
                {
                    var displayAttribute = property.GetCustomAttribute <DisplayAttribute>();

                    if (displayAttribute == null)
                    {
                        continue;
                    }

                    if (!string.IsNullOrWhiteSpace(displayAttribute.Name))
                    {
                        item.Names.Add(displayAttribute.Name);
                    }
                    if (!string.IsNullOrWhiteSpace(displayAttribute.Prompt))
                    {
                        item.Names.Add(displayAttribute.Prompt);
                    }
                    if (!string.IsNullOrWhiteSpace(displayAttribute.Description))
                    {
                        item.Names.Add(displayAttribute.Description);
                    }
                }

                if (item.Names.Any())
                {
                    localizedFileItems.Add(item);
                }
            }

            LocalizationAssert.Validate(projectPath, resourcesDirectoryPath, localizedFileItems);

            return(localizedFileItems);
        }
        /// <summary>
        /// Validates the controllers.
        /// </summary>
        /// <param name="projectPath">The project path.</param>
        /// <param name="resourcesDirectoryPath">The resources directory path.</param>
        /// <param name="controllersDirectoryPath">The controllers directory path.</param>
        /// <param name="predefinedLocalizedFileItems">The predefined localized file items.</param>
        /// <returns>
        /// List of LocalizedFileItems
        /// </returns>
        public static List <LocalizedFileItem> Validate(string projectPath, string resourcesDirectoryPath = @"\Resources\Controllers", string controllersDirectoryPath = @"\Controllers", List <LocalizedFileItem> predefinedLocalizedFileItems = null)
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, controllersDirectoryPath, "", "*Controller.cs");

            var localizedFileItems = new List <LocalizedFileItem>();

            var regex = new Regex(@"_controllerLocalization\[""(?<name>\S+)("",|""])");

            foreach (var fileItem in fileItems)
            {
                var content = File.ReadAllText(fileItem.GetFullPath());

                var item = new LocalizedFileItem(projectPath, controllersDirectoryPath, fileItem.RelativePath, fileItem.FileName);

                foreach (Match match in regex.Matches(content))
                {
                    item.Names.Add(match.Groups["name"].Value);
                }

                var predefinedLocalizedFileItem = predefinedLocalizedFileItems?.FirstOrDefault(x => x.RelativePath == item.RelativePath && x.FileNameWithoutExtension == item.FileNameWithoutExtension);

                if (predefinedLocalizedFileItem != null)
                {
                    foreach (var name in predefinedLocalizedFileItem.Names)
                    {
                        item.Names.Add(name);
                    }
                }

                if (item.Names.Any())
                {
                    localizedFileItems.Add(item);
                }
            }

            LocalizationAssert.Validate(projectPath, resourcesDirectoryPath, localizedFileItems);

            return(localizedFileItems);
        }