Repository navigation
Open
Labels
good first issueGood for newcomersGood for newcomerslangLanguage and translation packageLanguage and translation packagerefactoring
Milestone
Description
Problem
Quantum\Lang\Factories\LangFactory::resolve() detects the language on every call, even when the adapter is already cached, and a few details around adapter creation are redundant.
detectLanguage()runs on everyresolve(), before the cache is checked._t()andcurrent_lang()go throughLangFactory::get(), so every translation repeats the query-parameter, URL-segment andAccept-Languagechecks and then throws the result away: the detected language is only used when the adapter is first created. It can also throwLangException::misconfiguredDefaultConfig()when an adapter is already cached (no language detected and nolang.default_locale).- The cache ignores a later language. The adapter is cached by name and keeps the language it was created with. For the web app this is by design:
PrepareRequestStagecallsLangFactory::get()once per request, which fixes the language for that app context, andLang::setLang()changes it. This is not documented in the code. - The factory builds an
HttpClientthe adapters already create.createInstance()usesnew $adapterClass($lang, $langConfig)for the file adapter andnew $adapterClass($lang, $langConfig, new HttpClient())for the others, butDeepLAdapterandGoogleTranslateAdaptertake?HttpClient $httpClient = nulland default tonew HttpClient()themselves.
Proposal (non-breaking)
- In
LangFactory::resolve(), return the cached instance before callingdetectLanguage(). Behaviour for callers does not change: today the detected value is already ignored once the adapter exists. - Document on
resolve()that the language is fixed when the adapter is first created, and changes withLang::setLang(). - In
createInstance(), build all three adapters withnew $adapterClass($lang, $langConfig)and drop theHttpClientimport.
No signature changes.
Tests
- New, must fail without the fix: after one
LangFactory::get(), removelang.default_localeand the detection sources, callLangFactory::get()again and assert the same instance is returned and nothing throws (today the second call throws). - New: after the first call, change the
langquery parameter and assert the second call still returns the instance with the first language and does not read the request again. - The existing
LangFactoryTestcases for DeepL and Google keep passing without the explicitHttpClient.
Not in this ticket (3.2.0)
The language is also stored twice at creation: the adapter constructor sets $this->lang, then Lang::__construct() calls setLang(), which calls $adapter->setLang() with the same value. It is harmless, and removing it means changing the adapter constructors to take only the config, which is a public signature change. To be handled with a 3.2.0 or later change to the adapter factories.
Acceptance
- A cached adapter is returned without any language detection.
composer cs:check,composer phpstanandvendor/bin/phpunit tests/Unit/Langpass.CHANGELOG.mdentry.
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomerslangLanguage and translation packageLanguage and translation packagerefactoring
Activity