October CMS resources and help articles

Simple and to the point. Optimized by the community.

Generate a page URL in a specific locale

7
by OFFLINE, last modified on June 11th, 2021

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($cmsPage->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?