Pulling data from HubDB based on language selected
I have a `products` table with multiple prices since these products are available in several countries.
Based on this, if a user decides to change the language of the site they're on, I want to show them that countries price. But unsure on how this can be achieved?
This is how I'm displaying prices currently:
{% set query = hubdb_table_rows([table_id], "&product_id="~module.link_product) %}
{% for row in query %}
{% set product_price_global = row["price_global"] %}
{% set product_price_france = row["price_france"] %}
<a href="#">{{ product_price_global }}</a>
{% endfor %}
As you can see, I only have the global price displayed. However, if a user chooses to change language to French, I want to display the France prices.
How can this be achieved? The only way I can think of is to check if a cookie exists i.e. "if a cookie with the value `fr` exists, show this price", but I can't see a way on how you can check cookies then perform statements around them in HubL.
The solution depends on how you have your multilingual content set up. Is it using Hubspot's language selector module? If so, you could utilize Hubspot's Format Currency filter within conditionals.
{% set query = hubdb_table_rows([table_id], "&product_id="~module.link_product) %}
{% for row in query %}
{% set product_price_global = row["price_global"] %}
{% if content.language.languageTag == "fr" %}
{{ product_price_global|format_currency("fr-FR") }}
{% elif content.language.languageTag == "es" %} {{ product_price_global|format_currency("es-ES") }} {% else %} {{ product_price_global|format_currency("en-US") }} {% endif %}
If you're translating the content using something else like Google Translate, you'll probably have to use some Javascript to detect which language the page is translated to and then set up your conditionals - or give it a shot using your cookie idea.
If nothing above works, can you provide a little more info on exactly how you're currently determining which language to display the content into, etc? I can further assist from there 🙂