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, null); } /// Retrieve localised text. public LocalizedString GetText(string key, bool logMissingKey = true) { return GetText(key, null, logMissingKey); } /// Retrieve localised text. public LocalizedString GetText(string key, object param) { return GetText(key, new object[] { param }); } /// Retrieve localised text. public LocalizedString GetText(string key, object param1, object param2) { return GetText(key, new object[] { param1, param2 }); } /// Retrieve localised text. 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()) { 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); } } //------------------------------------------------------------------- /// /// 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 CodesCache { private Dictionary> codeTypeCache { get; } = new Dictionary>(); public List GetCodesOfType(Sax.CodeType codeType) { List 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(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 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 specialityLabel) { return GetCodeTypeCaption(Sax.CodeType.SportSpecialityLabel, specialityLabel); } // 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); } // 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); } // 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; } //------------------------------------------------------------------- } //======================================================================= }