public void Write(LocalizationDictionary dictionary, Stream stream) { using (var w = new StreamWriter(stream, new UTF8Encoding(true))) { foreach (var p in dictionary) { if (LocalizationDictionary.IsComment(p.Key)) { if (!string.IsNullOrEmpty(p.Value.Context)) { foreach (var i in p.Value.Context.Split('\n')) { w.WriteLine("# " + i); } } else { w.WriteLine(); } } else { w.WriteLine('[' + Escape(p.Key) + ']'); if (!string.IsNullOrEmpty(p.Value.Context)) { foreach (var i in p.Value.Context.Split('\n')) { w.WriteLine("# " + i); } } w.WriteLine(p.Value.Text); w.WriteLine(); } } } }
public static void UpdateCharset(TftConfig.CharSet charSet, string assetDirectory, bool sortByFrequency = false) { if (string.IsNullOrEmpty(charSet.ExtractFromDictionaries)) { return; } var characters = new HashSet <char>(); var frequency = new Dictionary <char, int>(); var dict = new LocalizationDictionary(); foreach (var localization in charSet.ExtractFromDictionaries.Split(',')) { // cause EN is default dictionary var loc = localization == "EN" ? string.Empty : localization; var dictPath = AssetPath.Combine(assetDirectory, Localization.DictionariesPath, $"Dictionary.{loc}.txt".Replace("..", ".")); if (!File.Exists(dictPath)) { Console.WriteLine($"Dictionary of {localization} localization is missing!: {dictPath}"); continue; } using (var stream = File.Open(dictPath, FileMode.Open)) { dict.ReadFromStream(stream); } ExtractCharacters(dict, characters, frequency); } charSet.Chars = string.Join("", sortByFrequency ? characters.OrderByDescending(c => frequency[c]) : characters.OrderBy(c => c)); }
public void Read(LocalizationDictionary dictionary, Stream stream) { using (var r = new StreamReader(stream)) { string line = r.ReadLine(); while (line != null) { line = line.Trim(); if (ValidateComment(line) || ValidateSpace(line)) { dictionary.AddComment(ValidateSpace(line) ? null : line.Substring(1).Trim()); line = r.ReadLine(); continue; } if (!ValidateKey(line)) { throw new Lime.Exception("Invalid key format: {0}", line); } var key = Unescape(line.Substring(1, line.Length - 2)); string context = null; string text = ""; while (true) { line = r.ReadLine(); var isCommentAfterValue = line != null && !string.IsNullOrEmpty(text) && ValidateComment(line); if (line == null || ValidateKey(line) || isCommentAfterValue) { break; } if (ValidateComment(line)) { context = (context ?? "") + line.Substring(1).Trim() + '\n'; } else { text += line + '\n'; } } text = text.TrimEnd('\n'); if (context != null) { context = context.TrimEnd('\n'); } dictionary.Add(key, text, context); } } }
private static void ExtractCharacters(LocalizationDictionary dictionary, HashSet <char> chars, Dictionary <char, int> frequency) { foreach (var(_, value) in dictionary) { if (value.Text == null) { continue; } foreach (var c in value.Text) { if (c != '\n' && !char.IsSurrogate(c)) { chars.Add(c); frequency[c] = frequency.TryGetValue(c, out var v) ? v + 1 : 1; } } } }