当前位置: 首页>>代码示例>>C#>>正文


C# Key.FillOriginalKeyWithEnglish方法代码示例

本文整理汇总了C#中Key.FillOriginalKeyWithEnglish方法的典型用法代码示例。如果您正苦于以下问题:C# Key.FillOriginalKeyWithEnglish方法的具体用法?C# Key.FillOriginalKeyWithEnglish怎么用?C# Key.FillOriginalKeyWithEnglish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Key的用法示例。


在下文中一共展示了Key.FillOriginalKeyWithEnglish方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseLine

        /// <summary>
        /// Parses a line from the csv file and constructs a key from it.
        /// </summary>
        /// <param name="line">The line to be parsed.</param>
        /// <param name="languages">The languages</param>
        /// <param name="fillMissing">Whether to fill the missing values</param>
        /// <returns>The key constructed from the line of csv. Null if line is irrelevant.</returns>
        public static Key ParseLine(string line, List<string> languages, bool fillMissing = false)
        {
            // return null if line is invalid or irrelevant
            if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("//") || line.Trim().StartsWith("\"//") ||
                line.Contains("LANGUAGE,"))
            {
                return null;
            }

            // replace all whitespace with spaces
            line = Regex.Replace(line, @"\s+", " ");

            // split the string by commas taking into account that commas might be in quotes
            var resultArray = Regex.Split(line.Trim(), ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

            // the first value becomes the key
            var result = new Key
            {
                Id = resultArray[0]
            };

            // if mismatch in number of values do nothing with the extra
            // if (resultArray.GetLength(0) - 1 != languages.Count) throw new ApplicationException(string.Format("The provided line contains more values than specified languages: {0}", line));

            // add the languages and the values to the dictionary
            for (int i = 1; i <= languages.Count; i++)
            {
                // trim
                resultArray[i] = resultArray[i].Trim();

                // remove unnecessary quotes
                if (resultArray[i].StartsWith("\"") && resultArray[i].EndsWith("\""))
                {
                    resultArray[i] = resultArray[i].Substring(1, resultArray[i].Length - (1 * 2));
                }

                try
                {
                    result.GetType().GetProperty(languages[i - 1]).SetValue(result, resultArray[i]);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(string.Format("Could not set key value. Lang: {1}, Val: {2}. Error: {0}", ex.Message, languages[i - 1], resultArray[i]));
                }
            }

            result.FillOriginalKeyWithEnglish();

            // fill the languages that were not found
            if (fillMissing)
            {
                result.FillEmptyKeysWithEnglishOrOriginal();
            }

            return result;
        }
开发者ID:nikolauska,项目名称:StringForge,代码行数:63,代码来源:CsvParser.cs


注:本文中的Key.FillOriginalKeyWithEnglish方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。