lochmueller / language-detection
Modern language detection middleware for TYPO3. Based on PSR-7, PSR-14 & PSR-15.
Fund package maintenance!
lochmueller
paypal.me/lochmueller
Installs: 46 534
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 2
Forks: 5
Open Issues: 2
Type:typo3-cms-extension
Requires
- php: ^8.1
- ext-intl: *
- typo3/cms-core: ^12.3||^13.4
Requires (Dev)
- ext-json: *
- friendsofphp/php-cs-fixer: ^3.11
- geoip2/geoip2: ^2.13
- infection/infection: ^0.29
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.8
- qossmic/deptrac-shim: ^0.24||^1.0
- typo3/testing-framework: ^8.2
Suggests
- geoip2/geoip2: Allows to use local MaxMind IP database or webservice
README
Table of Contents
Why?
Language Detection should be easy & simple to integrate and powerfully in development! TYPO3 Core do not handle language detection via client information. EXT:languag_detection use a PSR-15/PSR-7 middleware/request (TYPO3 Documentation) to handle a language detection logic via PSR-14 events(TYPO3 Documentation). Very flexible! Give it a try and checkout the future of language detection!
Installation
composer require lochmueller/language-detection
Configuration
Use the site configuration module to configure the language detection. Just enable it, and it will work :) There are several configuration options for the Site configuration that handle the control events. The following screenshot show the options of the detection configuration.
Event Structure
There are four central PSR-14 events that control the language detection. The attached list explain the different events and the default listener. The events are ordered in the execution order.
CheckLanguageDetectionEvent
Check if the language detection should execute by the extension. You can register listeners for this event and call "disableLanguageDetection" on the event object to disable the language detection.
Default-Listener:
DetectUserLanguagesEvent
This event collect user information to get the user languages. You can register your own detections and manipulate the data via "getUserLanguages" and "setUserLanguages".
Default-Listener:
Please keep data privacy in mind in case of the "IpLanguage" Listener!
NegotiateSiteLanguageEvent
This event calculates the best matching page language for the user. If you build your own listener. Please use "setSelectedLanguage" on the event. If a language is already selected the default listener will be skipped.
Default-Listener:
BuildResponseEvent
The last event build the middleware response. You can overwrite this step. You have to use "setResponse" to set the response.
Default-Listener:
Troubleshooting
There are missing or wrong languages in the detection process. Why?
Do you check in incognito mode? The browser will not send all languages in incognito mode. So "wrong results" are possible. Please check the request header to TYPO3 in detail. Otherwise, perhaps the DefaultNegotiation do not handle the "best fitting language" selection process for your needs?
Why the redirect not work on subpages?
The middleware is early in the middleware stack. There is no concept of links and translations (or even page UID). Furthermore, it is recommended not redirect on subpages. A user that call a subpage first bookmark the page or search in a search engine. In both cases the user already get the right language. I suggest hreflang tags so search engines get the right language of the content https://developers.google.com/search/docs/advanced/crawling/localized-versions
Dev
Run all code standards
docker run --rm -it --volume $(pwd):/app prooph/composer:8.0 -d /app code:all
Execute tests with PHP 8.0:
docker run --rm -it --volume $(pwd):/app prooph/composer:8.0 -d /app test:unit
With coverage:
docker run --rm -it --volume $(pwd):/app cicnavi/dap:80 /app/.Build/bin/phpunit -c /app/phpunit.xml --coverage-text --testdox --coverage-html=/app/var/phpunit
Run Mutation tests:
docker run --rm -it --workdir=/app/ --volume $(pwd):/app cicnavi/dap:80 /app/.Build/bin/infection -c /app/phpunit.xml
Contribution
Thanks all for the great contribution to the project!
Licence
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.