106 lines
3.7 KiB
C#
106 lines
3.7 KiB
C#
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
|
|
{
|
|
// <summary>
|
|
/// Dummy class to group shared resources - named as per '.resx' file in root namespace.
|
|
/// <para>Required to create the IStringLocalizer instance using the type from the class.</para>
|
|
/// <para>Name of the class is used as the prefix for localisation filenames.</para>
|
|
/// <para>Resource files are in the Localization ResourcesPath in Startup.cs.</para>
|
|
/// </summary>
|
|
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<OpnProperties> opnLocalizer;
|
|
private readonly IHtmlLocalizer<OpnProperties> opnHtmlLocalizer;
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Localisers are supplied using construction injection.
|
|
/// </summary>
|
|
public OpnLocaliser (IStringLocalizer<OpnProperties> opnLocalizer, IHtmlLocalizer<OpnProperties> opnHtmlLocalizer)
|
|
{
|
|
this.opnLocalizer = opnLocalizer;
|
|
this.opnHtmlLocalizer = opnHtmlLocalizer;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Retrieve localised text.</summary>
|
|
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); }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Localised HTML text.
|
|
/// <para>Line delimiters changed to '<br/>'.</para>
|
|
/// </summary>
|
|
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", "<br/>").Replace("\r", "<br/>");
|
|
return new LocalizedString(localisedText, value, localisedText.ResourceNotFound);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Localised javascript string.
|
|
/// <para>All quotes are escaped.</para>
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
//=======================================================================
|
|
}
|