示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="projectPath">
        /// C:\Users\hyzx8\Source\Repos\OrchardCore\OrchardCore\src\OrchardCore.Modules\OrchardCore.Liquid\
        /// </param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List <PoModel> SearchKeysFromFile(string projectPath, string filePath)
        {
            if (!projectPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
            {
                projectPath = projectPath + Path.AltDirectorySeparatorChar;
            }
            var projectRootName = projectPath.Split(new char[] { Path.AltDirectorySeparatorChar },
                                                    StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

            var msgctxt = filePath.Replace(projectPath, string.Empty);

            var poList      = new List <PoModel>();
            var fileContent = File.ReadAllText(filePath);

            if (filePath.ToLower().EndsWith("cshtml"))
            {
                msgctxt = msgctxt.Replace(".cshtml", string.Empty);
                Regex regex = new Regex(@"(([@.*T])|(@T))[[]""(?<KeyName>.*)""(,.*)?[]]");

                foreach (Match match in regex.Matches(fileContent))
                {
                    GroupCollection groups = match.Groups;
                    var             strKey = groups["KeyName"].Value;
                    var             po     = new PoModel {
                        Msgctxt = msgctxt, MsgId = strKey, MsgStr = strKey
                    };
                    WriteLog(po.ToString());
                    poList.Add(po);
                }
            }
            else if (filePath.ToLower().EndsWith("cs"))
            {
                msgctxt = msgctxt.Replace(".cs", string.Empty);
                Regex regex = new Regex(@"([HTS]|TS)[[]""(?<KeyName>.*)((,.* ""[]])|(""[]]))");

                foreach (Match match in regex.Matches(fileContent))
                {
                    GroupCollection groups = match.Groups;

                    var strKey = groups["KeyName"].Value;
                    var po     = new PoModel {
                        Msgctxt = msgctxt, MsgId = strKey, MsgStr = strKey
                    };

                    poList.Add(po);
                }
            }

            return(poList);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="projectPath">
        /// C:\Users\hyzx8\Source\Repos\OrchardCore\OrchardCore\src\OrchardCore.Modules\OrchardCore.Liquid\
        /// </param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List <PoModel> SearchKeysFromFile(string projectPath, string filePath)
        {
            if (!projectPath.EndsWith("\\"))
            {
                projectPath = projectPath + "\\";
            }
            var projectRootName = projectPath.Split(new char[] { '\\' },
                                                    StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

            string sourceFileName = projectRootName + "." + filePath.Replace(projectPath, string.Empty);
            var    msgctxt        = sourceFileName.Replace('\\', '.');
            var    poList         = new List <PoModel>();
            var    fileContent    = File.ReadAllText(filePath);

            if (filePath.ToLower().EndsWith("cshtml"))
            {
                msgctxt = msgctxt.Replace(".cshtml", string.Empty);
                Regex regex = new Regex(@"(?:(@?T[[])"")(?'KeyName'.+?)(?:(""[],]))",
                                        RegexOptions.Singleline | RegexOptions.Multiline |
                                        RegexOptions.IgnorePatternWhitespace |
                                        RegexOptions.ExplicitCapture);
                foreach (Match match in regex.Matches(fileContent))
                {
                    GroupCollection groups = match.Groups;
                    var             strKey = groups["KeyName"].Value;
                    var             po     = new PoModel
                    {
                        FullFilePath = sourceFileName,
                        Msgctxt      = msgctxt,
                        MsgId        = strKey,
                        MsgStr       = strKey
                    };
                    WriteLog(po.ToString());
                    poList.Add(po);
                }
            }
            else if (filePath.ToLower().EndsWith("cs"))
            {
                msgctxt = msgctxt.Replace(".cs", string.Empty);
                Regex regex = new Regex(@"([HTS]|TS)[[]""(?<KeyName>.+?)((,.* ""[]])|(""[]]))");


                Regex ns = new Regex(@"(?:namespace\s)(?'namespace'(\w|\d|[.])*)(?:\b)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

                var mc     = ns.Match(fileContent);
                var nsName = mc.Groups["namespace"].Value;

                Regex clnm    = new Regex(@"(?:\sclass\s)(?'class'(\w|[.])+?)(?:\b)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                var   clMatch = clnm.Match(fileContent);
                var   clname  = clMatch.Groups["class"].Value;


                foreach (Match match in regex.Matches(fileContent))
                {
                    GroupCollection groups = match.Groups;

                    var strKey = groups["KeyName"].Value;
                    var po     = new PoModel
                    {
                        FullFilePath = sourceFileName,
                        Msgctxt      = nsName + "." + clname,
                        MsgId        = strKey,
                        MsgStr       = strKey
                    };
                    poList.Add(po);
                }
            }

            return(poList);
        }