public static DivinityLocaleEntry CreateNewLocaleEntry(DivinityLocaleFile fileData, string key = "NewKey", string content = "") { var rootNode = fileData.Resource.Regions.First().Value; var refNode = fileData.Entries.Cast <DivinityLocaleEntry>().Where(f => f.SourceNode != null).FirstOrDefault().SourceNode; if (refNode != null) { var node = new Node(); node.Parent = rootNode; node.Name = refNode.Name; //Log.Here().Activity($"Node name: {node.Name}"); foreach (var kp in refNode.Attributes) { var att = new NodeAttribute(kp.Value.Type); att.Value = new TranslatedString() { Value = "", Handle = CreateHandle() }; node.Attributes.Add(kp.Key, att); } DivinityLocaleEntry localeEntry = LoadFromNode(node, fileData.Format); localeEntry.Key = key == "NewKey" ? key + (fileData.Entries.Count + 1) : key; localeEntry.Content = content; return(localeEntry); } return(null); }
public static DivinityLocaleFile CreateFileData(string destinationPath, string name) { var resource = CreateLocalizationResource(); var fileData = new DivinityLocaleFile() { Resource = resource, Format = ResourceFormat.LSB }; LoadFromResource(fileData, resource, ResourceFormat.LSB, true); fileData.ChangesUncommitted = true; return(fileData); }
public static bool LoadFromResource(DivinityLocaleFile fileData, Resource resource, ResourceFormat resourceFormat, bool sort = true) { try { if (resourceFormat == ResourceFormat.LSB) { var rootNode = resource.Regions.First().Value; foreach (var entry in rootNode.Children) { foreach (var node in entry.Value) { DivinityLocaleEntry localeEntry = LoadFromNode(node, resourceFormat); localeEntry.Parent = fileData; fileData.Entries.Add(localeEntry); } } } if (resourceFormat == ResourceFormat.LSJ || resourceFormat == ResourceFormat.LSX) { var rootNode = resource.Regions.First().Value; var stringNodes = new List <Node>(); foreach (var nodeList in rootNode.Children) { var nodes = FindTranslatedStringsInNodeList(nodeList); stringNodes.AddRange(nodes); } foreach (var node in stringNodes) { DivinityLocaleEntry localeEntry = LoadFromNode(node, resourceFormat); localeEntry.Parent = fileData; fileData.Entries.Add(localeEntry); } } if (sort) { fileData.Entries = fileData.Entries.OrderBy(e => e.Key).ToList(); } return(true); } catch (Exception ex) { Log.Here().Error($"Error loading from resource: {ex.ToString()}"); return(false); } }
public static int SaveDataFile(DivinityLocaleFile dataFile) { try { if (dataFile.Source != null) { Log.Here().Activity($"Saving '{dataFile.Name}' to '{dataFile.Source}'."); LSLib.LS.ResourceUtils.SaveResource(dataFile.Resource, dataFile.Source, dataFile.Format); Log.Here().Important($"Saved '{dataFile.Source}'."); return(1); } } catch (Exception ex) { Log.Here().Error($"Error saving localizaton resource: {ex.ToString()}"); } return(0); }
public static DivinityLocaleFile LoadResource(string filePath) { var resourceFormat = ResourceFormat.LSB; if (FileExtensionFound(filePath, ".lsj")) { resourceFormat = ResourceFormat.LSJ; } var resource = LSLib.LS.ResourceUtils.LoadResource(filePath, resourceFormat); DivinityLocaleFile data = new DivinityLocaleFile() { Resource = resource, Format = resourceFormat, Source = filePath, Name = Path.GetFileName(filePath) }; LoadFromResource(data, resource, resourceFormat); return(data); }
public static List <ILocaleData> ImportFilesAsEntries(IEnumerable <string> files, DivinityLocaleFile fileData) { List <ILocaleData> newEntryList = new List <ILocaleData>(); try { foreach (var path in files) { Log.Here().Activity($"Checking file '{path}'"); if (FileExtensionFound(path, ".lsb", ".lsj")) { Log.Here().Activity($"Creating entries from resource."); var tempData = LoadResource(path); newEntryList.AddRange(tempData.Entries); } else if (FileExtensionFound(path, ".txt", ".tsv", ".csv")) { Log.Here().Activity($"Creating entries from delimited text file."); char delimiter = '\t'; if (FileExtensionFound(path, ".csv")) { delimiter = ','; } string line = String.Empty; using (var stream = new System.IO.StreamReader(path)) { int lineNum = 0; while ((line = stream.ReadLine()) != null) { lineNum += 1; // Skip top line, as it typically describes the columns if (lineNum == 1 && line.Contains("Key\tContent")) { continue; } var parts = line.Split(delimiter); var key = parts.ElementAtOrDefault(0); var content = parts.ElementAtOrDefault(1); if (key == null) { key = "NewKey"; } if (content == null) { content = ""; } var entry = CreateNewLocaleEntry(fileData, key, content); newEntryList.Add(entry); } stream.Close(); } } } } catch (Exception ex) { Log.Here().Error("Error importing files as entries to the localization editor: " + ex.ToString()); } return(newEntryList); }
public static List <DivinityLocaleFile> ImportFilesAsData(IEnumerable <string> files, string targetDirectory) { List <DivinityLocaleFile> newFileDataList = new List <DivinityLocaleFile>(); try { foreach (var path in files) { if (FileExtensionFound(path, ".lsb", ".lsj")) { var fileData = LoadResource(path); newFileDataList.Add(fileData); } else if (FileExtensionFound(path, ".txt", ".tsv", ".csv")) { char delimiter = '\t'; if (FileExtensionFound(path, ".csv")) { delimiter = ','; } string line = String.Empty; using (var stream = new System.IO.StreamReader(path)) { string sourcePath = Path.Combine(targetDirectory, Path.GetFileNameWithoutExtension(path), ".lsb"); string name = Path.GetFileName(path); DivinityLocaleFile fileData = CreateFileData(sourcePath, name); int lineNum = 0; while ((line = stream.ReadLine()) != null) { lineNum += 1; // Skip top line, as it typically describes the columns Log.Here().Activity(line); if (lineNum == 1 && line.Contains("Key\tContent")) { continue; } var parts = line.Split(delimiter); var key = parts.ElementAtOrDefault(0); var content = parts.ElementAtOrDefault(1); if (key == null) { key = "NewKey"; } if (content == null) { content = ""; } var entry = CreateNewLocaleEntry(fileData, key, content); fileData.Entries.Add(entry); } //Remove the empty default new key if (fileData.Entries.Count > 1) { fileData.Entries.Remove(fileData.Entries.First()); } newFileDataList.Add(fileData); } } } } catch (Exception ex) { Log.Here().Error("Error importing files to the localization editor: " + ex.ToString()); } return(newFileDataList); }