October CMS resources and help articles

Simple and to the point. Optimized by the community.

Generate a page URL in a specific locale

2
by OFFLINE, last modified on August 12th, 2019

Use the rewriteTranslatablePageUrl method to get a page URL in a specific locale.

$locale = 'de';
$pageName = 'target-cms-page';
// If your model slug is translatable use
// $params = ['slug' => $model->locale($locale)->slug];
$params = ['slug' => 'your-model-slug'];

$theme = Theme::getActiveThemeCode();
$cmsPage = Page::loadCached($theme, $pageName);

$cmsPage->rewriteTranslatablePageUrl($locale);

$router = new \October\Rain\Router\Router;
$localeUrl = $router->urlFromPattern($page->url, $params);
// If you need the locale prefix use 
// $localeUrl = $router->urlFromPattern(sprintf("/%s%s", $locale, $page->url), $params);

echo url($localeUrl);

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?