using System; using System.Linq; using System.Collections.Generic; using System.Text; namespace APT.Infrastructure.Core { public class LibMessageFormatManager { private static LibMessageFormatManager _entityConfigManager = null; private readonly List _formats = new List(); public static LibMessageFormatManager Instance { get { if (_entityConfigManager == null) _entityConfigManager = new LibMessageFormatManager(); return _entityConfigManager; } } public List Formats { get { return _formats; } } public LibMessageFormatManager() { } public string GetMessageFormat(string code, string lang) { var f = Formats.FirstOrDefault(t => string.Compare(t.Code, code, true) == 0); if (f == null) throw new Exception(" message code[" + code + "] is not exist"); var ret = f.GetFormat(lang); if (string.IsNullOrEmpty(ret)) throw new Exception("error message lang " + lang); return ret; } public void AddMessageFormat(LibMessageFormat format) { if (string.IsNullOrEmpty(format.Code)) return; var old = Formats.FirstOrDefault(t => string.Compare(t.Code, format.Code, true) == 0); if (old != null) Formats.Remove(old); Formats.Add(format); } } public class LibMessageFormat { private readonly List _formatInfos; public LibMessageFormat() { _formatInfos = new List(); } public string Code { get; set; } public void AddFormat(string lang,string format) { if (string.IsNullOrEmpty(format)) return; var f = _formatInfos.FirstOrDefault(t => string.Compare(t.Lang, lang, true) == 0); if (f != null) _formatInfos.Remove(f); var info = new LibMessageFormatInfo() { Lang = lang, Format = format, }; _formatInfos.Add(info); } public string GetFormat(string lang) { var f = _formatInfos.FirstOrDefault(t => string.Compare(t.Lang, lang, true) == 0); if (f != null) return f.Format; if (_formatInfos.Any()) { var r = _formatInfos.FirstOrDefault(t => string.IsNullOrEmpty(t.Lang)); if (r != null) return r.Format; } return null; } } public class LibMessageFormatInfo { public string Lang { get; set; } public string Format { get; set; } } }