-
Notifications
You must be signed in to change notification settings - Fork 20
IBX-12091: Added TypeScript translation extraction support #1967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mikadamczyk
merged 8 commits into
6.0
from
feature/IBX-12091-typescript-translation-extraction
Jul 28, 2026
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a6c9b07
IBX-12091: Added TypeScript translation extraction support
mikadamczyk 8a5efda
IBX-12091: Improved TypeScript translation extractor structure
mikadamczyk f1532dc
IBX-12091: Simplified runtime readiness state
mikadamczyk 2d44f1c
IBX-12091: Clarified TypeScript extractor naming
mikadamczyk 8b44786
IBX-12091: Installed frontend dependencies for backend tests
mikadamczyk 8b9bbb0
After cr
albozek d0dfdfe
IBX-12091: Addressed code review feedback
mikadamczyk 4d9ea52
Merge branch '6.0' into feature/IBX-12091-typescript-translation-extr…
mikadamczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\AdminUi\Exception; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| final class TypeScriptExtractorException extends RuntimeException | ||
| { | ||
| } |
205 changes: 205 additions & 0 deletions
205
src/lib/Translation/Extractor/TypeScriptExtractorClient.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\AdminUi\Translation\Extractor; | ||
|
|
||
| use Ibexa\AdminUi\Exception\TypeScriptExtractorException; | ||
| use JsonException; | ||
| use Symfony\Component\Process\InputStream; | ||
| use Symfony\Component\Process\Process; | ||
|
|
||
| /** | ||
| * Talks to the Node script that reads translations from TypeScript files. | ||
| * | ||
| * The state below is on purpose and only lives during one extraction run: | ||
| * we keep one Node process running ({@see self::$serverProcess}) and send it the file paths, | ||
| * instead of starting Node again for every file. {@see self::$isRuntimeReady} remembers that | ||
| * we already checked Node works, so we do not check again for each file. | ||
| */ | ||
| final class TypeScriptExtractorClient | ||
| { | ||
| private const float DEFAULT_RESPONSE_TIMEOUT_SECONDS = 30.0; | ||
|
|
||
| private string $parserScriptPath; | ||
|
|
||
| /** True once we checked Node works. Only kept in memory for this run. */ | ||
| private bool $isRuntimeReady = false; | ||
|
|
||
| private ?Process $serverProcess = null; | ||
|
|
||
| private ?InputStream $serverInput = null; | ||
|
|
||
| private string $responseBuffer = ''; | ||
|
|
||
| public function __construct( | ||
| ?string $parserScriptPath = null, | ||
| private readonly string $nodeBinary = 'node', | ||
| private readonly float $responseTimeoutSeconds = self::DEFAULT_RESPONSE_TIMEOUT_SECONDS, | ||
| ) { | ||
| $this->parserScriptPath = $parserScriptPath ?? __DIR__ . '/typescript_translation_extractor.mjs'; | ||
| } | ||
|
|
||
| public function __destruct() | ||
|
konradoboza marked this conversation as resolved.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHP doesn't allow a return type declaration |
||
| { | ||
| $this->discardServerProcess(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{messages?: mixed, warnings?: mixed, error?: string} | ||
| */ | ||
| public function extract(string $filePath): array | ||
| { | ||
| $this->assertRuntimeIsReady(); | ||
| $this->ensureServerStarted(); | ||
|
|
||
| $this->getServerInput()->write($filePath . "\n"); | ||
|
|
||
| return $this->readExtractionResponse(); | ||
| } | ||
|
|
||
| private function assertRuntimeIsReady(): void | ||
| { | ||
| if ($this->isRuntimeReady) { | ||
| return; | ||
| } | ||
|
alongosz marked this conversation as resolved.
|
||
|
|
||
| if (!is_file($this->parserScriptPath)) { | ||
| throw new TypeScriptExtractorException(sprintf( | ||
| 'TypeScript translation extractor script not found: %s.', | ||
| $this->parserScriptPath, | ||
| )); | ||
| } | ||
|
|
||
| // Runs the Node script once to check it works: this fails early if Node is missing | ||
| // or the `@typescript-eslint/typescript-estree` package is not installed. Whether each | ||
| // file parses correctly is checked later in readExtractionResponse(). | ||
| $runtimeCheckProcess = new Process([ | ||
| $this->nodeBinary, | ||
| $this->parserScriptPath, | ||
| '--check-runtime', | ||
| ]); | ||
| $runtimeCheckProcess->run(); | ||
|
|
||
| if (!$runtimeCheckProcess->isSuccessful()) { | ||
| throw new TypeScriptExtractorException(sprintf( | ||
| 'TypeScript translation extractor runtime is not available. Please ensure `%s` is installed and `@typescript-eslint/typescript-estree` is available for the admin-ui package. %s', | ||
| $this->nodeBinary, | ||
| trim($runtimeCheckProcess->getErrorOutput()), | ||
| )); | ||
| } | ||
|
|
||
| $this->isRuntimeReady = true; | ||
| } | ||
|
|
||
| private function ensureServerStarted(): void | ||
| { | ||
| if ($this->serverProcess !== null && $this->serverProcess->isRunning()) { | ||
| return; | ||
| } | ||
|
|
||
| $processInput = new InputStream(); | ||
|
|
||
| $extractorProcess = new Process([ | ||
| $this->nodeBinary, | ||
| $this->parserScriptPath, | ||
| '--serve', | ||
| ]); | ||
| $extractorProcess->setInput($processInput); | ||
| $extractorProcess->setTimeout(null); | ||
| $extractorProcess->start(); | ||
|
|
||
| $this->serverInput = $processInput; | ||
| $this->serverProcess = $extractorProcess; | ||
| $this->responseBuffer = ''; | ||
| } | ||
|
|
||
| private function getServerInput(): InputStream | ||
| { | ||
| if ($this->serverInput === null) { | ||
| throw new TypeScriptExtractorException('TypeScript translation extractor input stream has not been started.'); | ||
| } | ||
|
|
||
| return $this->serverInput; | ||
| } | ||
|
|
||
| private function getServerProcess(): Process | ||
| { | ||
| if ($this->serverProcess === null) { | ||
| throw new TypeScriptExtractorException('TypeScript translation extractor process has not been started.'); | ||
| } | ||
|
|
||
| return $this->serverProcess; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{messages?: mixed, warnings?: mixed, error?: string} | ||
| */ | ||
| private function readExtractionResponse(): array | ||
| { | ||
| $process = $this->getServerProcess(); | ||
| $responseDeadline = microtime(true) + $this->responseTimeoutSeconds; | ||
|
|
||
| while (!str_contains($this->responseBuffer, "\n")) { | ||
| if (!$process->isRunning()) { | ||
| $this->discardServerProcess(); | ||
|
|
||
| throw new TypeScriptExtractorException(sprintf( | ||
| 'TypeScript translation extractor process terminated unexpectedly: %s', | ||
| trim($process->getErrorOutput()), | ||
| )); | ||
| } | ||
|
|
||
| if (microtime(true) > $responseDeadline) { | ||
| $this->discardServerProcess(); | ||
|
|
||
| throw new TypeScriptExtractorException( | ||
| 'Timed out waiting for the TypeScript translation extractor process to respond.', | ||
| ); | ||
| } | ||
|
|
||
| $this->responseBuffer .= $process->getIncrementalOutput(); | ||
|
|
||
| if (!str_contains($this->responseBuffer, "\n")) { | ||
| usleep(2000); | ||
| } | ||
| } | ||
|
|
||
| [$responseLine, $rest] = explode("\n", $this->responseBuffer, 2); | ||
| $this->responseBuffer = $rest; | ||
|
|
||
| return $this->decodeResponse($responseLine); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{messages?: mixed, warnings?: mixed, error?: string} | ||
| */ | ||
| private function decodeResponse(string $responseLine): array | ||
| { | ||
| try { | ||
| $responseData = json_decode($responseLine, true, 512, JSON_THROW_ON_ERROR); | ||
| } catch (JsonException) { | ||
| return ['error' => 'Unable to decode extractor output.']; | ||
| } | ||
|
|
||
| if (!is_array($responseData)) { | ||
| return ['error' => 'Extractor output must be a JSON object.']; | ||
| } | ||
|
|
||
| return $responseData; | ||
| } | ||
|
|
||
| private function discardServerProcess(): void | ||
| { | ||
| $this->serverInput?->close(); | ||
| $this->serverProcess?->stop(3); | ||
|
|
||
| $this->serverProcess = null; | ||
| $this->serverInput = null; | ||
| $this->responseBuffer = ''; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.