platformOS Community

What is the easiest way to pass a translation string to render argument?

zx Dec 15 2020 at 09:43

I have a render that I am using to output some content on the page. I want this content to be taken from translation strings, so something like this:

{% render 'theme/simple/ui/content', content: app.content | t %}

This statement won't work obviously. I am wondering if there is any clean solution to this problem? I can obviously assign the translation to another variable but it seems like a unnecessary additional step.

piotrze Dec 15 2020 at 11:10

You can pass to the partial content_key and then in it apply translation. Moreover your partial can take either content or content_key

# theme/simple/ui/content.liquid
{% liquid
  if content_key
    assign content = content_key | t
  endif
%}
{{ content }}
	
	
# some_file.liquid
	
{% render 'theme/simple/ui/content', content_key: 'app.content' %}
zx Dec 15 2020 at 11:24

Alternative way I found out might work is to check if the passed string starts with an translation key (so app. in my example) and if so - use filter to get the translation. This is a little easier to use as one doesn't have to think about which argument to use, but I am not sure if it's a bullet-proof solution.

{% liquid
  assign is_translation = content | slice: 0, 4

  if is_translation == 'app.'
    assign content = content | t
  else
    assign content = content
  endif
%}
  • zx Dec 17 2020 at 13:51
    I just wanted to note that this is in fact risky as app. might not always be the key name or one can have many keys.
Please sign in or fill up your profile to answer a question