Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6136309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:35:15+00:00 2026-05-23T17:35:15+00:00

I am using .mo files for localization in Django. Also, in my database, I

  • 0

I am using .mo files for localization in Django.
Also, in my database, I store some translated text in different fields, such as:

name_en, name_es, name_de (they are all columns in each row).

What will be the best method to choose the correct field inside a template?

i.e.:
{{ name.some_method }} will generate the correct translation based on the current localization.

Thanks,

Meit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:35:15+00:00Added an answer on May 23, 2026 at 5:35 pm

    I can see two method for doing this, one in your view and the other one is in the template…

    1. In view:

    Probably you keep the user language information somewhere so,

    user_lang = 'es'
    obj = Somemodel.objects.get(pk=123434)
    obj.local_name = getattr(obj, 'name_%s'%user_lang)
    

    So, you keep local translation in a specific variable of the instance and in your template you can use is as:

    {{obj.local_name}}
    

    But that might be costly if you wish to pass the template a queryset instead of a single instance. For a such usege you have to evaluate that value for each object in your queryset.

    1. In template:

    That is a more complex way of solving the porblem in the template…

    Define a template tag and pass object_id, and local language information and get the translated text using a similar getattr function. But in that point, if you wish to use this for more than one model, you probably have to pass a content type information for your template tag too, such as:

    {% get_translation <object_id> <content_type_id> <local_language> %}
    

    And in your template tag function, do something like:

    from django.contrib.contenttypes.models import ContentType
    
    ....
    cont_obj = Content_type.objects.get_for_id(<cotent_type_id>) #get the related model 
    obj = cont_obj.get_object_for_this_type(pk=<object_id>) # get your object 
    return getattr(obj, 'name_%s'%<local_language>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Localization/Globalization is pretty easy in Visual Studio using RESX files. I just add a
I recently figured out how to do localization in MVC using resource files containing
I use i18n for localization like django does using a custom request handler for
In my application I store on every machine some files in an application folder.
in some projects (web application projects) I'm using global resource files. (Stored in the
For localization I'm using the Resource-file (.resx files) functionality in .NET, but I'm wondering
I'm using Locbaml to generate localization files in my WPF application. Yesterday, I've upgraded
I'm working on a quite simple Webpage (MVC2), using localisation based on Resource Files.
When using PNG files (made with Paint.NET) as background images on my web site,
I have heard using PDB files can help diagnose where a crash occurred. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.