Merge branch 'release/1.0.0'
All checks were successful
ci/woodpecker/manual/code-style Pipeline was successful
ci/woodpecker/manual/functional-tests Pipeline was successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests Pipeline was successful

This commit is contained in:
Ferdinand Kuhl 2023-08-10 10:38:15 +02:00
commit 6e0180e9d5
16 changed files with 546 additions and 0 deletions

View file

@ -0,0 +1,8 @@
pipeline:
code-style:
image: composer
commands:
- composer global config repositories.repo-name vcs https://git.digital-competence.de/Packages/php-codesniffer
- composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
- composer global require digicomp/php-codesniffer:@dev
- composer global exec -- phpcs --runtime-set ignore_warnings_on_exit 1 --standard=DigiComp Classes/ Tests/

View file

@ -0,0 +1,32 @@
workspace:
base: /woodpecker
path: package
matrix:
include:
- FLOW_VERSION: 6.3
PHP_VERSION: 7.4
- FLOW_VERSION: 7.3
PHP_VERSION: 7.4
- FLOW_VERSION: 7.3
PHP_VERSION: 8.1
- FLOW_VERSION: 8.3
PHP_VERSION: 8.1
pipeline:
functional-tests:
image: "thecodingmachine/php:${PHP_VERSION}-v4-cli"
environment:
# Enable the PDO_SQLITE extension
- "PHP_EXTENSION_PDO_SQLITE=1"
- "FLOW_VERSION=${FLOW_VERSION}"
- "NEOS_BUILD_DIR=/woodpecker/Build-${FLOW_VERSION}"
commands:
- "sudo mkdir $NEOS_BUILD_DIR"
- "sudo chown -R docker:docker $NEOS_BUILD_DIR"
- "cd $NEOS_BUILD_DIR"
- "composer create-project --no-install neos/flow-base-distribution:^$FLOW_VERSION ."
- "composer config repositories.repo-name path /woodpecker/package"
- "composer config --no-plugins allow-plugins.neos/composer-plugin true"
- "composer require digicomp/flow-translation-endpoint:@dev"
- "bin/phpunit --configuration Build/BuildEssentials/PhpUnit/FunctionalTests.xml Packages/Application/DigiComp.FlowTranslationEndpoint/Tests/Functional"

View file

