Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Good day,
I have written a code on my product page to display an expected delivery date. I'm translating my shop into French, so I would like to translate this part, too.
According to what I read, the date is supposed to translate automatically, but it doesn't.
Here is the whole script for both languages:
{% if localization.language.endonym_name contains 'fr' %} {% assign message = 'Dépêchez-vous, plus que quelques restants!' %} {% if target.inventory_quantity > 3 %} {% assign min = 1 %} {% assign max = 3 %} {% assign diff = max | minus: min %} {% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %} {% if randomNumber == 1 %} {% assign message = 'Disponible. Envoyé depuis Hamilton, Ontario' %} {% elsif randomNumber == 2 %} {% assign todayDate = 'now' | date: "%w" %} {% unless todayDate == "4" %} {% assign seconds = 3 | times: 24 | times: 60 | times: 60 %} {% capture message %}Recevez-le le {{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }} (Express){% endcapture %} {% else %} {% assign seconds = 4 | times: 24 | times: 60 | times: 60 %} {% capture message %}Recevez-le le {{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }} (Express){% endcapture %} {% endunless %} {% else %} {% assign message = 'Envoyé depuis Hamilton, Ontario' %} {% endif %} {% endif %} {% else %} {% assign message = 'Act fast, only a few kits left!' %} {% if target.inventory_quantity > 3 %} {% assign min = 1 %} {% assign max = 3 %} {% assign diff = max | minus: min %} {% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %} {% if randomNumber == 1 %} {% assign message = 'In-stock. Ships from California' %} {% elsif randomNumber == 2 %} {% assign todayDate = 'now' | date: "%w" %} {% unless todayDate == "4" %} {% assign seconds = 3 | times: 24 | times: 60 | times: 60 %} {% capture message %}Get it by {{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }} (Expedited){% endcapture %} {% else %} {% assign seconds = 4 | times: 24 | times: 60 | times: 60 %} {% capture message %}Get it by {{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }} (Expedited){% endcapture %} {% endunless %} {% else %} {% assign message = 'Ships from San Bernadino, CA' %} {% endif %} {% endif %} {% endif %}
And here is the code used to display it on the page:
{% if product.selected_or_first_available_variant.available %} <span class="badge color-{{ settings.sale_badge_color_scheme }}{% unless display_custom_badge %} hidden{% endunless %} badge_msg" aria-hidden="true"> {{ message }} </span> {% endif %}
This code "{{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }}" doesn't translate.
I still get: "Recevez-le le Mon, Jan 01 (Express)" in french.
Do you have any advice on how I can solve the problem?
Thanks in advance for your assistance!
Cheers,
Gaétan
Hi @El_Bouck
Use reduced test cases first when seeking help , then main walls of text second after a simple problem statement. That's a lot of branching logic for someone else to get through to tell if you've made a conditional mistake, or it's just the translation system not working like assumed.
Simplify the noise and test:
{% if localization.language.endonym_name contains 'fr' %}
{{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }}
{% endif %}
Afaik you generally need to parse strings, or use even more translation keys to pull this off in liquid with the date objects. Most guides on this are only about swapping around the format strings(mm-dd vs dd-mm) and NOT about actually translating the human read word values between languages.
By parsing I mean: {{ date: '%d %B %Y' | replace: 'January', 'januari' }}
I.e. https://community.shopify.com/c/technical-q-a/change-date-format-overall-blog-view/m-p/1414976 or https://freakdesign.com.au/blogs/news/translate-a-liquid-date-string-in-shopify
@El_Bouck wrote:According to what I read, the date is supposed to translate automatically, but it doesn't.
..
This code "{{ 'now' | date: "%s" | plus: seconds | date: "%a, %b %d" }}" doesn't translate.
I still get: "Recevez-le le Mon, Jan 01 (Express)" in french.
What did you read that said it translates automatically, provide references when assumptions are in play but outputs don't match expectations.
Relevant info from shopify partner blog:
from https://www.shopify.com/partners/blog/liquid-date-format
💣BE AWARE when using 'now', generally not a problem in cart template but 🤷:
from https://shopify.dev/docs/api/liquid/filters/date
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024