Dynamic keys in a dictionary

Hi we have a client that wants a custom language switcher and they want to be able to personaly adjust images for each Language. I belive the best way to do this would be to have a repeater module set up and have it translate itself into a dictionary, here is my code so far:

{% set language_data = {} %}
{% for i in module.languages %}
 {% set language_code = i.code|lower %}
 {{ language_code }}
 {% set language_name = i.full_name|capitalize %}
 {% set language_image = i.image.src %}
 {% set language_info = { "name": language_name, "image": language_image } %}
 {% set language_data = language_data.update({language_code: language_info}) %}
{% endfor %}

But by the looks of the output it isn’t able to set the language_code as a dynamic key. Is there any solution for this or would be easier to write a dictionary of almost every language as just have it loop through and assign images based on the repeater module

Hey @AKoprivec

You can use the put function.

{% do mydictionary.put(key, value) %}

Try this out:

{% set language_data = {} %}
{% for i in module.languages %}
 {% set language_code = i.code|lower %}
 {{ language_code }}
 {% set language_name = i.full_name|capitalize %}
 {% set language_image = i.image_field.src %}
 {% set language_info = { "name": language_name, "image": language_image } %}
 {% do language_data.put(language_code, language_info) %}
{% endfor %}

And a reference for future use.

Hope this gets you moving.

Yes i tried that but it didn’t even let me publish that code at first, adding an if statment checking if i.code existed first did the trick and then it worked, wierd since the language_code variable was populated just fine. I saw the reference post but wanted to make a seperate thread since for some reason it didn’t want to work.
Thank you for your help!