Same websites with different languages in different domains (routing)


I have a website that is accessible in two different domains, same content but different language. The routes are written for the respective language. I need to create routing rules for both domains and be able to create the URLs for both domains in the same request to be able to write hreflang tags.

I’ve tried to use the ‘_host’ route parameter by duplicating the routes but then I get duplicated route names. If I delete the route names only the first defined set of routes are used.

$routes->scope('/', function (RouteBuilder $builder) {
    // Register scoped middleware for in scopes.
    $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
        'httpOnly' => true,

     * Apply a middleware to the current route scope.
     * Requires middleware to be registered through `Application::routes()` with `registerMiddleware()`

     * Here, we are connecting '/' (base path) to a controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, templates/Pages/home.php)...
    $builder->connect('/', ['controller' => 'Articles', 'action' => 'index'])->setHost('');
    $builder->connect('/', ['controller' => 'Articles', 'action' => 'index'])->setHost('');

I could generate two RouteBuilders but then I would have to set the rule set in the static Routes class every time I need to generate a URL for the alternate site.

Do you know any simpler solution?