What's your biggest current challenge? Have your say in Community Polls along the right column.

How can I load an article by locale using Liquid?

How can I load an article by locale using Liquid?

abiratsis
Shopify Partner
10 1 0

Hello, I am currently loading an Article dynamically as shown below:

 

    {% assign introductionPath = blog.handle | append: '/introduction'  %}
    {% assign introductionSection = articles[introductionPath]  %}

{{ introductionSection.content }} <!-- this shows always the default language -->

although the problem with this approach is that the current locale is completely ignored. Is there any way to rewrite the above script so that I include the current selected locale and load the content with the correct language?

 

Thank you for the help

Replies 6 (6)

Dan-From-Ryviu
Shopify Partner
10303 2044 2115

Hi @abiratsis 

You can refer to this code and check if it works

{% assign introductionPath = blog.handle | append: '/introduction' %}
{% assign introductionSection = articles[introductionPath] %}

{% capture localeContentKey %}{{ introductionPath }}_{{ request.locale | downcase }}{% endcapture %}
{% assign localeContent = introductionSection[localeContentKey] %}
{{ localeContent.content }}

- Helpful? Like and Accept solution! Or Support me: Buy Coffees
- Reton: Loyalty & Rewards - Earn points through tasks, redeem for discounts, and enjoy exclusive VIP rewards!
- Ryviu - Reviews & QA app: Collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy Gallery: Lookbook Image: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

abiratsis
Shopify Partner
10 1 0

Thank you very much for your answer. Apparently, there is another issue with the code, when I try to print out

 

request.locale.iso_code

 

 I always get the default locale == en. Do you have any clue why is this happening?

Dan-From-Ryviu
Shopify Partner
10303 2044 2115

You can try to use this code, it will return the name of the locale in the locale itself.

{{ request.locale.endonym_name }}

 

- Helpful? Like and Accept solution! Or Support me: Buy Coffees
- Reton: Loyalty & Rewards - Earn points through tasks, redeem for discounts, and enjoy exclusive VIP rewards!
- Ryviu - Reviews & QA app: Collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy Gallery: Lookbook Image: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

abiratsis
Shopify Partner
10 1 0

Well ok, that will give me English instead of en, but the problem is that when I change to German I still get en, instead of de

Dan-From-Ryviu
Shopify Partner
10303 2044 2115

Hi @abiratsis 

You can try to use this code to get current locale and check if it works for you

{% if content_for_header %}
  {% assign locale_info = content_for_header | split: ',"locale":"' %}
  {% assign locale_info = locale_info[1] | split: '","' %}
  {% assign current_locale = locale_info[0] %}
  Current Locale: {{ current_locale }}
{% endif %}

- Helpful? Like and Accept solution! Or Support me: Buy Coffees
- Reton: Loyalty & Rewards - Earn points through tasks, redeem for discounts, and enjoy exclusive VIP rewards!
- Ryviu - Reviews & QA app: Collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy Gallery: Lookbook Image: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

abiratsis
Shopify Partner
10 1 0

Hello @Dan-From-Ryviu thank you for the answer but that didn't work either.

 

Let's assume I will manage to extract the current locale from the url, may I ask you where did you get the pattern 

 

{% capture localeContentKey %}{{ introductionPath }}_{{ request.locale.iso_code | downcase }}{% endcapture %}

 

from? I am asking because when I tried to hardocode the locale this didn't work either. For instance I tried introductionSection['myblog/introduction_en'] and this doesn't work either. So I was wondering if there is a way to get content of an article by specifying locale.

 

Thank you