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.
//-------------------------------------------------------------------
///
/// Localisers are supplied by derived object.
///
protected SaxLocaliser()
{
}
//-------------------------------------------------------------------
// Properties.
public abstract IStringLocalizer MainProperties { get; }
public virtual List AlternativeProperties { get; } = new List();
//-------------------------------------------------------------------
// Generic phrase translation.
//-------------------------------------------------------------------
/// Retrieve localised text.
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())
{
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); }
}
//-------------------------------------------------------------------
///
/// 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);
}
//-------------------------------------------------------------------
///
/// 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);
}
//-------------------------------------------------------------------
// Specific phrase translation.
//-------------------------------------------------------------------
private class CodeCache
{
public List genderCodes;
public List sportParticipationCodes;
public List sportOfficiatingCodes;
public List regionTypeCodes;
public List userRoleCodes;
public List organisationTypeCodes;
public List organisationCategoryCodes;
public List organisationRoleCodes;
public List opportunityTypeCodes;
}
private CodeCache codes = new CodeCache();
// Status.
public String GetStatusCaption(SaStatus status)
{
// ToDo Status description localisation.
return SacsSystem.GetStatusDesc(status);
}
// Gender.
public String GetGenderCaption(Sax.Gender gender)
{
if ((int)gender < 1) return "";
if (codes.genderCodes == null)
codes.genderCodes = SaxSystem.GetCodesOfType(Sax.CodeType.Gender);
// ToDo Opportunity Type localisation.
return codes.genderCodes.GetDescForType(gender);
}
// Sport Participation.
public String GetSportParticipationTypeCaption(Sax.SportParticipation participation)
{
if ((int)participation < 1) return "";
if (codes.sportParticipationCodes == null)
codes.sportParticipationCodes = SaxSystem.GetCodesOfType(Sax.CodeType.SportParticipation);
// ToDo Sport Participation localisation.
return codes.sportParticipationCodes.GetDescForType(participation);
}
// Sport Officiating.
public String GetSportOfficiatingTypeCaption(Sax.SportOfficiating participation)
{
if ((int)participation < 1) return "";
if (codes.sportOfficiatingCodes == null)
codes.sportOfficiatingCodes = SaxSystem.GetCodesOfType(Sax.CodeType.SportOfficiating);
// ToDo Sport Officiating localisation.
return codes.sportOfficiatingCodes.GetDescForType(participation);
}
// Region type.
public String GetRegionTypeCaption(Sax.RegionType regionType)
{
if ((int)regionType < 1) return "";
if (codes.regionTypeCodes == null)
codes.regionTypeCodes = SaxSystem.GetCodesOfType(Sax.CodeType.RegionType);
// ToDo Region type localisation.
return codes.regionTypeCodes.GetDescForType(regionType);
}
// User role.
public String GetUserRoleCaption(Sax.UserRole userRole)
{
if ((int)userRole < 1) return "";
if (codes.userRoleCodes == null)
codes.userRoleCodes = SaxSystem.GetCodesOfType(Sax.CodeType.UserRole);
// ToDo User Role localisation.
return codes.userRoleCodes.GetDescForType(userRole);
}
// Organisation type.
public String GetOrganisationTypeCaption(Sax.OrganisationType organisationType)
{
if ((int)organisationType < 1) return "";
if (codes.organisationTypeCodes == null)
codes.organisationTypeCodes = SaxSystem.GetCodesOfType(Sax.CodeType.OrganisationType);
// ToDo Organisation Type localisation.
return codes.organisationTypeCodes.GetDescForType(organisationType);
}
// Organisation category.
public String GetOrganisationCategoryCaption(Sax.OrganisationCategory organisationCategory)
{
if ((int)organisationCategory < 1) return "";
if (codes.organisationCategoryCodes == null)
codes.organisationCategoryCodes = SaxSystem.GetCodesOfType(Sax.CodeType.OrganisationCategory);
// ToDo Organisation Category localisation.
return codes.organisationCategoryCodes.GetDescForType(organisationCategory);
}
// Organisation role.
public String GetOrganisationRoleCaption(Sax.OrganisationRole organisationRole)
{
if ((int)organisationRole < 1) return "";
if (codes.organisationRoleCodes == null)
codes.organisationRoleCodes = SaxSystem.GetCodesOfType(Sax.CodeType.OrganisationRole);
// ToDo Organisation Role localisation.
return codes.organisationRoleCodes.GetDescForType(organisationRole);
}
// Opportunity type.
public String GetOpportunityTypeCaption(Sax.OpportunityType opportunityType)
{
if ((int)opportunityType < 1) return "";
if (codes.opportunityTypeCodes == null)
codes.opportunityTypeCodes = SaxSystem.GetCodesOfType(Sax.CodeType.OpportunityType);
// ToDo Opportunity Type localisation.
return codes.opportunityTypeCodes.GetDescForType(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;
}
//-------------------------------------------------------------------
}
//=======================================================================
}