No description
Find a file
Ferdinand Kuhl ad9c9fd678
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests Pipeline was successful
Adding error handling to code example
2023-08-08 10:56:04 +02:00
.woodpecker First version, extracted and tested 2023-08-05 15:58:50 +02:00
Classes First version, extracted and tested 2023-08-05 15:58:50 +02:00
Configuration First version, extracted and tested 2023-08-05 15:58:50 +02:00
Resources/Private/Translations First version, extracted and tested 2023-08-05 15:58:50 +02:00
Tests/Functional First version, extracted and tested 2023-08-05 15:58:50 +02:00
composer.json First version, extracted and tested 2023-08-05 15:58:50 +02:00
README.md Adding error handling to code example 2023-08-08 10:56:04 +02:00

DigiComp.FlowTranslationEndpoint

Build status

This package is designed to help bringing needed translations to javascript components, without pushing them to the DOM in your views.

Other solutions, which would generate files available for usage in client scope, have the disadvantage that one would have to repeat the relativ complex overriding and merging logic of Flow. With this endpoint you can get the same content, as you would get, if you call the translation service with your translation id.

The main components are a CurrentHtmlLangViewHelper, which is intended to be used to fill the lang attribute of the html tag, so the frontend knows, which language is currently active (and is good practice anyway) and a TranslationMiddleware, which will respond to any request, which have a X-Translation-Request header, carrying a pattern of translation ids which should be returned.

For example:

X-Translation-Request: Neos.Flow:Main|authentication.*

would return all translation keys from the main unit of Neos.Flow starting with "authentication" and would look like that:

{
    "Neos.Flow:Main": {
        "authentication.required": "Authentication required", 
        "authentication.username": "Username",
        "authentication.password": "Password", 
        "authentication.new-password": "New password",
        "authentication.login": "Login", 
        "authentication.logout": "Logout"
    }
}

To let the middleware know, in which langauge the translated units should be, you should set the correct Accept-Language-Header with your request, which you obtained from the lang attribute of the html element.

Your JavaScript could look like that:

async function translate(idPatterns) {
    const response = await fetch(document.location, {headers: {
        'X-Translation-Request': idPatterns,
        'Accept-Language': document.documentElement.lang
    }});
    if (! response.ok) {
        return Promise.reject('Unexpected server response');
    }
    return await response.json();
}

If, for whatever reason, you prefer to have a "traditional" single endpoint, which works without a custom header, you can set DigiComp.FlowTranslationEndpoint.replaceRoutedEndpoint.reactOnPath. At this point a middleware translates all incoming requests and the GET parameter idPatterns (you can change the name in Settings.yaml).