feat(multi-language): Add multi-language support and routing updates
Add configuration for multiple languages in the Website Manager
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Plugi\Modules\Router;
|
||||
use Plugi\Contracts\PageContract;
|
||||
use Plugi\Contracts\PageTranslationContract;
|
||||
use Plugi\Contracts\RouterContract;
|
||||
use Plugi\PageTranslationService;
|
||||
use Plugi\Repositories\PageRepository;
|
||||
use Plugi\Repositories\PageTranslationRepository;
|
||||
|
||||
@@ -49,23 +50,46 @@ class DatabasePageRouter implements RouterContract
|
||||
*/
|
||||
public function resolve($url)
|
||||
{
|
||||
// strip URL query parameters
|
||||
$url = explode('?', $url, 2)[0];
|
||||
// split URL into segments using / as separator
|
||||
$this->routeParameters = [];
|
||||
$this->routeToPageTranslationIdMapping = [];
|
||||
$translationService = new PageTranslationService();
|
||||
$url = preg_split('/[?#]/', $url, 2)[0] ?? '';
|
||||
$url = preg_replace('#/+#', '/', '/' . trim($url, '/')) ?: '/';
|
||||
$urlSegments = explode('/', $url);
|
||||
|
||||
// request all routes and convert each to its segments using / as separator
|
||||
$pageTranslations = $this->pageTranslationRepository->getAll(['id', 'route']);
|
||||
$activeLanguages = phpb_active_languages();
|
||||
$pageTranslations = $this->pageTranslationRepository->getAll(['id', 'route', 'locale']);
|
||||
$routes = [];
|
||||
foreach ($pageTranslations as $pageTranslation) {
|
||||
$route = $pageTranslation->route;
|
||||
$this->routeToPageTranslationIdMapping[$route] = $pageTranslation->id;
|
||||
$routeSegments = explode('/', $route);
|
||||
$routes[] = $routeSegments;
|
||||
if (! array_key_exists($pageTranslation->locale, $activeLanguages)) {
|
||||
continue;
|
||||
}
|
||||
if (! is_string($pageTranslation->route) || trim($pageTranslation->route) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$route = $translationService->normalizeRoute($pageTranslation->route);
|
||||
$routeKey = $translationService->getRouteKey($route);
|
||||
if (isset($routes[$routeKey])) {
|
||||
$routes[$routeKey] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
$routes[$routeKey] = [
|
||||
'route' => $route,
|
||||
'translationId' => $pageTranslation->id,
|
||||
];
|
||||
}
|
||||
|
||||
// sort routes into the order for evaluation
|
||||
$orderedRoutes = $this->getRoutesInOrder($routes);
|
||||
$routeSegments = [];
|
||||
foreach ($routes as $route) {
|
||||
if ($route === false) {
|
||||
continue;
|
||||
}
|
||||
$this->routeToPageTranslationIdMapping[$route['route']] = $route['translationId'];
|
||||
$routeSegments[] = explode('/', $route['route']);
|
||||
}
|
||||
$orderedRoutes = $this->getRoutesInOrder($routeSegments);
|
||||
|
||||
// match each route with current URL segments and return the corresponding page once we find a match
|
||||
foreach ($orderedRoutes as $routeSegments) {
|
||||
@@ -186,7 +210,7 @@ class DatabasePageRouter implements RouterContract
|
||||
break;
|
||||
}
|
||||
// the URL segment matches if equal to the route segment
|
||||
if ($urlSegment === $routeSegment) {
|
||||
if (strtolower($urlSegment) === strtolower($routeSegment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user