337 lines
11 KiB
C#
337 lines
11 KiB
C#
using Microsoft.AspNetCore.Html;
|
|
using Microsoft.AspNetCore.Mvc.Localization;
|
|
using Microsoft.Extensions.Localization;
|
|
using Microsoft.Extensions.Logging;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Resources;
|
|
using System.Web;
|
|
|
|
namespace Saxonite.Net.Core.Web.Base.App
|
|
{
|
|
//=======================================================================
|
|
public abstract class SaxLocaliser
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Localisers are supplied by derived object.
|
|
/// </summary>
|
|
protected SaxLocaliser()
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// 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, null);
|
|
}
|
|
/// <summary>Retrieve localised text.</summary>
|
|
public LocalizedString GetText(string key, bool logMissingKey = true)
|
|
{
|
|
return GetText(key, null, logMissingKey);
|
|
}
|
|
/// <summary>Retrieve localised text.</summary>
|
|
public LocalizedString GetText(string key, object param)
|
|
{
|
|
return GetText(key, new object[] { param });
|
|
}
|
|
/// <summary>Retrieve localised text.</summary>
|
|
public LocalizedString GetText(string key, object param1, object param2)
|
|
{
|
|
return GetText(key, new object[] { param1, param2 });
|
|
}
|
|
/// <summary>Retrieve localised text.</summary>
|
|
public LocalizedString GetText(string key, object[] parameters, bool logMissingKey = true)
|
|
{
|
|
LocalizedString text = parameters == null || parameters.IsEmpty() ? MainProperties.GetString(key) : 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)
|
|
SaLogger.Debug(GetType().Name + ".GetText 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>
|
|
/// 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 CodesCache
|
|
{
|
|
private Dictionary<Sax.CodeType, List<TypeCodeDesc>> codeTypeCache { get; } = new Dictionary<Sax.CodeType, List<TypeCodeDesc>>();
|
|
public List<TypeCodeDesc> GetCodesOfType(Sax.CodeType codeType)
|
|
{
|
|
List<TypeCodeDesc> codes = null;
|
|
lock (codeTypeCache)
|
|
{
|
|
codes = codeTypeCache.GetValue(codeType, null);
|
|
if (codes == null)
|
|
{
|
|
codes = SaxSystem.GetCodesOfType(codeType);
|
|
codeTypeCache.Add(codeType, codes);
|
|
}
|
|
}
|
|
return codes;
|
|
}
|
|
}
|
|
private CodesCache codesCache = new CodesCache();
|
|
|
|
// Status.
|
|
public String GetStatusCaption(SaStatus status)
|
|
{
|
|
// ToDo Status description localisation.
|
|
return SacsSystem.GetStatusDesc(status);
|
|
}
|
|
|
|
// Generic code type.
|
|
private String GetCodeTypeCaption<T>(Sax.CodeType codeType, T type) where T : struct, IConvertible
|
|
{
|
|
if (!(type is Enum))
|
|
throw new ArgumentException(GetType().Name + ".GetCodeTypeCaption Type parameter must be an enumerated type. Type supplied: " + type.GetType().Name);
|
|
return GetCodeTypeCaption(codeType, (int)(type as IConvertible), type.ToString());
|
|
}
|
|
private String GetCodeTypeCaption(Sax.CodeType codeType, int type, String typeName)
|
|
{
|
|
if (type < 1) return "";
|
|
List<TypeCodeDesc> codes = this.codesCache.GetCodesOfType(codeType);
|
|
TypeCodeDesc code = codes.GetEntryForType(type);
|
|
if (code != null)
|
|
{
|
|
// Code Type localisation.
|
|
String codeTypeKey = "codeType." + codeType.ToString() + "." + typeName;
|
|
String typeDesc = GetText(codeTypeKey, false);
|
|
if (typeDesc.EqualsCI(codeTypeKey))
|
|
typeDesc = "";
|
|
return StringUtil.OnEmpty(typeDesc, code.Desc);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
// Yes/No.
|
|
public String GetYesNoCaption(Sax.YesNo yesNo)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.YesNo, yesNo);
|
|
}
|
|
|
|
// Data item type.
|
|
public String GetItemDataTypeCaption(Sax.DataItemType itemType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.DataItemType, itemType);
|
|
}
|
|
|
|
// Gender.
|
|
public String GetGenderCaption(Sax.Gender gender)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.Gender, gender);
|
|
}
|
|
|
|
// Sport Speciality label.
|
|
public String GetSportSpecialityLabelCaption(Sax.SportSpecialityLabel sportSpecialityLabel)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.SportSpecialityLabel, sportSpecialityLabel);
|
|
}
|
|
|
|
// Sport Contact Type.
|
|
public String GetSportContactTypeCaption(Sax.SportContactType sportContactType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.SportContactType, sportContactType);
|
|
}
|
|
|
|
// Sport Participation.
|
|
public String GetSportParticipationCaption(Sax.SportParticipation participation)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.SportParticipation, participation);
|
|
}
|
|
|
|
// Sport Officiating.
|
|
public String GetSportOfficiatingCaption(Sax.SportOfficiating participation)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.SportOfficiating, participation);
|
|
}
|
|
|
|
// Region type.
|
|
public String GetRegionTypeCaption(Sax.RegionType regionType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.RegionType, regionType);
|
|
}
|
|
|
|
// Eligibility type.
|
|
public String GetEligibilityTypeCaption(Sax.EligibilityType regionType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.EligibilityType, regionType);
|
|
}
|
|
|
|
// User role.
|
|
public String GetUserRoleCaption(Sax.UserRole userRole)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.UserRole, userRole);
|
|
}
|
|
|
|
// Organisation type.
|
|
public String GetOrganisationTypeCaption(Sax.OrganisationType organisationType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.OrganisationType, organisationType);
|
|
}
|
|
|
|
// Organisation category.
|
|
public String GetOrganisationCategoryCaption(Sax.OrganisationCategory organisationCategory)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.OrganisationCategory, organisationCategory);
|
|
}
|
|
|
|
// Organisation role.
|
|
public String GetOrganisationRoleCaption(Sax.OrganisationRole organisationRole)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.OrganisationRole, organisationRole);
|
|
}
|
|
|
|
// Association types.
|
|
public String GetAssociationTypeCaption(Sax.AssociationType addressType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.AssociationType, addressType);
|
|
}
|
|
|
|
// Address types.
|
|
public String GetAddressTypeCaption(Sax.AddressType addressType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.AddressType, addressType);
|
|
}
|
|
|
|
// Contact Method.
|
|
public String GetContactMethodCaption(Sax.ContactMethod contactMethod)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.ContactMethod, contactMethod);
|
|
}
|
|
|
|
// Event type.
|
|
public String GetEventTypeCaption(Sax.EventType eventType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.EventType, eventType);
|
|
}
|
|
|
|
// Opportunity type.
|
|
public String GetOpportunityTypeCaption(Sax.OpportunityType opportunityType)
|
|
{
|
|
return GetCodeTypeCaption(Sax.CodeType.OpportunityType, opportunityType);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Region.
|
|
public String GetRegionName(SaxRegion.Data region)
|
|
{
|
|
if (region == null) return "";
|
|
// ToDo Region name localisation.
|
|
return region.Name;
|
|
}
|
|
public String GetRegionName(SysId regionId, String regionName)
|
|
{
|
|
// ToDo Region name localisation.
|
|
return regionName;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Sport.
|
|
public String GetSportName(SaxSport.Data sport)
|
|
{
|
|
if (sport == null) return "";
|
|
// ToDo Sport name localisation.
|
|
return sport.Name;
|
|
}
|
|
public String GetSportName(SysId sportId, String sportName)
|
|
{
|
|
// ToDo Sport name localisation.
|
|
return sportName;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
} |