1

I'm trying to loop on locales to get the path to the current entry, but for the other locales (for a language switcher).

Here is my code:

{% set locales = craft.i18n.getSiteLocales() %}
{% for locale in locales %}

    {% set localeEntry = craft.entries.id(entry.id).locale(locale.id).first %}

        {{dump(localeEntry.locale)}}
        {{dump(localeEntry.getUrl()) }}

{% endfor %}

Results:

"en"
"http://website.dev/en/page"

"en_gb"
"http://website.dev/en/page"

"en_us"
"http://website.dev/en/page"

"de"
"http://website.dev/de/page"

The language only versions are fine en and de, though the language-location versions are incorrect. These are all set correctly in my config.

What am I doing wrong here?

  • 1
    In your channel section, how did you define your url-format for your different locales? – Jan_dh Feb 7 '17 at 18:08
  • Different for quite a few sections, sometimes just {slug}, sometimes news/{slug}. I never put {locale} in if that's what you're curious about? I've seen that isn't recommended. – joe Feb 8 '17 at 9:15
  • Nevermind, sorted it. – joe Feb 8 '17 at 10:36
2

Turns out the issue was my config set incorrectly. I was doing this:

'siteUrl' => array(
    'en' => 'http://website.dev/en',
    'en-gb' => 'http://website.dev/en-gb',
    'en-us' => 'http://website.dev/en-us',
    'en-au' => 'http://website.dev/en-au',
    'en-nz' => 'http://website.dev/en-nz'
),

When the array keys need to match the way they are set in craft with underscores (and your public/locale/index.php file)

'siteUrl' => array(
    'en' => 'http://website.dev/en',
    'en_gb' => 'http://website.dev/en-gb',
    'en_us' => 'http://website.dev/en-us',
    'en_au' => 'http://website.dev/en-au',
    'en_nz' => 'http://website.dev/en-nz'
),

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.