public static StringResources Read(string sourceLanguage, string targetLanguage, FileInfo inputFile) { using var package = new ExcelPackage(inputFile); ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; int row = 1; // Read the header, to make sure this file contains translated strings if ((string.CompareOrdinal(NameHeader, worksheet.Cells[row, 1].Value as string) != 0) || (string.CompareOrdinal(IndexHeader, worksheet.Cells[row, 2].Value as string) != 0) || (string.CompareOrdinal(sourceLanguage, worksheet.Cells[row, 3].Value as string) != 0) || (string.CompareOrdinal(targetLanguage, worksheet.Cells[row, 4].Value as string) != 0) || (string.CompareOrdinal(FinalHeader, worksheet.Cells[row, 5].Value as string) != 0)) { // This file does not contain translations throw new ArgumentException(nameof(inputFile)); } ++row; // This file contains translations StringResources targetStrings = new StringResources(targetLanguage, isSourceLanguage: false); var name = worksheet.Cells[row, 1].Value as string; while (!string.IsNullOrEmpty(name)) { ResourceType resourceType = ResourceType.String; string indexString = null; ushort index = 0; { var indexObject = worksheet.Cells[row, 2].Value; if (indexObject != null) { indexString = indexObject as string; if (indexString != null) { indexString = indexString.Trim(); if (indexString.Length > 0) { resourceType = ResourceType.Plurals; } } else { index = (ushort)((double)indexObject); resourceType = ResourceType.StringArray; } } } var sourceString = worksheet.Cells[row, 3].Value as string; var targetString = worksheet.Cells[row, 4].Value as string; bool final = worksheet.Cells[row, 5].Value is string finalString?finalString.StartsWith("y", ignoreCase : true, CultureInfo.InvariantCulture) : false; StringResource stringResource; if (resourceType == ResourceType.String) { if (targetStrings.Strings.TryGetValue(name, out stringResource)) { throw new InvalidDataException(string.Format("A string resource with name {0:s} appears more than once", name)); } stringResource = new StringResource(ResourceType.String) { Name = name }; ((StringContent)stringResource.Content).Value = targetString; if (final) { var source = new StringResource(ResourceType.String) { Name = name }; ((StringContent)source.Content).Value = sourceString; stringResource.Source = source; } targetStrings.Strings.Add(name, stringResource); } else { if (resourceType == ResourceType.StringArray) { // This is a string array resource bool indexOrderError = false; if (targetStrings.Strings.TryGetValue(name, out stringResource)) { // Not the first item in the string array var content = (StringArrayContent)stringResource.Content; if (content.Values.Count == index) { content.Values.Add(targetString); if (stringResource.Source != null) { // Previous items were all marked final if (final) { ((StringArrayContent)stringResource.Source.Content).Values.Add(sourceString); } else { stringResource.Source = null; // This is not final, so the array is not final } } // else: previous was not final, so the array is not final } else { indexOrderError = true; } } else { // This is the first of the items in the string array if (index == 0) { stringResource = new StringResource(ResourceType.StringArray) { Name = name }; ((StringArrayContent)stringResource.Content).Values.Add(targetString); if (final) { var source = new StringResource(ResourceType.StringArray) { Name = name }; ((StringArrayContent)source.Content).Values.Add(sourceString); stringResource.Source = source; } targetStrings.Strings.Add(name, stringResource); } else { indexOrderError = true; // The first line of the string array resourcer is not at index 0 } } if (indexOrderError) { throw new InvalidDataException(string.Format("The items in the string-array resource {0:s} are not arranged in increasing order of index", name)); } } else { // This is a plurals string resource if (targetStrings.Strings.TryGetValue(name, out stringResource)) { // Not the first item in the plurals array var content = (PluralsContent)stringResource.Content; if (content.Values.ContainsKey(indexString)) { throw new InvalidDataException(string.Format("An item {0:s} of the plurals resource {1:s} has been duplicated", indexString, name)); } else { content.Values.Add(indexString, targetString); if (stringResource.Source != null) { // Previous items were all marked final if (final) { ((PluralsContent)stringResource.Source.Content).Values.Add(indexString, sourceString); } else { stringResource.Source = null; // This is not final, so the array is not final } } // else: previous was not final, so the array is not final } } else { // This is the first item we have seen in this plurals array stringResource = new StringResource(ResourceType.Plurals) { Name = name }; ((PluralsContent)stringResource.Content).Values.Add(indexString, targetString); if (final) { var source = new StringResource(ResourceType.Plurals) { Name = name }; ((PluralsContent)source.Content).Values.Add(indexString, sourceString); stringResource.Source = source; } targetStrings.Strings.Add(name, stringResource); } } } ++row; name = worksheet.Cells[row, 1].Value as string; } return(targetStrings); }
public uint Read(string fileName, XmlReader reader) { uint count = 0; if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } if (!reader.ReadToFollowing(ResourcesElementName) || reader.IsEmptyElement) { return(count); // Not a resources file or an empty resources file } if (!reader.Read()) { throw new ArgumentException("Reader ended unexpectedly"); } // This is a resources file - read the resources List <string> commentLines = null; while (reader.NodeType != XmlNodeType.EndElement) { if (reader.NodeType == XmlNodeType.Comment) { if (commentLines == null) { commentLines = new List <string>(); } commentLines.Add(reader.Value); reader.Skip(); continue; } ResourceType resourceType = ResourceType.Other; if (reader.NodeType == XmlNodeType.Element) { resourceType = StringResource.GetResourceType(reader.LocalName); } if (resourceType == ResourceType.Other) { // We don't care about this XML - skip it if (commentLines != null) { commentLines.Clear(); // We don't care about comments that are not before strings } reader.Skip(); continue; } // This is a string resource var stringResource = new StringResource(resourceType, reader) { FileName = fileName }; if ((commentLines != null) && (commentLines.Count > 0)) { if (this.IsSourceLanguage) { // If this is a string resource in the source language, save the comments stringResource.CommentLines = new List <string>(commentLines); } else { // This is a string resource in a translated language // See if we can get the source string for this string from the last comment stringResource.TrySetSourceFromComment(commentLines[commentLines.Count - 1]); } commentLines.Clear(); } if (this.Strings.ContainsKey(stringResource.Name)) { throw new DuplicateStringResourceException(stringResource.Name); } this.Strings.Add(stringResource.Name, stringResource); ++count; } reader.ReadEndElement(); return(count); }