using Microsoft.AspNetCore.Mvc.Localization; using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Sarand.OperationsMgr { // /// Dummy class to group shared resources - named as per '.resx' file in root namespace. /// Required to create the IStringLocalizer instance using the type from the class. /// Name of the class is used as the prefix for localisation filenames. /// Resource files are in the Localization ResourcesPath in Startup.cs. /// public class OpnProperties { } // Class must be in Root/Default Namespace to reference properties with same name. // NB 'Assembly name' must be the same as 'Default Namespace' until 'Root Namespace' can be set (.Net Core 2.1?). // Ref. Project Properties - Application. } namespace Sarand.OperationsMgr.Web.App { //======================================================================= public class OpnLocaliser { //------------------------------------------------------------------- // Data members. private readonly IStringLocalizer opnLocalizer; private readonly IHtmlLocalizer opnHtmlLocalizer; //------------------------------------------------------------------- /// /// Localisers are supplied using construction injection. /// public OpnLocaliser (IStringLocalizer opnLocalizer, IHtmlLocalizer opnHtmlLocalizer) { this.opnLocalizer = opnLocalizer; this.opnHtmlLocalizer = opnHtmlLocalizer; } //------------------------------------------------------------------- /// Retrieve localised text. public LocalizedString GetText (string key) { return GetText(key, new object[] { }); } public LocalizedString GetText (string key, string param) { return GetText(key, new object[] { param }); } public LocalizedString GetText (string key, string param1, string param2) { return GetText(key, new object[] { param1, param2 }); } public LocalizedString GetText (string key, object[] parameters) { return opnLocalizer.GetString(key, parameters); } public LocalizedString this[String key] { get { return GetText(key); } } public LocalizedString this[String key, String param] { get { return GetText(key, param); } } public LocalizedString this[String key, String param1, String param2] { get { return GetText(key, param1, param2); } } //------------------------------------------------------------------- /// /// Localised HTML text. /// Line delimiters changed to '
'.
///
public LocalizedString GetHtmlText (string key) { LocalizedString localisedText = GetText(key); String value = localisedText.Value; // Replace all line delimiters. value = value.Replace("\r\n", "\n").Replace("\n", "
").Replace("\r", "
"); return new LocalizedString(localisedText, value, localisedText.ResourceNotFound); } //------------------------------------------------------------------- /// /// Localised javascript string. /// All quotes are escaped. /// public LocalizedString GetJavascriptText (string key) { LocalizedString localisedText = GetText(key); return new LocalizedString(localisedText, EscapeJavascriptText(localisedText.Value), localisedText.ResourceNotFound); } public String EscapeJavascriptText (string value) { // Escape all quotes. value = value.Replace("'", "\\'").Replace("\"", "\\\""); return value; } } //======================================================================= }