Search code examples
javascripthtmldjangotranslation

How I can translate text on buttons in Django?


I try to make bilingual site on Django.

Frontend wasn't written by me, and, unfortunatelly, I cannot ask this question for person who did it.

const configButton = {
    purchases: {
        attr: 'data-out',
        default: {
            class: 'button_style_blue',
            text: '<span class="icon-plus button__icon"></span>Add to purchase list'
        },
        active: {
            class: 'button_style_light-blue-outline',
            text: '<span class="icon-check button__icon"></span>Recipe in purchase list'
        }
    }
}

How I can add translation of text on the button using Django internalization tools?


Solution

  • In your file import

    from django.utils.translation import gettext as _
    

    then whichever texts you wants to translate make it in a variable, example in your case -

    addToList = _("Add to purchase list")
    reciepeInList = _("Recipe in purchase list")
    
    const configButton = {
        purchases: {
            attr: 'data-out',
            default: {
                class: 'button_style_blue',
                text: '<span class="icon-plus button__icon"></span>' + addToList
            },
            active: {
                class: 'button_style_light-blue-outline',
                text: '<span class="icon-check button__icon"></span>' + reciepeInList
            }
        }
    }