using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; using Serilog; using YamlDotNet.Serialization; namespace NadekoBot.Core.Services { /// /// Loads strings from the local default filepath /// public class LocalFileStringsSource : IStringsSource { private readonly string _responsesPath = "data/strings/responses"; private readonly string _commandsPath = "data/strings/commands"; public LocalFileStringsSource(string responsesPath = "data/strings/responses", string commandsPath = "data/strings/commands") { _responsesPath = responsesPath; _commandsPath = commandsPath; } public Dictionary> GetResponseStrings() { var outputDict = new Dictionary>(); foreach (var file in Directory.GetFiles(_responsesPath)) { try { var langDict = JsonConvert.DeserializeObject>(File.ReadAllText(file)); var localeName = GetLocaleName(file); outputDict[localeName] = langDict; } catch (Exception ex) { Log.Error(ex, "Error loading {FileName} response strings: {ErrorMessage}", file, ex.Message); } } return outputDict; } public Dictionary> GetCommandStrings() { var deserializer = new DeserializerBuilder() .Build(); var outputDict = new Dictionary>(); foreach (var file in Directory.GetFiles(_commandsPath)) { try { var text = File.ReadAllText(file); var langDict = deserializer.Deserialize>(text); var localeName = GetLocaleName(file); outputDict[localeName] = langDict; } catch (Exception ex) { Log.Error(ex, "Error loading {FileName} command strings: {ErrorMessage}", file, ex.Message); } } return outputDict; } private static string GetLocaleName(string fileName) { fileName = Path.GetFileName(fileName); var dotIndex = fileName.IndexOf('.') + 1; var secondDotIndex = fileName.LastIndexOf('.'); return fileName.Substring(dotIndex, secondDotIndex - dotIndex); } } }