Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django: variable translation
#1
I'm using Django to develop a project. I can internationalize the templates easily.

But I have to translate a field in a template to represent the gender of people. This gender is in a database so I show in a template as {{person.gender}}.

How can I translate the value of the gender in the template,so, if I get 'male' from the database, the template display 'Hombre' for Spanish browsers?
--
Adrián E. Córdoba
Reply
#2
I don't have django, never used it, prefer PHP, but the principle would be like this:

from django.http import HttpResponse
from django.template import loader

html = '/home/peterr/temp/html/template.html'

def testing(request, language):
  template = loader.get_template(html)
  context = {
    'firstname': 'Linus',
    'gender': 'male',
     }
  if language == 'castellano':
      context['gender'] == 'macho'
  return HttpResponse(template.render(context, request))

# define context without the function for demonstration purposes
context = {'firstname': 'Linus', 'gender': 'male'}
# get the required language from somewhere
language = 'castellano' # from ip analysis?? or user request??
if context['gender'] == 'male' and language== 'castellano':
      context['gender'] = 'macho'

with open(html) as t:
    webpage = t.read()
    webpage = webpage.replace('firstname',  context['firstname']).replace('gender', context['gender' ])
print(webpage)
Gives:

Output:
<!DOCTYPE html> <html> <body> <h1>Hello {{ Linus }}, how are you?</h1> <h1>Eres un verdadero {{ macho }} ¿verdad?</h1> </body> </html>
Reply
#3
(Apr-10-2026, 03:16 AM)Pedroski55 Wrote: I don't have django, never used it, prefer PHP, but the principle would be like this:

from django.http import HttpResponse
from django.template import loader

html = '/home/peterr/temp/html/template.html'

def testing(request, language):
  template = loader.get_template(html)
  context = {
    'firstname': 'Linus',
    'gender': 'male',
     }
  if language == 'castellano':
      context['gender'] == 'macho'
  return HttpResponse(template.render(context, request))

# define context without the function for demonstration purposes
context = {'firstname': 'Linus', 'gender': 'male'}
# get the required language from somewhere
language = 'castellano' # from ip analysis?? or user request??
if context['gender'] == 'male' and language== 'castellano':
      context['gender'] = 'macho'

with open(html) as t:
    webpage = t.read()
    webpage = webpage.replace('firstname',  context['firstname']).replace('gender', context['gender' ])
print(webpage)
Gives:

Output:
<!DOCTYPE html> <html> <body> <h1>Hello {{ Linus }}, how are you?</h1> <h1>Eres un verdadero {{ macho }} ¿verdad?</h1> </body> </html>

Pedroski55:
The gender is coming from a database. I think there must be a much simpler logic.
Thanks anyway.
--
Adrián E. Córdoba
Reply
#4
Nice question threads like this are exactly why forums like Python Forum are so helpful for learning and troubleshooting real-world coding issues.

From what I can tell, this looks like a common situation where breaking the logic into smaller steps and testing each part individually can really help identify the issue faster. Adding print/debug statements or checking variable values step-by-step usually makes things much clearer.
Reply
#5
Hello,

an answer can be found here: https://stackoverflow.com/questions/5810...anslations.

In case this doesn't work for you, please provides more context and real-world examples showing the actual translation problem.

Regards, noisefloor
Reply
#6
(Apr-11-2026, 01:49 PM)noisefloor Wrote: Hello,

an answer can be found here: https://stackoverflow.com/questions/5810...anslations.

In case this doesn't work for you, please provides more context and real-world examples showing the actual translation problem.

Regards, noisefloor
In that case, the gender is hard-coded in script.
In my case, the value of gender is in database.
Thank you, anyway.
--
Adrián E. Córdoba
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  DJANGO Looping Through Context Variable with specific data Taz 0 3,070 Feb-18-2021, 03:52 PM
Last Post: Taz
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 6,185 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django translation 2 languages( Deutsch and Austrian) sonic911 2 4,347 Jan-16-2018, 01:34 PM
Last Post: sonic911

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020