andersundsehr/geo_redirect

根据浏览器语言和IP国家将用户重定向到网站的相应语言版本

安装: 604

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:typo3-cms-extension

1.0.3 2024-07-04 11:45 UTC

This package is auto-updated.

Last update: 2024-09-04 12:10:57 UTC


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获取所有相关信息,并自行设置siteLanguageredirectUrl

<?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用♥️制作

如果某些事情没有按预期工作 😮
或者您欣赏这个扩展 🥰,请告诉我们。

我们正在招聘 https://www.andersundsehr.com/karriere/