private StringResource GetOrCreateStringResource(Dictionary <string, StringResource> fileStringResources, string storageLocation, XlfTransUnit u) { if (!fileStringResources.TryGetValue(u.Id, out StringResource stringResource)) { stringResource = new StringResource(u.Id, u.Optional.Notes.FirstOrDefault()?.Value ?? "") { Name = u.Id, StorageLocation = storageLocation }; stringResource.SetLocaleText(InvariantLanguage, RemoveMultipleWhitespaces(u.Source)); fileStringResources.Add(u.Id, stringResource); } return(stringResource); }
public ICollection <StringResource> Import() { var matchedXlfFile = false; xliffDocumentProvider.LoadXlfDocuments(); foreach (var doc in xliffDocumentProvider.XlfDocuments) { string plainFileName = XliffFileHelpers.GetPlainFileName(baseDirectory, doc.FileName); foreach (var xlfFile in doc.Files) { if (!FileMatchesProjectInvariantLocale(xlfFile)) { continue; } matchedXlfFile = true; string storageLocation = XliffFileHelpers.GetStorageLocation(plainFileName, xlfFile.Original); string locale = GetLocale(doc.FileName, xlfFile); Dictionary <string, StringResource> fileStringResources = GetStringResourcesForFile(storageLocation); foreach (var transUnit in xlfFile.TransUnits) { StringResource stringResource = GetOrCreateStringResource(fileStringResources, storageLocation, transUnit); if (ShouldWriteTargetLanguage(locale, transUnit)) { stringResource.SetLocaleText(locale, transUnit.Target); } } } } ValidateXlfFilesFound(matchedXlfFile, xliffDocumentProvider.XlfDocuments.Count); return(resourcesPerFile.SelectMany(t => t.Value.Select(x => x.Value)).ToList()); }
private ICollection <StringResource> ReadResourceStrings(string filename) { var result = new List <StringResource>(); using (var sr = new System.IO.StreamReader(filename)) { string line = ""; while ((line = sr.ReadLine()) != null) { if (line.TrimStart().StartsWith("#") || line.TrimStart().StartsWith("!") || !line.Contains("=")) { continue; // skip comments or lines without '=' } var keyvalue = line.Split(new char[] { '=' }, 2); if (keyvalue.Length < 2) { continue; // skip invalid lines } string key = keyvalue[0].Trim(); string value = keyvalue[1].TrimStart(); StringResource stringRes = new StringResource(key, ""); // get locale from file name Regex regex = new Regex(localeRegex); var match = regex.Match(filename); string locale = match.Value.TrimStart(new char[] { '_' }); // add locale text stringRes.SetLocaleText(locale, value); result.Add(stringRes); } } return(result); }
private ICollection <StringResource> ReadResourceStrings(string filename) { var result = new List <StringResource>(); XmlDocument doc = new XmlDocument(); doc.Load(filename); string xpath = "/strings/string"; XmlNodeList nodes = doc.SelectNodes(xpath); foreach (XmlNode node in nodes) { if (node.Attributes["key"] == null) { throw new ApplicationException("Invalid XML file, 'key' attribute not found!"); } string key = node.Attributes["key"].InnerText; string comment = node.Attributes["comment"]?.InnerText; string value = node?.InnerText; StringResource stringRes = new StringResource(key, ""); // get locale from file name string locale = Path.GetExtension(Path.GetFileNameWithoutExtension(filename)).TrimStart(new char[] { '.' }); // add locale text stringRes.SetLocaleText(locale, value); // add comment stringRes.Notes = comment; result.Add(stringRes); } return(result); }