Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

345
Visualizações
How to translate text in an external javascript file? (Django)

I have a JavaScript file that appends elements to the body according to the user interaction.

Right now in my index.html template I'm declaring global variables with the translated text:

 {% block main %}
      <script>
           let TRANSLATION = {% trans "my text" %}
      </script>
 {% endblock %}

So after Django translates the text in my index template, my JavaScript file can take the translated text and append it using something like:

element.innerHTML = TRANSLATION;

I know this is a very dirty way of translating the text that JavaScript will use because some users won't need that text and in those cases I'll be wasting resources with variables that I won't be using.

So the question is: What is the clean and correct way to translate text for external JavaScript files?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Most of the work will be done by django. Follow their documentation: https://docs.djangoproject.com/en/4.0/topics/i18n/translation/#internationalization-in-javascript-code

A little further down on the page is also documented how to generate the language file for your javascript code. You will have two language files later, one for your django application and one for your javascript code.

https://docs.djangoproject.com/en/4.0/topics/i18n/translation/#creating-message-files-from-javascript-source-code

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda