using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; using Sarand.BusinessMgr.Net.Standard.Base.App; using Sarand.BusinessMgr.Net.Standard.Base.Consoles; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Net; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Text; namespace Sarand.BusinessMgr.Net.Core.Web.App { //======================================================================= public abstract class SbmLocaliser { //------------------------------------------------------------------- // Data members. //------------------------------------------------------------------- /// /// Localisers are supplied by derived object. /// protected SbmLocaliser() { } //------------------------------------------------------------------- // Properties. public abstract IStringLocalizer MainProperties { get; } public virtual List AlternativeProperties { get; } = new List(); private HashSet MissingKeysLogged { get; } = new HashSet(StringComparer.OrdinalIgnoreCase); //------------------------------------------------------------------- // Generic phrase translation. //------------------------------------------------------------------- /// Retrieve localised text. public LocalizedString GetText(string key, bool logMissingKey = true) { return GetText(key, new object[] { }, logMissingKey); } public LocalizedString GetText(string key, object param) { return GetText(key, new object[] { param }); } public LocalizedString GetText(string key, object param1, object param2) { return GetText(key, new object[] { param1, param2 }); } public LocalizedString GetText(string key, object[] parameters, bool logMissingKey = true) { LocalizedString text = MainProperties.GetString(key, parameters); if (text.ResourceNotFound) { // Look up key in alternative property files. foreach (IStringLocalizer properties in AlternativeProperties ?? new List()) { text = parameters == null || parameters.IsEmpty() ? properties.GetString(key) : properties.GetString(key, parameters); if (!text.ResourceNotFound) break; } } if (text.ResourceNotFound && logMissingKey && !MissingKeysLogged.Contains(key)) { MissingKeysLogged.Add(key); SaLogger.Debug($"{SysUtil.Fn} Key not found: {key}"); } return text; } // Square bracket indexing. public LocalizedString this[String key] { get { return GetText(key); } } public LocalizedString this[String key, object param] { get { return GetText(key, param); } } public LocalizedString this[String key, object param1, object param2] { get { return GetText(key, param1, param2); } } //------------------------------------------------------------------- /// /// Localised HTML text. /// Line delimiters changed to '
'.
///
public HtmlString GetHtmlText(string key, String param1 = "", String param2 = "") { return GetHtmlText(key, new Object[] { param1, param2 }); } /// /// Localised HTML text. /// Line delimiters changed to '
'.
///
public HtmlString GetHtmlText(string key, object[] parameters) { LocalizedString localisedText = GetText(key, parameters); String value = localisedText.Value; // Replace all line delimiters. value = value.Replace("\r\n", "\n").Replace("\n", "
").Replace("\r", "
"); return new HtmlString(value); } //------------------------------------------------------------------- /// /// Encode the text to avoid conflict with HTML document structure. /// public String HtmlEncode(string text) { return WebUtil.HtmlEncode(text); } //------------------------------------------------------------------- /// /// Localised javascript string. /// All quotes are escaped. /// public LocalizedString GetJavascriptText(string key, String param1 = "", String param2 = "") { LocalizedString localisedText = GetText(key, param1, param2); return new LocalizedString(localisedText, EscapeJavascriptText(localisedText.Value), localisedText.ResourceNotFound); } public String EscapeJavascriptText(string value) { // Escape all quotes. value = value.Replace("'", "\\'").Replace("\"", "\\\""); return value; } //------------------------------------------------------------------- // Numeric and datetime formatting //------------------------------------------------------------------- public static String DateFormat_Default { get; } = "d-MMM-yyyy"; public static String DateWithWeekdayFormat_Default { get; } = "ddd d-MMM-yyyy"; public String FormatDate(DateTime value, Boolean includeWeekday = true) { String dateFormat = includeWeekday ? DateWithWeekdayFormat_Default : DateFormat_Default; return DateUtil.Format(value, dateFormat); } //------------------------------------------------------------------- // System Codes. //------------------------------------------------------------------- private class CodeCache { public Dictionary> codes { get; } = new Dictionary>(); public List GetSystemCodes(Sbm.ApplicationCode codeType) { if (!codes.ContainsKey(codeType)) codes.Add(codeType, SbmSystem.GetSystemCodes(codeType)); return codes.GetValueOrDefault(codeType, new List()); } } private readonly CodeCache systemCodes = new CodeCache(); // Status. public String GetStatusCaption(SaStatus status) { // ToDo Status description localisation. return SacsSystem.GetStatusDesc(status); } // Affiliate Type. public String GetAffiliateTypeCaption(Sbm.AffiliateType affiliateType) { if ((int)affiliateType < 1) return ""; // ToDo Affiliate Type localisation. List affiliateTypeCodes = systemCodes.GetSystemCodes(Sbm.ApplicationCode.AffiliateType); return affiliateTypeCodes.GetDescForType(affiliateType, affiliateTypeCodes.GetCodeForType(affiliateType)); } // Association Type. public String GetAssociationTypeCaption(Sbm.AssociationType associationType) { if ((int)associationType < 1) return ""; // ToDo Association Type localisation. List associationTypeCodes = systemCodes.GetSystemCodes(Sbm.ApplicationCode.AssociationType); return associationTypeCodes.GetDescForType(associationType, associationTypeCodes.GetCodeForType(associationType)); } // Asset Type. public String GetAssetTypeCaption(Sbm.AssetType assetType) { if ((int)assetType < 1) return ""; // ToDo Asset Type localisation. List assetTypeCodes = systemCodes.GetSystemCodes(Sbm.ApplicationCode.AssetType); return assetTypeCodes.GetDescForType(assetType, assetTypeCodes.GetCodeForType(assetType)); } // Transaction Type. public String GetTransactionTypeCaption(Sbm.TransactionType transactionType) { if ((int)transactionType < 1) return ""; // ToDo Transaction Type localisation. List transactionTypeCodes = systemCodes.GetSystemCodes(Sbm.ApplicationCode.TransactionType); return transactionTypeCodes.GetDescForType(transactionType, transactionTypeCodes.GetCodeForType(transactionType)); } // To be completed. //------------------------------------------------------------------- } //======================================================================= }