Can I pass variable into module field translations?

Hi all,
I know module in hubspot support multiple language via json file. Wondering can the message in json file be something like: “Name is {{name}}” so that I can pass some variable into it. Usually i18n handle the translation that way: Interpolation | i18next documentation
Also wondering does the translation in hubspot support plurals like the ones in i18n: Plurals | i18next documentation

Thanks

Hey @iqoOopi - thanks for posting in the Community!
I’m not finding any documentation that answers this question directly, so I’d like to tag in a few Community experts to see if they have any insight that could be of use here! @Kevin-C, @SteveHTM, and @stefen - any thoughts on helping out @iqoOopi here?
Shane, Community Manager

Hey @iqoOopi,
yes it’s possible, but it’s not really gonna work with module translations because the’re only handling existing module fields. So unless you have text fields for “name” and “names” it won’t really work.

You got several other options you can try.

First of all the most easiest one that will be living in the module.html:

{% if module.some-number >= 2 %}
 {% if content.domain is string_containing == "/de" %}
 {% set name_plural = "Namen" %}
 {% elif content.domain is string_containing == "/en" %}
 {% set name_plural = "names" %}
 {% else %} {# Fallback if no string_containing returns true #}
 {% set name_plural = "names" %}
 {% endif %}
{% else %}
 {% if content.domain is string_containing == "/de" %}
 {% set name_plural = "Name" %}
 {% elif content.domain is string_containing == "/en" %}
 {% set name_plural = "name" %}
 {% else %} {# Fallback if no string_containing returns true #}
 {% set name_plural = "name" %}
 {% endif %}
{% endif %}

If you want a more global level of editing and keeping the module.html cleaner, you can create your own _locales folder structure somewhere in the theme

  • _locales
    • de
      • messages.json
    • en
      • messages.json

You can find an example in the @hubspot/cmsdefaultsystempages folder. Just look at something like the email-subscription-preferences.html and the system-base.html.

The key here is to have a variable like this in your theme or module

{% set template_translations = load_translations("./_locales", html_lang, "en") %}

as well as the mentioned folder-structure (you might need to change the path).

Once this is implemented, you can open the messages.json file(s) and type in something like

en/messages.json:

{
 "my_name_labels":{
 "singular": "name",
 "plural": "names"
 }
}

de/messages.json:

{
 "my_name_labels":{
 "singular": "Name",
 "plural": "Namen"
 }
}

the module.html would look like this:

{% set translations = load_translations("./_locales", html_lang, "en") %}
...

{% if module.some_number >= 2 %}
 {% set label = translations.my_name_labels.plural %}
{% else %}
 {% set label = translations.my_name_labels.singular %}
{% endif %}
...

<span>There are {{ module.some_number }} {{ label }} in the list </span>

best,

Anton