private IEnumerable <LocalizationItem> LoadLocalizationData(string cultureName)
        {
            string xlsPath = Path.GetFullPath(filePath);

            if (!File.Exists(xlsPath))
            {
                throw new FileNotFoundException("localization file not found", xlsPath);
            }

            var wb         = WorkbookFactory.Create(filePath);
            var items      = new List <LocalizationItem>();
            var enumerator = wb.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var sheet = (ISheet)enumerator.Current;
                var rows  = ReadSheet(sheet);
                if (rows.Count < 2)
                {
                    // sheet is empty
                    continue;
                }
                string scope = rows.FirstOrDefault()?.FirstOrDefault();
                if (scope == null)
                {
                    throw new InvalidDataException("The cell A1 must contains value of Scope");
                }

                var headerRow = rows[1];
                Tuple <int, int> dataIndexes = GetIndexes(headerRow, cultureName);
                int keyCol     = dataIndexes.Item1;
                int cultureCol = dataIndexes.Item2;
                rows.RemoveAt(0);
                rows.RemoveAt(0);
                foreach (var row in rows)
                {
                    string key   = row[keyCol];
                    string value = row[cultureCol];

                    var item = new LocalizationItem(scope, key, value);
                    items.Add(item);
                }
            }
            return(items);
        }
Exemple #2
0
        /// <summary>
        /// 加载资源数据
        /// </summary>
        /// <param name="cultrueName"></param>
        /// <exception cref="FormatException">XML文档格式不正确,详细出错信息见异常的提示信息</exception>
        /// <returns></returns>
        private IEnumerable <LocalizationItem> LoadLocalizationData(string cultrueName)
        {
            // the root element, its name must be the same as RootName
            XElement root = xmlDocument.Root;

            if (root == null || root.Name != RootTagName)
            {
                throw new FormatException("XML文档格式错误,根节点必须是" + RootTagName);
            }

            var locDictionary = root.Elements(DictionaryTagName).FirstOrDefault(dict => IsCultureNameMatched(dict, cultrueName));

            if (locDictionary == null)
            {
                throw new FormatException("XML文档中未找到CultureName=\"" + cultrueName + "\"的LocalizationDictionary节点");
            }

            var list     = new List <LocalizationItem>();
            var children = locDictionary.Elements(ScopeTagName);

            foreach (XElement scopeNode in children)
            {
                var    attr      = scopeNode.Attribute("Name");
                string scopeName = attr == null ? "" : attr.Value;
                foreach (var child in scopeNode.Elements(ItemTagName))
                {
                    string key = GetAttributeOrSubnodeValue(child, KeyName);
                    // if key is null(means not found) or is an empty string, skipping this one
                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }
                    string value = GetAttributeOrSubnodeValue(child, ValueName);
                    // if value is not found, skipping this one
                    if (value == null)
                    {
                        continue;
                    }
                    var newItem = new LocalizationItem(scopeName, key, value);
                    list.Add(newItem);
                }
            }
            return(list);
        }