Files
BusinessMgr-2019/SaBusMgrCoreWeb/Web/App/SbmLocaliser.cs
T
2021-07-08 20:50:46 +01:00

162 lines
5.3 KiB
C#

using Microsoft.AspNetCore.Html;
using Microsoft.Extensions.Localization;
using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Core;
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>();
//-------------------------------------------------------------------
// Generic phrase translation.
//-------------------------------------------------------------------
/// <summary>Retrieve localised text.</summary>
public LocalizedString GetText(string key)
{
return GetText(key, new object[] { });
}
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)
{
LocalizedString text = MainProperties.GetString(key, parameters);
if (text.ResourceNotFound)
{
SaLogger.Debug(GetType().Name + ".GetText Key not found: " + key);
// Look up key in alternative property files.
foreach (IStringLocalizer properties in AlternativeProperties ?? new List<IStringLocalizer>())
{
text = properties.GetString(key, parameters);
if (!text.ResourceNotFound)
break;
}
}
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>
/// 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);
}
//-------------------------------------------------------------------
// Specific phrase translation.
//-------------------------------------------------------------------
private class CodeCache
{
public List<TypeCodeDesc> affiliateTypeCodes;
// To be completed.
}
private CodeCache codes = new CodeCache();
// Status.
public String GetStatusCaption(SaStatus status)
{
// ToDo Status description localisation.
return SacsSystem.GetStatusDesc(status);
}
// Affiliate Type.
public String GetAffiliateTypeTypeCaption(Sbm.AffiliateType participation)
{
if ((int)participation < 1) return "";
if (codes.affiliateTypeCodes == null)
codes.affiliateTypeCodes = SbmSystem.GetCodesOfType(null, Sbm.SystemCode.AffiliateType);
// ToDo Affiliate Type localisation.
return codes.affiliateTypeCodes.GetDescForType(participation);
}
// To be completed.
//-------------------------------------------------------------------
}
//=======================================================================
}