refactor(HomeController): add SetLanguage and GetLanguages endpoints
This commit is contained in:
parent
845d06fc4c
commit
ada76d5030
@ -499,37 +499,6 @@ public class HomeController : ViewControllerBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize(Roles = ReceiverRole.FullyAuth)]
|
|
||||||
[HttpGet("/IsAuthenticated")]
|
|
||||||
public IActionResult IsAuthenticated()
|
|
||||||
{
|
|
||||||
var envelopeUuid = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
||||||
var receiverSignature = User.FindFirst(ClaimTypes.Hash)?.Value;
|
|
||||||
return Ok(new { EnvelopeUuid = envelopeUuid, ReceiverSignature = receiverSignature });
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost("/lang/{culture}")]
|
|
||||||
public IActionResult SetLanguage([FromRoute] string culture)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!_cultures.Languages.Contains(culture))
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
Response.Cookies.SetCulture(culture);
|
|
||||||
|
|
||||||
return Redirect(Request.Headers["Referer"].ToString());
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(statusCode: StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("/lang")]
|
|
||||||
public IActionResult GetLanguages() => Ok(_cultures.Languages);
|
|
||||||
|
|
||||||
[HttpGet("Error404")]
|
[HttpGet("Error404")]
|
||||||
public IActionResult Error404() => this.ViewError404();
|
public IActionResult Error404() => this.ViewError404();
|
||||||
}
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
using EnvelopeGenerator.Application.Resources;
|
using EnvelopeGenerator.Application.Resources;
|
||||||
|
using EnvelopeGenerator.Web.Extensions;
|
||||||
|
using EnvelopeGenerator.Web.Models;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
@ -8,11 +10,17 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class LocalizationController : ControllerBase
|
public class LocalizationController : ControllerBase
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<LocalizationController> _logger;
|
||||||
|
|
||||||
private readonly IStringLocalizer<Resource> _localizer;
|
private readonly IStringLocalizer<Resource> _localizer;
|
||||||
|
|
||||||
public LocalizationController(IStringLocalizer<Resource> localizer)
|
private readonly Cultures _cultures;
|
||||||
|
|
||||||
|
public LocalizationController(IStringLocalizer<Resource> localizer, Cultures cultures, ILogger<LocalizationController> logger)
|
||||||
{
|
{
|
||||||
_localizer = localizer;
|
_localizer = localizer;
|
||||||
|
_cultures = cultures;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@ -23,5 +31,19 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
?? _localizer.GetAllStrings().Where(ls => !ignore.Contains(ls.Name)).ToDictionary(ls => ls.Name, ls => ls.Value); ;
|
?? _localizer.GetAllStrings().Where(ls => !ignore.Contains(ls.Name)).ToDictionary(ls => ls.Name, ls => ls.Value); ;
|
||||||
return Ok(pairs);
|
return Ok(pairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("lang/{culture}")]
|
||||||
|
public IActionResult SetLanguage([FromRoute] string culture)
|
||||||
|
{
|
||||||
|
if (!_cultures.Languages.Contains(culture))
|
||||||
|
return BadRequest();
|
||||||
|
|
||||||
|
Response.Cookies.SetCulture(culture);
|
||||||
|
|
||||||
|
return Redirect(Request.Headers["Referer"].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("lang")]
|
||||||
|
public IActionResult GetLanguages() => Ok(_cultures.Languages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ function postEnvelope(annotations) {
|
|||||||
async function setLangAsync(language, flagCode) {
|
async function setLangAsync(language, flagCode) {
|
||||||
document.getElementById('selectedFlag').className = 'fi ' + flagCode + ' me-2';
|
document.getElementById('selectedFlag').className = 'fi ' + flagCode + ' me-2';
|
||||||
|
|
||||||
await fetch(`/lang/${language}`, {
|
await fetch(`/api/localization/lang/${language}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@ -36,7 +36,7 @@ async function setLangAsync(language, flagCode) {
|
|||||||
|
|
||||||
async function setLanguage(language) {
|
async function setLanguage(language) {
|
||||||
|
|
||||||
const hasLang = await fetch('/lang', {
|
const hasLang = await fetch('/api/localization/lang', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@ -47,7 +47,7 @@ async function setLanguage(language) {
|
|||||||
.catch(err => false);
|
.catch(err => false);
|
||||||
|
|
||||||
if (hasLang)
|
if (hasLang)
|
||||||
return await fetch(`/lang/${language}`, {
|
return await fetch(`/api/localization/lang/${language}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user