Dynamically serving static images in Django e.g. based on language in templates

I am looking for a way to serve static files in templates based on language.

Lets assume I am having three files called de.png, en.png and fr.png.

What I want to achieve is something like:

 {% get_current_language as LANGUAGE_CODE %}
 {% static LANGUAGE_CODE+'.png' %}

Any suggestions?

Fixed this myself by writing a template tag myself. Not elegant, but a solution.

from django.templatetags.static import static
from django import template
register = template.Library()


@register.simple_tag(name='static_language')
def static_language(language: str) -> str:
    """
    Returns the correct button based on language
    :param file takes a filename and returns the static
    :return: static url for an image
    """
    result = static('images/'+language+'.png')
    return result
Back to Top