public void Generate() { IEnumerable <Translation> translations = _source.GetTranslations(); Dictionary <string, object> cfg = _config.Plugins["generatorJson"]; ResultEnding endsIn = Enum.Parse <ResultEnding>(cfg.GetString("ends", "default"), true); string outputFile = cfg.GetString("output"); Dictionary <string, object> rootFormat = cfg.Get <Dictionary <object, object> >("format").DeepCast <object>(); Dictionary <string, object> result = rootFormat.FormatTranslations(translations, endsIn); string resultText = JsonSerializer.Serialize(result, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }); System.IO.File.WriteAllText(outputFile, resultText); }
internal static Dictionary <string, object> FormatTranslations( this Dictionary <string, object> cfgFormat, IEnumerable <Translation> translations, ResultEnding endsIn) { (IEnumerable <IEnumerable <TranslationValueType> > groupings, IEnumerable <TranslationValueType> wantedValues) = cfgFormat.Parse(); List <IEnumerable <TranslationValueType> > groupingsList = groupings.ToList(); List <TranslationValueType> wantedValuesList = wantedValues.ToList(); Dictionary <string, object> result = new(); foreach (Translation translation in translations) { result.InsertTranslation(translation, groupingsList, wantedValuesList, endsIn); } return(result); }
internal static void InsertTranslation( this Dictionary <string, object> dictionary, Translation translation, IEnumerable <IEnumerable <TranslationValueType> > groupings, IEnumerable <TranslationValueType> wantedValues, ResultEnding endsIn) { List <string> groupKeys = groupings.Select(translation.GetGroupKey).ToList(); dynamic resultTranslation = translation.GetResult(wantedValues); Dictionary <string, object> deepDictionary = dictionary; for (int i = 0; i < groupKeys.Count - 1; i++) { string groupKey = groupKeys[i]; if (!deepDictionary.ContainsKey(groupKey)) { deepDictionary[groupKey] = new Dictionary <string, object>(); } deepDictionary = (Dictionary <string, object>)deepDictionary[groupKey]; } string lastGroupKey = groupKeys.Last(); if (deepDictionary.ContainsKey(lastGroupKey)) { if (endsIn != ResultEnding.Multiple) { throw new InvalidDataException("Format configuration does not return single results, " + "fix the format or change ending to 'multiple'."); } ((List <object>)deepDictionary[lastGroupKey]).Add(resultTranslation); } else { deepDictionary[lastGroupKey] = endsIn == ResultEnding.Multiple ? new List <object> { resultTranslation } : resultTranslation; } }