1

I'm building a site with two languages, and I need a language switcher so wherever you enter the website you can switch the language easily. The website is a onepager, so i can't direct to a specific language URL. Instead i'm thinking of adding a segment (/de /fr) to the URL.

I don't know how to display the language fields and language globals.

  • Samuel, I removed your question about the visitors' language setting in the browser, as this needs to go in a separate post. But this probaby gets covered by Brandon's answer to a similar question anyways. – carlcs Dec 3 '14 at 12:53
  • I think this question is not a dupe, that's why I added an answer. The difference is, that you do not necessarily switch between entries on a one page website. This changes / simplifies the logic. – carlcs Dec 3 '14 at 21:50
  • Noted, thanks @carlcs! We'll go ahead and reopen it. – Lindsey D Dec 7 '14 at 22:58
1

The basics for setting up your localized site are nicely written down in the Localization Guide. This walks you through the steps necessary to make your site being available under their subfolders /de and /fr.

Set up correctly, your templates will always show the field content translated to the currently accessed locale.

If your one-pager template is not connected to an entry (case 4 in the Routing article) and you simply save your template files at templates/de/index.html and templates/fr/index.html (case 6), have a look at the include tag, that allows you to maintain one template responsible for all of your locales (e.g. {% include '../de/index.html' %}).

The following code is a simplified version of the example I wrote down here, and should do fine for a one-page website:

{# Loop through selected site locales #}
{% set locales = ['de', 'fr'] %}
{% for locale in locales %}

    {# Check if `locale` equals the requested page locale #}
    {% if locale == craft.locale %}
        {% set current = true %}
    {% endif %}

    {# Output a link to the hompage and add a class "current" to the current locale's link #}
    <a href="{{ craft.config.siteUrl[locale] }}" class="{{ current ? 'current' }}">{{ craft.i18n.getLocaleById(locale).name }}</a>

{% endfor %}
|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.