<p>You can use <a href="https://craftcms.com/classreference/helpers/UrlHelper#getSiteUrl-detail" rel="nofollow">UrlHelper::getSiteUrl()</a> instead, which does have a <code>$localeId</code> argument, for generating a URL relative to a given locale’s base URL (per the <a href="https://craftcms.com/docs/config-settings#siteUrl" rel="nofollow">siteUrl</a> config setting).</p>

<p>So if your <code>siteUrl</code> config setting is set to:</p>



<pre class="lang-php prettyprint-override"><code>'siteUrl' =&gt; array(
    'en' =&gt; 'http://example.com/',
    'de' =&gt; 'http://example.com/de/',
)
</code></pre>

<p>and you do this:</p>

<pre class="lang-php prettyprint-override"><code>UrlHelper::getSiteUrl('foo/bar', null, null, 'de');
</code></pre>

<p>it will return this:</p>

<pre class="lang-php prettyprint-override"><code>http://example.com/de/foo/bar
</code></pre>

<p>(Regardless of which site you call it from.)</p>

<p>UrlHelper::getSiteUrl() is also available to Twig templates via a <code>siteUrl()</code> global function:</p>

<pre class="lang-php prettyprint-override"><code>{{ siteUrl('foo/bar', null, null, 'de') }}
</code></pre>

CC BY-SA 3.0
2016-02-24T23:22:20.277Z
2016-02-24T23:27:47.323Z
2016-02-24T23:27:47.323Z
Brandon Kelly
<p><code>UrlHelper::getUrl()</code> 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 <code>/foo/bar</code> or <code>foo/bar</code>?</p>

<plugin-development><localization><url><config>
How can I localize a URL in a plugin?
# How can I localize a URL in a plugin?

(Asked by: username_0 on 2016-02-23)

`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`?

**Tags:** plugin-development, localization, url, config


---

## 1 answers

### Answer by username_1 on 2016-02-24. Score: 5 | Accepted answer

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') }}


---

