Hey @piersg ,
Sorry to revisit this, just setting it live now for the client.
I have an issue on the blog listing pages. This is the code I’m using. The issue is the current language appears twice and the dropdown no longer works. It works fine on normal pages. Am I missing something?
<div class="lang">
{% if content.translated_content|length %}
{% for item in content.translated_content|sort(False, False, 'language') %}
<!-- you can set the text next to the flag here. -->
{% if item.language == 'en-us' %}
{% set name = 'USA' %}
{% elif item.language == 'nl' %}
{% set name = 'NL' %}
{% endif %}
<!-- this grabs the current lang switcher you are on, you can edit the text next to the flag here -->
{% set currLang = content.language.languageTag %}
{% if content.language.languageTag == 'en-us' %}
{% set currLang = 'USA' %}
{% endif %}
<!-- this sets the translated page URL based on content Id -->
{% set translated_page = content_by_id(item.id) %}
<!-- this is the current switch you have selected -->
<a id="lang-toggle" class="lang__option lang__option--current" href="#">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{{ content.language.languageTag }}.png" width="15px" style="margin-right:5px;">
{{ currLang }} <span class="lang__chevron">›</span>
</a>
<!-- this is the sub menu of the language switcher, it auto populates once you create language variable pages -->
<ul class="lang__list" style="min-width: 6rem;">
<li>
<a href="https://www.fulfilmentcrowd.com" class="lang__option">
<img src="https://f.hubspotusercontent40.net/hubfs/8149498/en.png" width="15px" style="margin-right:5px;">GB
</a>
</li>
<li>
<a class="lang__option" href="{{ translated_page.absolute_url }}">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{{item.language}}.png" alt="{{ name }} flag" width="15px" style="margin-right:5px;">
{{ name }}
</a>
</li>
</ul>
{% endfor %}
<!-- this is for the blogs, slightly different code to get the switcher to work but the only thing you need to edit is the item language -->
{% elif group.translations|length %}
{% for item in group.translations|sort(False, False, 'language') %}
<!-- this is the item language where you can change the text next to each flag -->
{% if item.language == 'en-us' %}
{% set name = 'USA' %}
{% elif item.language == 'nl' %}
{% set name = 'NL' %}
{% endif %}
<!-- this grabs the current lang switcher you are on, you can edit the text next to the flag here -->
{% set currLang = content.language.languageTag %}
{% if content.language.languageTag == 'en-us' %}
{% set currLang = 'USA' %}
{% endif %}
<!-- this is the current switch you have selected -->
<a id="lang-toggle" class="lang__option lang__option--current" href="#">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{{ content.language.languageTag }}.png" width="15px" style="margin-right:5px;">
{{ currLang }} <span class="lang__chevron">›</span>
</a>
<!-- this is the sub menu of the language switcher, it auto populates once you create language variable blogs -->
<ul class="lang__list" style="min-width: 6rem;">
<li>
<a href="https://www.fulfilmentcrowd.com" class="lang__option">
<img src="https://f.hubspotusercontent40.net/hubfs/8149498/en.png" width="15px" style="margin-right:5px;">GB
</a>
</li>
<li>
<a class="lang__option" href="{{ item.absolute_url }}">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{{item.language}}.png" alt="{{name}} flag" width="15px" style="margin-right:5px;">
{{ name }}
</a>
</li>
</ul>
{% endfor %}
{% endif %}
</div>
You have the current language inside the for loops that are looping over translations, so for every translation you’ll generate a new current languauge toggle. You have two languages currently (NL and en-US right?) so that’s why you’re getting current language twice. This needs to be separate/outside of you looping over translations (and I’m assuming that you don’t want a new “GB” in the lang list for each item so I’ve put that outside the loop). See if this works for you:
<div class="lang">
<!-- this grabs the current lang switcher you are on, you can edit the text next to the flag here -->
{% set currLang = content.language.languageTag %}
{% if group.language %}
{% set currLang = group.language.languageTag %}
{% endif %}
{% if currLang == 'en-us' %}
{% set currLang = 'USA' %}
{% endif %}
<!-- this is the current switch you have selected -->
<a id="lang-toggle" class="lang__option lang__option--current">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{% if group.language %}{{ group.language }}{% else %}{{ content.language.languageTag }}{% endif %}.png">
{{ currLang }} <span class="lang__chevron">›</span>
</a>
<ul class="lang__list" style="min-width: 6rem;">
<li>
<a href="https://www.fulfilmentcrowd.com" class="lang__option">
<img src="https://f.hubspotusercontent40.net/hubfs/8149498/en.png">GB
</a>
</li>
{% if content.translated_content|length %}
{% for item in content.translated_content|sort(False, False, 'language') %}
<!-- you can set the text next to the flag here. -->
{% if item.language == 'en-us' %}
{% set name = 'USA' %}
{% elif item.language == 'nl' %}
{% set name = 'NL' %}
{% endif %}
<!-- this sets the translated page URL based on content Id -->
{% set translated_page = content_by_id(item.id) %}
<!-- this is the sub menu of the language switcher, it auto populates once you create language variable pages -->
<li>
<a class="lang__option" href="{{ translated_page.absolute_url }}">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{{item.language}}.png" alt="{{ name }} flag">
{{ name }}
</a>
</li>
{% endfor %}
<!-- this is for the blogs, slightly different code to get the switcher to work but the only thing you need to edit is the item language -->
{% elif group.translations|length %}
{% for item in group.translations|sort(False, False, 'language') %}
<!-- this is the item language where you can change the text next to each flag -->
{% if item.language == 'en-us' %}
{% set name = 'USA' %}
{% elif item.language == 'nl' %}
{% set name = 'NL' %}
{% endif %}
<!-- this is the sub menu of the language switcher, it auto populates once you create language variable blogs -->
<li>
<a class="lang__option" href="{{ item.absolute_url }}">
<img class="lang-flag" src="https://f.hubspotusercontent10.net/hubfs/8149498/flags/{{item.language}}.png" alt="{{name}} flag">
{{ name }}
</a>
</li>
{% endfor %}
{% endif %}
</ul>
</div>
Also minor point but I removed the inline styling, as updating that in the future might get annoying. Add this to your global css:
Thanks for this, the blog now shows EN-US instead of USA.
To change this I could target group.language?
{% if group.language == ‘en-us’ %}
{% set currLang = ‘USA’ %}
@piersgWe have translated the blogs on a client site and I have a custom module ( code above ) on the homepage of the site which grabs 3 recent blog posts. Works great on the www.domain.us version of the site. We also have www.domain.nl site and the module still brings in the english version of the blogs even though I have a selector to switch to the blog ID to the dutch blog.
How can I use the code above to bring through the translated versions of the posts for the dutch site? Thank you. I’ve tried content.translated.content.absolute_url etc etc but that doesn’t seem to work.
@ScottD22 Assuming that module.blog gives you the id correctly, i.e. a number as a string, you just need to change your blog_recent_posts to (no apostrophes, no delimiters):