Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-24T23:22:20.277Z
Answers.LastActivityDate: 2016-02-24T23:27:47.323Z
Answers.LastEditDate: 2016-02-24T23:27:47.323Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <plugin-development><localization><url><config>
Title: How can I localize a URL in a plugin?
ThreadText: # 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()](https://craftcms.com/classreference/helpers/UrlHelper#getSiteUrl-detail) instead, which does have a `$localeId` argument, for generating a URL relative to a given locale’s base URL (per the [siteUrl](https://craftcms.com/docs/config-settings#siteUrl) config setting).
  
  So if your `siteUrl` config setting is set to:
  
  ```php
  'siteUrl' => array(
      'en' => 'http://example.com/',
      'de' => 'http://example.com/de/',
  )
  
  ```
  
  and you do this:
  
  ```php
  UrlHelper::getSiteUrl('foo/bar', null, null, 'de');
  
  ```
  
  it will return this:
  
  ```php
  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:
  
  ```php
  {{ siteUrl('foo/bar', null, null, 'de') }}
  
  ```
  
  
  ---
  
  