andersundsehr / geo_redirect
根据浏览器语言和IP国家将用户重定向到网站的相应语言版本
Requires
- php: ~8.2.0 || ~8.3.0
- composer-runtime-api: ^2
- ext-json: *
- codezero/browser-locale: ^3.3
- geoip2/geoip2: ^2
- typo3/cms-backend: ^11.5.0 || ^12.4.0
- typo3/cms-core: ^11.5.0 || ^12.4.0
- typo3/cms-redirects: ^11.5.0 || ^12.4.0
Requires (Dev)
- andersundsehr/resource-watcher: dev-master
- phpunit/phpunit: ^10.5.25
- pluswerk/grumphp-config: ^7.0.3
- saschaegerer/phpstan-typo3: ^1.10.1
- spatie/phpunit-watcher: ^1.23.6
- ssch/typo3-rector: ^2.6.2
- typo3/cms-reports: *
README
此扩展根据浏览器语言和IP国家将用户重定向到网站的相应语言版本。
安装
composer require andersundsehr/geo_redirect
前往TYPO3扩展设置并根据需要配置。
!!!
如果您想使用mmdb文件进行IP到国家的检测,应在扩展设置中添加您的 maxmindLicenseKey
(参见扩展设置)。
在此获取密钥: https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
特性
- 将域名根重定向到检测到的语言
- 将sys_redirect重定向到检测到的语言
工作原理
此扩展使用用户的Accept-Language头信息和从IP地址导出的国家来找到网站的正确语言版本。
对于IP到国家,有多种实现方式。
如果您使用Cloudflare或Sucuri,扩展将使用由服务设置的头信息。
如果您在扩展设置中设置了maxmindLicenseKey
,扩展将使用来自maxmind.com的mmdb文件。
否则它将忽略请求的原始国家。仅使用Accept-Language头信息。
选项
ipCountryIsMoreImportantThanLanguage
:如果用户获取其国家的内容比获取其语言的内容更重要,则打开此选项。maxmindLicenseKey
:Maxmind.com许可证密钥:如果您想使用mmdb文件,应添加许可证密钥(https://dev.maxmind.com/geoip/geolite2-free-geolocation-data)
mmdb文件
mmdb文件从maxmind.com下载,并每5周自动更新。
如果您始终希望为用户提供最佳性能,
应将typo3 geo-redirect:update-up-database
命令添加到每月(4周)运行。
调试
如果您请求此路径的域名:/geo_redirect/debug
,您将获得关于检测到的语言的详细说明。
基于siteLanguage的sys_redirect
此功能使您能够根据siteLanguage将用户重定向到特定页面。
为此功能正常工作,您无需进行任何配置。
所有使用t3://page?uid=链接的sys_redirect记录将自动重定向到页面的正确语言版本。
(如果重定向中未定义_language=)
扩展功能
PHP API
获取当前请求的IP国家
$ipCountryOrNull = GeneralUtility::makeInstance(IpCountryLocatorInterface::class)->getIpCountry();
获取检测到的语言
$siteLanguage = GeneralUtility::makeInstance(SiteLanguageFinderService::class)->findByRequest($request); // or if you don't have a request object: (in cli the request object is required) $siteLanguage = GeneralUtility::makeInstance(SiteLanguageFinderService::class)->findByRequest(); // the siteLanguage is never null, because it will always return the default language
自定义重定向定义
如果您想使用不同的重定向定义,可以使用\AUS\GeoRedirect\Dto\BeforeSiteLanguageFinderEvent
获取所有相关信息,并自行设置siteLanguage
和redirectUrl
。
<?php declare(strict_types=1); namespace AUS\GeoRedirect\EventListener; final class BeforeSiteLanguageFinderEventListener { public function __invoke(\AUS\GeoRedirect\Dto\BeforeSiteLanguageFinderEvent $event): void { $languageId = $this->customLanguageIdFinder($event); if ($languageId) { $event->siteLanguage = $event->site->getLanguageById($languageId); } } // ... }
添加自定义IpCountryLocator
如果您想添加自定义IpCountryLocator,可以通过添加实现IpCountryLocatorInterface
的类,并通过CollectIpCountryLocatorEvent
添加它。
<?php declare(strict_types=1); namespace AUS\GeoRedirect\EventListener; final class BeforeSiteLanguageFinderEventListener { public function __invoke(\AUS\GeoRedirect\Dto\CollectIpCountryLocatorEvent $event): void { $event->addFirst(\AUS\GeoRedirect\IpCountryLocator\MyCustomIpCountryLocator::class); // or $event->addLast(\AUS\GeoRedirect\IpCountryLocator\MyCustomIpCountryLocator::class); // or if you want to add it at a specific position: $list = $event->getLocatorClasses(); // manipulate the list as you like $event->setLocatorClasses($list); } }
由anders und sehr GmbH用♥️制作
如果某些事情没有按预期工作 😮
或者您欣赏这个扩展 🥰,请告诉我们。