Hi All,
I’m looking into how to setup a multi-region website with kirby. For the regions where we don’t own the top level domain we would like to do something like Nike does:
http://www.nike.com/ar/es_la/
so:
<domain>/<region>/<lang>
What would be the best way to make this happen? I configured the languages in my config.php but this results in
<domain>/<language>/
Thanks
You could try with routes. Example:
c::set('routes', array(
array(
'pattern' => 'us/(:any)/(:all)',
'action' => function($lang, $uri) {
$page = page($uri);
return site()->visit($page, $lang);
}
)
));
But this will only work if all languages use a language code. Guess you could make the region dynamic as well.
c::set('routes', array(
array(
'pattern' => '(:any)/(:any)/(:all)',
'action' => function($region, $lang, $uri) {
$page = page($uri);
return site()->visit($page, $lang);
}
)
));
You would have to also make sure to create custom URLs.
Thanks for your help. Only I don’t understand exactly what you mean by creating some custom urls? Should I change my contact structure to do this?
No, what I mean is this: The native url()
method ($page->url()
) will not reflect the routes, so instead of getting the intended URL with the region code, it will give you the standard URL.
But you can create a custom page method, let’s call it regionURL()
that you could use instead of the native URL method and which would output the desired URL, something like:
page::$methods['regionURL'] = function($page) {
// warning: $region is not defined in this example, maybe you can pass it as an argument
return $site->url() . '/' . $region . '/' . $site->language()->code() . '/' . $page->uri();
};
Guess you would need some sort of mapping between language code and region code to make this work but should be doable.
Please also note that using this route you end up with duplicate URLs that you would have to take care of.
Thanks for your help! I almost got it to work, except the homepage.
The problem is that the homepage automatically redirects to /en even though I don’t have auto language detect enabled. It removes the /region from the url and leaves us with /language. It does return the page in the right language. What would be the best way to remove this automatic redirect on the homepage?
When I browse to i.e /us/en/home it does work correctly.
Try with a second route:
c::set('routes', array(
array(
'pattern' => '(:any)/(:any)/(:all)',
'action' => function($region, $lang, $uri) {
$page = page($uri);
return site()->visit($page, $lang);
}
),
array(
'pattern' => '(:any)/(:any)',
'action' => function($region, $lang) {
return site()->visit('/', $lang);
}
),
));
``
1 Like
Thanks a million for the help and the quick replies! It’s working now.
1 Like