@ -0,0 +1,180 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint\Http;
use GuzzleHttp\Psr7\Response;
use Neos\Cache\Exception as CacheException;
use Neos\Cache\Exception\InvalidDataException;
use Neos\Cache\Frontend\StringFrontend;
use Neos\Flow\I18n\Detector;
use Neos\Flow\I18n\Service;
use Neos\Flow\I18n\Xliff\Service\XliffFileProvider;
use Neos\Utility\Arrays;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class TranslationRequestMiddleware implements MiddlewareInterface
{
/**
* @var string
*/
protected string $reactOnPath;
/**
* @var string
*/
protected string $getParameterName;
/**
* @var int
*/
protected int $browserCacheMaxAge;
/**
* @var Service
*/
protected Service $i18nService;
/**
* @var XliffFileProvider
*/
protected XliffFileProvider $fileProvider;
/**
* @var Detector
*/
protected Detector $detector;
/**
* @var StringFrontend
*/
protected StringFrontend $responseCache;
public function __construct(
string $reactOnPath,
string $getParameterName,
int $browserCacheMaxAge,
Service $i18nService,
XliffFileProvider $fileProvider,
Detector $detector,
StringFrontend $responseCache
) {
$this->reactOnPath = $reactOnPath;
$this->getParameterName = $getParameterName;
$this->browserCacheMaxAge = $browserCacheMaxAge;
$this->i18nService = $i18nService;
$this->fileProvider = $fileProvider;
$this->detector = $detector;
$this->responseCache = $responseCache;
}
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws CacheException
* @throws InvalidDataException
* @throws \JsonException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($request->getUri()->getPath() === $this->reactOnPath) {
return $this->createResponse($request);
}
return $handler->handle($request);
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws \JsonException
* @throws CacheException
* @throws InvalidDataException
*/
protected function createResponse(ServerRequestInterface $request): ResponseInterface
{
if ($request->hasHeader('Accept-Language')) {
$wishedLocale = $this->detector->detectLocaleFromHttpHeader($request->getHeader('Accept-Language')[0]);
$bestMatching = $this->i18nService->findBestMatchingLocale($wishedLocale);
$this->i18nService->getConfiguration()->setCurrentLocale($bestMatching);
}
$idPatternList = $request->getQueryParams()[$this->getParameterName] ?? '';
$cacheId = $this->i18nService->getConfiguration()->getCurrentLocale() . '_' . \sha1(\serialize($idPatternList));
if ($this->responseCache->has($cacheId)) {
$response = $this->responseCache->get($cacheId);
} else {
$result = $this->getTranslations(Arrays::trimExplode(',', $idPatternList));
$response = \json_encode($result, \JSON_THROW_ON_ERROR);
$this->responseCache->set($cacheId, $response);
}
return new Response(
200,
[
'Content-Type' => 'application/json',
'Cache-Control' => 'max-age=' . $this->browserCacheMaxAge . ', must-revalidate',
'Last-Modified' => $this->getCacheDate(),
'Vary' => 'Accept, Accept-Language'
],
$response
);
}
/**
* @param array $idPatternList
*
* @return array
*/
protected function getTranslations(array $idPatternList): array
{
$result = [];
foreach ($idPatternList as $idPattern) {
$package = 'Neos.Flow:Main';
$parts = \explode('|', $idPattern);
switch (\count($parts)) {
case 2:
[$package, $pattern] = $parts;
break;
case 1:
[$pattern] = $parts;
break;
default:
throw new \InvalidArgumentException('Could not parse idPattern: ' . $idPattern);
}
$translationUnits = $this->fileProvider->getFile(
$package,
$this->i18nService->getConfiguration()->getCurrentLocale()
)->getTranslationUnits();
if (!isset($result[$package])) {
$result[$package] = [];
}
$matchingUnits = \array_filter(
$translationUnits,
static fn($unitId) => \preg_match('~^' . $pattern . '$~', $unitId),
\ARRAY_FILTER_USE_KEY
);
$result[$package] += \array_map(
static fn($value) => $value[0]['target'],
$matchingUnits
);
}
return $result;
}
/**
* @return string
*/
protected function getCacheDate(): string
{
// the translation file monitor will flush our complete cache, each time a file is modified. That way it resets
// the last modified date here, too.
if (false === $lastModified = $this->responseCache->get('lastModified')) {
$lastModified = (new \DateTimeImmutable())->format(\DATE_RFC7231);
$this->responseCache->set('lastModified', $lastModified);
}
return $lastModified;
}
}

32
Classes/Package.php Normal file
View file

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint;
use Neos\Flow\Cache\CacheManager;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Monitor\FileMonitor;
class Package extends \Neos\Flow\Package\Package
{
public function boot(Bootstrap $bootstrap)
{
parent::boot($bootstrap);
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(
FileMonitor::class,
'filesHaveChanged',
static function ($fileMonitorId, $changedFiles) use ($bootstrap) {
if ($fileMonitorId !== 'Flow_TranslationFiles') {
return;
}
if ($changedFiles !== []) {
$cacheManager = $bootstrap->getObjectManager()->get(CacheManager::class);
$cache = $cacheManager->getCache('DigiComp_FlowTranslationEndpoint_Responses');
$cache->flush();
}
}
);
}
}

