1

UrlHelper::getUrl() doesn't appear to take the current locale ID into account when generating a URL. Absolute URLs are fine as it handles that but is there a best practice way to localize relative Urls such as /foo/bar or foo/bar?

5

You can use UrlHelper::getSiteUrl() instead, which does have a $localeId argument, for generating a URL relative to a given locale’s base URL (per the siteUrl config setting).

So if your siteUrl config setting is set to:

'siteUrl' => array(
    'en' => 'http://example.com/',
    'de' => 'http://example.com/de/',
)

and you do this:

UrlHelper::getSiteUrl('foo/bar', null, null, 'de');

it will return this:

http://example.com/de/foo/bar

(Regardless of which site you call it from.)

UrlHelper::getSiteUrl() is also available to Twig templates via a siteUrl() global function:

{{ siteUrl('foo/bar', null, null, 'de') }}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.