using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using Newtonsoft.Json.Linq; using NLog; using Perfusion; namespace JetKarmaBot { public class Localization : IReadOnlyDictionary { private Dictionary locales = new Dictionary(); [Inject] public Localization(IContainer c) { c.ResolveObject(this); log.Info("Initializing..."); string langsFolder = "lang"; if (!Directory.Exists(langsFolder)) Directory.CreateDirectory(langsFolder); foreach (string langFilePath in Directory.EnumerateFiles(langsFolder, "*.json")) { try { string langName = Path.GetFileNameWithoutExtension(langFilePath); string langKey = langName.ToLowerInvariant(); locales[langKey] = new Locale(JObject.Parse(File.ReadAllText(langFilePath)), langKey); log.Debug("Found " + langName); } catch (Exception e) { log.Error($"Error while parsing {langFilePath}!"); log.Error(e); } } if (locales.Any()) log.Info("Initialized!"); else throw new FileNotFoundException($"No locales found in {langsFolder}!"); } public Locale this[string locale] { get { locale = locale.ToLowerInvariant(); return locales[locale]; } } [Inject] private Logger log; public Locale FindByCommonName(string name) { log.ConditionalTrace("Trying to find locale " + name); foreach (Locale l in locales.Values) { if (l.CommonNames.Contains(name)) { log.ConditionalTrace("Found locale " + l.Name); return l; } } log.Warn("Failed to find locale " + name); return null; } public bool ContainsLocale(string locale) { locale = locale.ToLowerInvariant(); return locales.ContainsKey(locale); } void Log(Exception e) => Console.WriteLine(e); public IEnumerable Keys => locales.Keys; public IEnumerable Values => locales.Values; public int Count => locales.Count; public bool ContainsKey(string key) => locales.ContainsKey(key.ToLower()); public bool TryGetValue(string key, out Locale value) { return locales.TryGetValue(key.ToLower(), out value); } public IEnumerator> GetEnumerator() { return locales.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class Locale : IReadOnlyDictionary { private Dictionary locale; private string localeName; private string[] commonNames; private string note; public Locale(JObject locale, string localeName) { this.locale = locale.Property("strings").Value.ToObject>(); this.localeName = localeName; this.commonNames = locale.Property("names").Value.ToObject(); if (locale.ContainsKey("note")) this.note = locale.Property("note").Value.ToObject(); } public string[] CommonNames => commonNames; public string Name => localeName; public bool HasNote => note != null; public string Note => note; public IEnumerable Keys => ((IReadOnlyDictionary)locale).Keys; public IEnumerable Values => ((IReadOnlyDictionary)locale).Values; public int Count => ((IReadOnlyDictionary)locale).Count; public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown"; public bool ContainsKey(string key) { return ((IReadOnlyDictionary)locale).ContainsKey(key); } public bool TryGetValue(string key, out string value) { return ((IReadOnlyDictionary)locale).TryGetValue(key, out value); } public IEnumerator> GetEnumerator() { return ((IReadOnlyDictionary)locale).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IReadOnlyDictionary)locale).GetEnumerator(); } } }