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 7694415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:18:57+00:00 2026-05-31T21:18:57+00:00

I’ve made a few Badge classes in Django, each containing some sort of description

  • 0

I’ve made a few Badge classes in Django, each containing some sort of description in a string variable:

"You get this badge because you've runned %d meters in %d minutes"
"You get this badge because you've killed %d monsters of the type %s"

etc. And the classes also have a function get_description(badge_level_requirements), so in the templates it will be called together with a list to assemble the string for a specific user:

class RunnerBadge(Badge):
    des=ugettext_lazy("You get this badge because you've runned %d meters in %d minutes")
    def get_description(cls,badge_level_requirements):
        return cls.des%badge_level_requirements

And I’ve stored the requirements lists in the database without any argument names already 🙁 As shown in the examples, different classes have different numbers of values to fill in the string, the values mean different things as well. So I can’t really name the arguments.

However, if I want to internationalize these strings, there’ll be errors: 'msgid' format string with unnamed arguments cannot be properly localized And the language file cannot be generated for this matter.

Is there a way to bypass this error?

Update

I’ve come across this method for bypassing the error without changing database.
In database, the level requirements are stored in a text field in the format of dict:

#Requirment of Runner's badge
"{'gold':(100,10),'silver':(50,5),'bronze':(25,2)}"

And in class definition, mannually add argument names as ‘arg_0’,’arg_1’… to descriptions. The get_description method is changed to pre-process the data before used for filling description strings.

class RunnersBadge(Badge):
    requirements=#get the previous dict from database
    description="You get this badge because you've runned %(arg_0)d meters in %(arg_1)d minutes"

    @classmethod
    def get_description(cls,level):
        '''
        This is actually a method of parent class Badge
        level is either 'gold','silver' or 'bronze'
        '''
        dic={}
        try:
            for (num,val) in enumerate(cls.requirements[level]):
                dic['arg_'+str(num)]=val
        except TypeError:
            dic['arg_0']=cls.requirements[level]


        return cls.description%dic

This method keeps most of the current structure (logic and database). And the translator just need to take care of the words’ placement.

  • 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-31T21:18:58+00:00Added an answer on May 31, 2026 at 9:18 pm
    1. like in code, variable names should be meaningful within their context, ‘meter_count’ and ‘minute_count’ are explicit, compared to ‘arg_0’ and ‘arg_1’ which are meaningless

    2. use standard translation in python code, it is less error prone and is recognized by the amazingly useful makemessages command

    3. use use named-string interpolation (e.g., %(day)s) instead of positional interpolation (e.g., %s or %d) whenever you have more than a single parameter, because the order of parameters can change depending on the language. I.e. germanic and latin languages inverse the noun/adjective order, dates are displayed differently depending on the language, etc, etc …

    4. use ran instead of runned, double check the syntactical validity of your english translation strings

    This:

    class RunnersBadge(Badge):
        requirements=#get the previous dict from database
        description="You get this badge because you've runned %(arg_0)d meters in %(arg_1)d minutes"
    

    Becomes:

    from django.utils.translation import ugettext as _
    
    class RunnersBadge(Badge):
        requirements=#get the previous dict from database
        description=_("You get this badge because you've ran %(meter_count)d meters in %(minute_count)d minutes")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has

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.