gehgassi_backend/gehGassi.Web/Controllers/LanguageController.cs

45 lines
1.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
namespace gehGassi.Web.Controllers
{
/// <summary>
/// Controller für die Sprachverwaltung
/// </summary>
public class LanguageController : Controller
{
private readonly IStringLocalizerFactory _localizer;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="localizer"></param>
public LanguageController(IStringLocalizerFactory localizer)
{
_localizer = localizer;
}
/// <summary>
/// Gibt die Ressource "Client" als Json-Dictionary zurück
/// </summary>
/// <param name="resource">Gewünschte Ressource</param>
/// <param name="lang">Gewünchte Sprache ISO2 code</param>
/// <param name="version">Versionsnummer wegen Caching</param>
/// <returns>Json-Dictionary</returns>
#if DEBUG
[ResponseCache(Duration = 1, VaryByQueryKeys = new[] { "resource", "lang", "version" })]
#else
[ResponseCache(CacheProfileName = "ResourcesCache")]
#endif
public ActionResult GetResource(string resource, string lang, string version)
{
var localizer = _localizer.Create(resource, "Localization");
var items = localizer.GetAllStrings();
Dictionary<string, string> dictionary = items.ToDictionary(k => k.Name, v => v.Value);
return Json(dictionary);
}
}
}