Do Management APIs return localized Problem Detail messages?

Hello :waving_hand:
So with the C# API Management API Controllers, do they support localising the error messages in the correct culture of the user viewing the backoffice?

Currently with Bellissima some of the localisation is done in client side which is fine and is done like so:

However when I am making requests to a custom API Controller, I am passing the response from the Problem Detail straight onto the Notification Context to display the error/warning to the user.

It would make sense if there was a way, pattern or approach to translate the messages coming back from a C# API response somehow.

Open to any ideas or suggestions on how to resolve this.

Goal

Allow this to be translated and use the XML lang files.

return BadRequest(new ProblemDetailsBuilder()
    .WithTitle("Unauthorized")
    .WithDetail("Someone else already has the piece of content locked and only the original user who locked this content can unlock it or a super user with the unlocking permission")
    .Build());

Detective at work…

OK after 45mins or so detective work I have something working.

giphy

Resources

Solution

This is the solution I came to in the end

Create lang.xml files

These files for my package need to be in App_Plugins/ContentLock/lang/en.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<language alias="en" intName="English (UK)" localName="English (UK)" lcid="" culture="en-GB">
    <creator>
        <name>The Umbraco community</name>
        <link>https://docs.umbraco.com/umbraco-cms/extending/language-files</link>
    </creator>
    <area alias="contentLockResponseTitles">
        <key alias="unauthorized">Unauthorized</key>
        <key alias="invalidPermission">Invalid Permission</key>
    </area>
    <area alias="contentLockResponseDetails">
        <key alias="contentAlreadyLocked">Someone else already has the piece of content locked and only the original user who locked this content can unlock it or a user with with the Content Lock 'Unlocker' permission.</key>
        <key alias="deniedUnlock">Only the original user who locked this content can unlock it or a user with with the Content Lock 'Unlocker' permission.</key>
        <key alias="deniedBulkUnlock">Only users with the Content Lock 'Unlocker' permission is allowed to perform a bulk unlock.</key>
    </area>
</language>

Use ILocalizedTextService

public ContentLockApiController(IBackOfficeSecurityAccessor backOfficeSecurityAccessor, 
    IContentLockService contentLockService,
    ILocalizedTextService localizedTextService)
{
    _backOfficeSecurityAccessor = backOfficeSecurityAccessor;
    _contentLockService = contentLockService;
    _localizedTextService = localizedTextService; // Needed for the XML lang files
}

Call Localize() with Backoffice User Language

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[HttpGet("Lock/{key:guid}")]
public async Task<IActionResult> LockContentAsync(Guid key)
{

    var userKey = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Key;
    var userLang = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Language ?? "en";
    var cultureInfo = new CultureInfo(userLang);
    
    // ... more code ...

    // Use _localizedTextService.Localize()
    // Along with getting the current user to backoffice API current set language

    return BadRequest(new ProblemDetailsBuilder()
        .WithTitle(_localizedTextService.Localize("contentLockResponseTitles", "unauthorized", cultureInfo))
        .WithDetail(_localizedTextService.Localize("contentLockResponseDetails", "contentAlreadyLocked", cultureInfo))
        .Build());

Result

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.