#34268 closed New feature (invalid)

Internationalization does not localize functional model choices

Reported by: Nima Owned by: nobody
Component: Internationalization Version: 4.2
Severity: Normal Keywords: Functional choice generation
Cc: Claude Paroz Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given a lazy/functional choice generator for a model, Django's internationalization cannot fully comprehend the lazy localizable text generated by the function.

Imagine a base class:

# gettext_lazy also does not work
from django.utils.translation import gettext as _

class SelectableModelField:
    @classmethod
    def make_choices(cls):
        return [(value, _(key.replace('_', ' '))) for (key, value) in vars(cls).items() 
                if not (key[-1] + key[0]).__contains__('_')]

    bool_choices = ((True, 'Yes'), (False, 'No'))

which is inherited by:

class User(models.Model):
    class __UserType__(SelectableModelField):
        basic = 0
        premium = 1
    
    user_type = models.PositiveSmallIntegerField(choices=__UserType__.make_choices())

the django.po file will not contain the functionally-generated choices after running python manage.py makemessages -l ...

Change History (3)

comment:1 by Nima, 17 months ago

Type: Cleanup/optimizationUncategorized

comment:2 by Mariusz Felisiak, 17 months ago

Severity: Release blockerNormal

comment:3 by Mariusz Felisiak, 17 months ago

Cc: Claude Paroz added
Resolution: invalid
Status: newclosed
Type: UncategorizedNew feature

Thanks for this ticket, however I don't think it's feasible (not only in Django but in general). You can only translate a fixed list of strings.

Note: See TracTickets for help on using tickets.
Back to Top