View file

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint\ViewHelpers;
use Neos\Flow\I18n\Service as I18nService;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
class CurrentHtmlLangViewHelper extends AbstractViewHelper
{
/**
* @var I18nService
*/
protected I18nService $i18nService;
/**
* @param I18nService $i18nService
*/
public function __construct(I18nService $i18nService)
{
$this->i18nService = $i18nService;
}
/**
* @return string
*/
public function render(): string
{
return \str_replace('_', '-', (string)$this->i18nService->getConfiguration()->getCurrentLocale());
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint\ViewHelpers;
use Neos\Flow\Annotations as Flow;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
class ParameterNameViewHelper extends AbstractViewHelper
{
/**
* @Flow\InjectConfiguration(package="DigiComp.FlowTranslationEndpoint", path="getParameterName")
* @var string
*/
protected $parameterName;
public function render()
{
return $this->parameterName;
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint\ViewHelpers;
use Neos\Flow\Annotations as Flow;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
class UriViewHelper extends AbstractViewHelper
{
/**
* @Flow\InjectConfiguration(package="DigiComp.FlowTranslationEndpoint", path="reactOnPath")
* @var string
*/
protected $reactOnPath;
public function render()
{
return $this->reactOnPath;
}
}

View file

@ -0,0 +1,3 @@
DigiComp_FlowTranslationEndpoint_Responses:
frontend: Neos\Cache\Frontend\StringFrontend
backend: Neos\Cache\Backend\SimpleFileBackend

View file

@ -0,0 +1,26 @@
DigiComp.FlowTranslationEndpoint:TranslationResponseCache:
className: "Neos\\Cache\\Frontend\\StringFrontend"
factoryObjectName: "Neos\\Flow\\Cache\\CacheManager"
factoryMethodName: "getCache"
arguments:
1:
value: "DigiComp_FlowTranslationEndpoint_Responses"
DigiComp.FlowTranslationEndpoint:TranslationRequestMiddleware:
className: "DigiComp\\FlowTranslationEndpoint\\Http\\TranslationRequestMiddleware"
autowiring: true
arguments:
1:
setting: "DigiComp.FlowTranslationEndpoint.reactOnPath"
2:
setting: "DigiComp.FlowTranslationEndpoint.getParameterName"
3:
setting: "DigiComp.FlowTranslationEndpoint.browserCacheMaxAge"
4:
object: "Neos\\Flow\\I18n\\Service"
5:
object: "Neos\\Flow\\I18n\\Xliff\\Service\\XliffFileProvider"
6:
object: "Neos\\Flow\\I18n\\Detector"
7:
object: "DigiComp.FlowTranslationEndpoint:TranslationResponseCache"

View file

@ -0,0 +1,14 @@
Neos:
Flow:
http:
middlewares:
translation:
position: "start 100"
middleware: "DigiComp.FlowTranslationEndpoint:TranslationRequestMiddleware"
DigiComp:
FlowTranslationEndpoint:
reactOnPath: '/xliff-units'
getParameterName: "idPatterns"
# default is 6 minutes, so we "expect" in average 3 minutes of "old" translation in worst case
browserCacheMaxAge: 360

View file

@ -0,0 +1,4 @@
DigiComp:
FlowTranslationEndpoint:
reactOnPath: 'testing/translate'
getParameterName: "idPatterns"

58
README.md Normal file
View file

@ -0,0 +1,58 @@
# DigiComp.FlowTranslationEndpoint
![Build status](https://ci.digital-competence.de/api/badges/Packages/DigiComp.FlowTranslationEndpoint/status.svg)
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 `TranslationRequestMiddleware`, which will respond to any request, where the request path equals `DigiComp.FlowTranslationEndpoint.reactOnPath` (Default: "/xliff-units"), and search for unit patterns in the `DigiComp.FlowTranslationEndpoint.getParameterName` (Default: "idPatterns").
"idPatterns" is built with following syntax:
`packageName:catalogName|SEARCH_REGEX, ANOTHER PATTERN...`
For example:
````
GET /xliff-units?idPatterns=Neos.Flow:Main|authentication.*
````
would return all translation keys from the main unit of `Neos.Flow` starting with "authentication" and would look like that:
```json
{
"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.
Given your HTML head looks like that:
```html
<html lang="{translation:currentHtmlLang()}" data-xliff-uri="{translation:uri()}" data-xliff-parameter="{translation:parameterName()}">
```
Your JavaScript could look like that:
```javascript
async function translate(idPatterns) {
const uri = new URL(document.documentElement.dataset.xliffUri, document.location);
uri.searchParams.set(document.documentElement.dataset.xliffParameter, idPatterns);
const response = await fetch(uri, {headers: {
'Accept': 'application/json',
'Accept-Language': document.documentElement.lang,
}});
if (! response.ok) {
return Promise.reject('Unexpected server response');
}
return await response.json();
}
```
Last but not least:
Do not forget to have a lot of fun.

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="Test" product-name="DigiComp.FlowTranslationEndpoint" source-language="en" datatype="plaintext">
<body>
<trans-unit id="key1">
<source>de_key1</source>
</trans-unit>
</body>
</file>
</xliff>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="Test" product-name="DigiComp.FlowTranslationEndpoint" source-language="en" datatype="plaintext">
<body>
<trans-unit id="key1">
<source>en_key1</source>
</trans-unit>
</body>
</file>
</xliff>

View file

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint\Tests\Functional;
use Neos\Flow\Tests\FunctionalTestCase;
use Psr\Http\Message\ServerRequestFactoryInterface;
class TranslationMiddlewareTest extends FunctionalTestCase
{
/**
* @var ServerRequestFactoryInterface
*/
protected $serverRequestFactory;
protected function setUp(): void
{
parent::setUp();
$this->serverRequestFactory = $this->objectManager->get(ServerRequestFactoryInterface::class);
}
/**
* @test
*/
public function itRespondsToConfiguredRoute(): void
{
$request = $this->serverRequestFactory->createServerRequest('GET', 'testing/translate');
$request = $request
->withQueryParams(['idPatterns' => 'DigiComp.FlowTranslationEndpoint:Test|.*'])
->withHeader('Accept-Language', 'en');
$response = $this->browser->sendRequest($request);
static::assertEquals(
'{"DigiComp.FlowTranslationEndpoint:Test":{"key1":"en_key1"}}',
(string)$response->getBody()
);
$request = $this->serverRequestFactory->createServerRequest('GET', 'testing/translate');
$request = $request
->withQueryParams(['idPatterns' => 'DigiComp.FlowTranslationEndpoint:Test|.*'])
->withHeader('Accept-Language', 'de');
$response = $this->browser->sendRequest($request);
static::assertEquals(
'{"DigiComp.FlowTranslationEndpoint:Test":{"key1":"de_key1"}}',
(string)$response->getBody()
);
}
}

45
composer.json Normal file
View file

@ -0,0 +1,45 @@
{
"name": "digicomp/flow-translation-endpoint",
"description": "A simple endpoint providing XLIFF translations as string",
"type": "neos-package",
"require": {
"ext-json": "*",
"neos/flow": "^6.3.0 | ^7.0 | ^8.0",
"php": ">=7.4"
},
"autoload": {
"psr-4": {
"DigiComp\\FlowTranslationEndpoint\\": "Classes/"
}
},
"autoload-dev": {
"psr-4": {
"DigiComp\\FlowTranslationEndpoint\\Tests\\": "Tests/"
}
},
"extra": {
"neos": {
"package-key": "DigiComp.FlowTranslationEndpoint"
},
"branch-alias": {
"dev-develop": "1.0.x-dev"
}
},
"authors": [
{
"name": "Ferdinand Kuhl",
"email": "f.kuhl@digital-competence.de",
"homepage": "https://www.digital-competence.de",
"role": "Developer"
}
],
"license": "MIT",
"homepage": "https://git.digital-competence.de/Packages/DigiComp.FlowTranslationEndpoint",
"keywords": [
"Neos",
"Flow",
"translation",
"xliff",
"json"
]
}