Skip to content

Commit

Permalink
Reading out and passing on selected language to pdf generator (#227)
Browse files Browse the repository at this point in the history
Co-authored-by: Ronny Birkeli <[email protected]>
  • Loading branch information
RonnyB71 and Ronny Birkeli authored Apr 18, 2023
1 parent 044df62 commit 6fd79fb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Altinn.App.Api/Controllers/ProcessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ private async Task<bool> CanTaskBeEnded(Instance instance, string currentElement
/// <param name="instanceGuid">unique id to identify the instance</param>
/// <param name="elementId">the id of the next element to move to. Query parameter is optional,
/// but must be specified if more than one element can be reached from the current process ellement.</param>
/// <param name="lang">Optional parameter to pass on the language used in the form if this differs from the profile language,
/// which otherwise is used automatically. The language is picked up when generating the PDF when leaving a step,
/// and is not used for anything else.
/// </param>
[HttpPut("next")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand All @@ -246,7 +250,8 @@ public async Task<ActionResult<ProcessState>> NextElement(
[FromRoute] string app,
[FromRoute] int instanceOwnerPartyId,
[FromRoute] Guid instanceGuid,
[FromQuery] string elementId = null)
[FromQuery] string elementId = null,
[FromQuery] string lang = null)
{
try
{
Expand Down
45 changes: 43 additions & 2 deletions src/Altinn.App.Core/Internal/Pdf/PdfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;

namespace Altinn.App.Core.Internal.Pdf;
Expand Down Expand Up @@ -85,11 +87,17 @@ public async Task GenerateAndStorePdf(Instance instance, CancellationToken ct)
{
var baseUrl = _generalSettings.FormattedExternalAppBaseUrl(new AppIdentifier(instance));
var pagePath = _pdfGeneratorSettings.AppPdfPagePathTemplate.ToLowerInvariant().Replace("{instanceid}", instance.Id);
string language = GetOverriddenLanguage();

Stream pdfContent = await _pdfGeneratorClient.GeneratePdf(new Uri(baseUrl + pagePath), ct);
// Avoid a costly call if the language is allready overriden by the user
language = language.IsNullOrEmpty() ? await GetLanguage() : language;

Uri uri = BuildUri(baseUrl, pagePath, language);

Stream pdfContent = await _pdfGeneratorClient.GeneratePdf(uri, ct);

var appIdentifier = new AppIdentifier(instance);
var language = await GetLanguage();

TextResource? textResource = await GetTextResource(appIdentifier.App, appIdentifier.Org, language);
string fileName = GetFileName(instance, textResource);

Expand All @@ -101,6 +109,23 @@ await _dataClient.InsertBinaryData(
pdfContent);
}

private static Uri BuildUri(string baseUrl, string pagePath, string language)
{
// Uses string manipulation instead of UriBuilder, since UriBuilder messes up
// query parameters in combination with hash fragments in the url.
string url = baseUrl + pagePath;
if (url.Contains('?'))
{
url += $"&lang={language}";
}
else
{
url += $"?lang={language}";
}

return new Uri(url);
}

/// <inheritdoc/>
public async Task GenerateAndStoreReceiptPDF(Instance instance, string taskId, DataElement dataElement, Type dataElementModelType)
{
Expand Down Expand Up @@ -243,6 +268,22 @@ private async Task<string> GetLanguage()
return language;
}

private string GetOverriddenLanguage()
{
// If the user has overridden the language from his profile by selecting a
// language directly in the form, then use the selected language.
if (_httpContextAccessor.HttpContext != null)
{
bool hasQueryLanguage = _httpContextAccessor.HttpContext.Request.Query.TryGetValue("lang", out StringValues queryLanguage);
if (hasQueryLanguage)
{
return queryLanguage.ToString();
}
}

return string.Empty;
}

private async Task<TextResource?> GetTextResource(string app, string org, string language)
{
TextResource? textResource = await _resourceService.GetTexts(org, app, language);
Expand Down

0 comments on commit 6fd79fb

Please sign in to comment.