using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; namespace Crowdin.Plugin.Helpers.FileHelpers { using Models.Translations; using Models.Translations.Items; using Server_API.Helpers; public static class ReadFileHelper { public static void ParseFileToTranslation(string filepath, string tableName) { var stringList = new List(); var parsedList = new List(); var streamReader = new StreamReader(filepath); var outLine = string.Empty; while (!streamReader.EndOfStream) { string fileLine = streamReader.ReadLine(); outLine += fileLine; // ReSharper disable once InvertIf if (outLine.EndsWith("\"")) { stringList.Add(outLine); outLine = string.Empty; } else { outLine += Environment.NewLine; } } streamReader.Close(); foreach (var line in stringList) { string[] separatingStrings = { "\",\"" }; var temp = line.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries); for (var j = 0; j < temp.Length; j++) { var tempChars = temp[j].ToCharArray().ToList(); if (tempChars[tempChars.Count - 1] is '"') { tempChars.RemoveAt(tempChars.Count - 1); } if (tempChars.Count > 0 && tempChars[0] is '"') { tempChars.RemoveAt(0); } temp[j] = new string(tempChars.ToArray()).Replace("\"\"", "\""); } if (temp.Length > 0) { parsedList.Add(temp); } } ParseData(parsedList, tableName); } private static void ParseData(IReadOnlyList parsedList, string tableName) { for (var i = 1; i < parsedList.Count; i++) { if (parsedList[i].Length < 2) continue; var translations = new List(); foreach (var languageItem in SettingsHelper.GetLanguageScheme()) { if (parsedList[i].Length <= languageItem.collumPos + 1) { continue; } var text = parsedList[i][languageItem.collumPos + 1]; translations.Add(new TranslationTextItem { language = languageItem.langType, text = text, }); } var textKey = parsedList[i][0]; var context = parsedList[i][1]; if (string.IsNullOrWhiteSpace(textKey)) { Debug.Log($"Key is empty in table {tableName}, record {i}. Skip entry"); continue; } var sourceText = parsedList[i][2]; var translation = ContextEntryIdHelper.TryReadEntryIdFromContextString(context, out long? entryId, out string strippedContext) ? new TranslationTextObj(entryId.GetValueOrDefault(), textKey, sourceText, translations, strippedContext, tableName) : new TranslationTextObj(null, textKey, sourceText, translations, context, tableName); TranslationsHelper.TryAddOrUpdateTranslation(translation); } } } }