-
Notifications
You must be signed in to change notification settings - Fork 22
Issue/533 lang adapters #561
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
armanist
merged 16 commits into
quantum-php:master
from
armanist:issue/533-lang-adapters
Jul 12, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a51bd3d
[#533] Introduce Lang adapter foundation
armanist b9bd500
[#533] Align Lang wrapper with adapter pattern
armanist 9ee2f94
[#533] Normalize Lang config contract
armanist b88e5a1
[#533] Add remote Lang adapters and lazy loading
armanist b4816f8
[#533] Add Lang adapter tests and remove dead enabled flag
armanist f5d2e64
[#533] Record Lang breaking changes in changelog
armanist 0b4c949
[#533] Refine Lang remote adapter request and contract
armanist 1ab98c5
[#533] Fix Lang provider request failure punctuation
armanist d795007
[#533] Guard missing Lang default adapter config
armanist 915a7f8
[#533] Simplify Lang remote adapter translation flow
armanist 223e042
[#533] Expand Lang coverage for adapter edge cases
armanist 11b5b9e
[#533] Add DeepL request timeout
armanist fdd63f9
[#533] Normalize Google adapter request call
armanist bbe630c
[#533] Clean up Lang coverage and test helpers
armanist 96fcdd5
[#533] Restore Lang bootstrap before controller dispatch
armanist 270a13a
[#533] Add source catalog support for remote Lang adapters
armanist 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Quantum PHP Framework | ||
| * An open-source software development framework for PHP | ||
| * @link https://quantumphp.io | ||
| */ | ||
|
|
||
| namespace Quantum\Lang\Adapters; | ||
|
|
||
| use Quantum\Lang\Contracts\LangAdapterInterface; | ||
| use Quantum\Lang\Traits\RemoteAdapterTrait; | ||
| use Quantum\Lang\Exceptions\LangException; | ||
| use Quantum\App\Exceptions\BaseException; | ||
| use Quantum\HttpClient\HttpClient; | ||
| use ReflectionException; | ||
| use ErrorException; | ||
|
|
||
| class DeepLAdapter implements LangAdapterInterface | ||
| { | ||
| use RemoteAdapterTrait; | ||
|
|
||
| public const API_URL = 'https://api.deepl.com/v2/translate'; | ||
|
|
||
| public const TIMEOUT = 30; | ||
|
|
||
| protected string $lang; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $params | ||
| */ | ||
| public function __construct(string $lang, array $params, ?HttpClient $httpClient = null) | ||
| { | ||
| $this->lang = $lang; | ||
| $this->params = $params; | ||
| $this->httpClient = $httpClient ?? new HttpClient(); | ||
| } | ||
|
|
||
| public function setLang(string $lang): LangAdapterInterface | ||
| { | ||
| $this->lang = $lang; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int|string, mixed>|string|null $params | ||
| * @throws BaseException|ReflectionException|ErrorException | ||
| */ | ||
| public function get(string $key, $params = null): string | ||
| { | ||
| $text = $this->buildSourceText($key, $params); | ||
|
|
||
| if ($this->shouldBypassProvider($key, $text)) { | ||
| return $text; | ||
| } | ||
|
|
||
| $cached = $this->getCachedTranslation('deepl', $text); | ||
|
|
||
| if ($cached !== null) { | ||
| return $cached; | ||
| } | ||
|
|
||
| $authKey = (string) ($this->params['auth_key'] ?? ''); | ||
|
|
||
| if ($authKey === '') { | ||
| throw LangException::missingConfig('lang.deepl.auth_key'); | ||
| } | ||
|
|
||
| $response = $this->sendRequest( | ||
| (string) ($this->params['api_url'] ?? self::API_URL), | ||
| $this->buildPayload($text), | ||
| [ | ||
| 'Authorization' => 'DeepL-Auth-Key ' . $authKey, | ||
| 'Content-Type' => 'application/json', | ||
| ], | ||
| [ | ||
| CURLOPT_TIMEOUT => self::TIMEOUT, | ||
| ] | ||
| ); | ||
|
|
||
| $translation = $this->extractTranslation($response); | ||
|
|
||
| $this->setCachedTranslation('deepl', $text, $translation); | ||
|
|
||
| return $translation; | ||
| } | ||
|
|
||
| /** | ||
| * @throws LangException | ||
| */ | ||
| private function buildPayload(string $text): string | ||
| { | ||
| $payload = [ | ||
| 'text' => [$text], | ||
| 'target_lang' => strtoupper($this->lang), | ||
| ]; | ||
|
|
||
| if (!empty($this->params['source_locale'])) { | ||
| $payload['source_lang'] = strtoupper((string) $this->params['source_locale']); | ||
| } | ||
|
|
||
| $payloadJson = json_encode($payload); | ||
|
|
||
| if ($payloadJson === false) { | ||
| throw LangException::payloadEncodingFailed('DeepL'); | ||
| } | ||
|
|
||
| return $payloadJson; | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $response | ||
| * @throws LangException | ||
| */ | ||
| private function extractTranslation($response): string | ||
| { | ||
| if ( | ||
| !is_object($response) | ||
| || !isset($response->translations) | ||
| || !is_array($response->translations) | ||
| || !isset($response->translations[0]->text) | ||
| || !is_string($response->translations[0]->text) | ||
| ) { | ||
| throw LangException::invalidProviderResponse('DeepL'); | ||
| } | ||
|
|
||
| return $response->translations[0]->text; | ||
| } | ||
| } | ||
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
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.