Files
BusinessMgr-2019/SaBusMgrCoreWeb/Web/App/SbmLocaliser.cs
T

210 lines
7.5 KiB
C#

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.
//-------------------------------------------------------------------
/// <summary>
/// Localisers are supplied by derived object.
/// </summary>
protected SbmLocaliser()
{
}
//-------------------------------------------------------------------
// Properties.
public abstract IStringLocalizer MainProperties { get; }
public virtual List<IStringLocalizer> AlternativeProperties { get; } = new List<IStringLocalizer>();
private HashSet<String> MissingKeysLogged { get; } = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
//-------------------------------------------------------------------
// Generic phrase translation.
//-------------------------------------------------------------------
/// <summary>Retrieve localised text.</summary>
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<IStringLocalizer>())
{
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); }
}
//-------------------------------------------------------------------
/// <summary>
/// Localised HTML text.
/// <para>Line delimiters changed to '<br/>'.</para>
/// </summary>
public HtmlString GetHtmlText(string key, String param1 = "", String param2 = "")
{
return GetHtmlText(key, new Object[] { param1, param2 });
}
/// <summary>
/// Localised HTML text.
/// <para>Line delimiters changed to '<br/>'.</para>
/// </summary>
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", "<br/>").Replace("\r", "<br/>");
return new HtmlString(value);
}
//-------------------------------------------------------------------
/// <summary>
/// Encode the text to avoid conflict with HTML document structure.
/// </summary>
public String HtmlEncode(string text)
{
return WebUtil.HtmlEncode(text);
}
//-------------------------------------------------------------------
/// <summary>
/// Localised javascript string.
/// <para>All quotes are escaped.</para>
/// </summary>
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<Sbm.SystemCode, List<TypeCodeDesc>> codes { get; } = new Dictionary<Sbm.SystemCode, List<TypeCodeDesc>>();
public List<TypeCodeDesc> GetSystemCodes(Sbm.SystemCode codeType)
{
if (!codes.ContainsKey(codeType))
codes.Add(codeType, SbmSystem.GetSystemCodes(codeType));
return codes.GetValueOrDefault(codeType, new List<TypeCodeDesc>());
}
}
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<TypeCodeDesc> affiliateTypeCodes = systemCodes.GetSystemCodes(Sbm.SystemCode.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<TypeCodeDesc> associationTypeCodes = systemCodes.GetSystemCodes(Sbm.SystemCode.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<TypeCodeDesc> assetTypeCodes = systemCodes.GetSystemCodes(Sbm.SystemCode.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<TypeCodeDesc> transactionTypeCodes = systemCodes.GetSystemCodes(Sbm.SystemCode.TransactionType);
return transactionTypeCodes.GetDescForType(transactionType, transactionTypeCodes.GetCodeForType(transactionType));
}
// To be completed.
//-------------------------------------------------------------------
}
//=======================================================================
}