1

I have the following setup for a language switcher. I have Member Directory pages which are using Users (rather than Entires or Categories). How can I update this to get it working on these pages?

{% set langSwitcher = craft.app.sites.getAllSites() %}

<div class="flag">
 {% for lang in langSwitcher %}
 {# Set homepage as default #}
 {% set url = alias(lang.baseUrl) %}

 {# Entry is defined #}
 {% if entry is defined %}
  {# Check if that entry exists in other locale #}
  {% set otherLocaleEntry = craft.entries.siteId(lang.id).id(entry.id).one() %}
  {% if otherLocaleEntry %}
    {% set url = otherLocaleEntry.url %}
  {% endif %}

{# Category is defined #}
{% elseif category is defined %}
  {# Check if that entry exists in other locale #}
  {% set otherLocaleCat = craft.categories.siteId(lang.id).id(category.id).one() %}
  {% if otherLocaleCat %}
    {% set url = otherLocaleCat.url %}
  {% endif %}
{% endif %}

 <div {% if lang.id == currentSite.id %}class="active"{% endif %}>
  <a href="{{url}}" > <img src="/assets/images/site/flag-{{lang.name|lower}}.svg" alt="{{lang.name|upper}}"></a>
 <p class="small-hide"><a href="{{url}}" >{{lang.name|upper}}</a></p>
 </div>

{% endfor %}

  • Do your directory pages just break? If so, what is the error? You can't translate users so I'm guessing you would still want to translate the UI on directory pages but stay on the current page? What is the definition for langSwitcher? – Oli Feb 14 '19 at 12:03
  • I don't get an error, I get re-directed to the homepage (of the language). Code updated with definition for langSwitcher – sarah3585 Feb 14 '19 at 12:13
  • So you want all your languages to show in the switcher but when the user chooses to change language, they stay on the user page but the UI around translates? Can you show the format of your url for a directory entry page please? – Oli Feb 14 '19 at 12:38
  • Basically they need to be redirected from website.com/membership/directory/1227(en) to website.com/ru/membership/directory/1227 (ru) – sarah3585 Feb 14 '19 at 13:00
2

This isn't perfect by any means and might need tweaking but to get you on your way:

Assuming that on a user page, you are doing {% set user = ... %} somewhere (as you would do {% set entry = ... %} on an entry page), You just add another clause to your if statement, after checking for category, you check for user:

{# User is defined #}
{% elseif user is defined %}
  {% set url = lang.baseUrl ~ 'membership/directory/' ~ craft.app.request.segments|last %}
{% endif %}

Or if you have set the user:

{# User is defined #}
{% elseif user is defined %}
  {% set url = lang.baseUrl ~ 'membership/directory/' ~ user.id %}
{% endif %}
| 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.