skip to Main Content

I have the following challenge within my Shopify Shop.

I’m using ACF (Advanced Custom Fields) if I want to use it I use this code in my frontend:
{{product.metafields.warnhinweise.not-under-three-years}} the Output is: true

Now I want to output a variable from the translation file (locals/de.json), to access this variable I have to use: {{ 'products.productwarnings.not-under-three-years' | t }}

What I want to accomplish and where I currently struggle is: How could I pass the ID from my custom field as a variable name from my translation-variable?

I tried the following:

<p>Example: {{ product.metafields.warnhinweise.not-under-three-years }}</p>
<ul>
  {% for field in product.metafields.warnhinweise %}
  <li>ID: {{ field | first }} - Value: {{ field | last }}</li>
    <ul>
      <li>normal way: {{ 'products.productwarnings.not-under-three-years' | t }}</li>
      <li>nest the variable output: {{ 'products.productwarnings.{{field | first}}'  | t }}</li>
    </ul>
  {% endfor %}
</ul>

But obviously, this will not work. How could I accomplish to insert {{field | first}} into my translation-variable?

The output of the above code is the following:

Example: true

 - ID: not-under-three-years - Value: true
   - normal way: Achtung: Nicht für Kinder unter drei Jahren geeignet.
   - nest the variable output: translation missing: de.products.productwarnings.field | first

Also I get an syntax error from liquid: [dev-henry] (sections/product-template.liquid) Liquid syntax error (line 220): Unexpected character ' in "{{ 'products.productwarnings.field | first | t }}"

2

Answers


  1. You can try to supply that object key name dynamically, like this:

    <p>Example: {{ product.metafields.warnhinweise.not-under-three-years }}</p>
    <ul>
      {% for field in product.metafields.warnhinweise %}
      {% assign field_first = field | first %}
      <li>ID: {{ field_first }} - Value: {{ field | last }}</li>
        <ul>
          <li>normal way: {{ 'products.productwarnings.not-under-three-years' | t }}</li>
          <li>nest the variable output: {{ 'products.productwarnings[field_first]' | t }}</li>
        </ul>
      {% endfor %}
    </ul>
    
    Login or Signup to reply.
  2. One common misconception when working with the Liquid templating language is to try running Liquid commands inside of Liquid commands:

    {{ 'products.productwarnings.{{field | first}}' | t }} <!-- Illegal nesting, won't work -->
    

    This syntax is invalid, and at best will cause the parser to look for a translation key in your language file literally named "{{field | first}}"

    You are on the right track, however. What we will want is a string that contains the full name of the language variable that we want to use, then feed that to the translation filter. We just need to break that out into some more explicit steps:

    {% assign field_first = field | first %}
    {% assign language_key = 'products.productwarnings.' | append: field_first %}
    <h2>{{ language_key | t }}</h2>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search