Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Translation of states #119

Closed
khlumzeemee opened this issue Feb 23, 2016 · 10 comments
Closed

Translation of states #119

khlumzeemee opened this issue Feb 23, 2016 · 10 comments

Comments

@khlumzeemee
Copy link

Hi,
I'm having a hard time trying to make the different states available for translation.
Code:

    @transition(field=state, source='new', target='accepted')
    def accept(self):

I tried with something like

source=_('new') 

but that's not the desired effect, because a different value will be saved to database depending on the user language. I just want to modify the label as a lazy translation on display.

Could I use the custom dictionary for this?

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Feb 24, 2016

You should translate choices attr on a field, or directly translate field value in template.

https://docs.djangoproject.com/es/1.9/ref/models/fields/#choices
https://docs.djangoproject.com/es/1.9/topics/i18n/translation/#trans-template-tag

@kmmbvnr kmmbvnr closed this as completed Feb 24, 2016
@khlumzeemee
Copy link
Author

Hi
I went with your first option, created a variable for each choice, then created a list of tuples, then referenced the variables in the transitions

However this does not reflect in the admin panel, the state is still displayed as "new" (the key) where I was expecting "New" (the value)

    #States are listed as choices to be translatable
    NEW = 'new'
    ACCEPTED = 'accepted'
    AWAITING_REVIEW = 'awaiting_review'
    REVIEWED = 'reviewed'
    COMPLETED = 'completed'
    CANCELLED = 'cancelled'
    REJECTED = 'rejected'

    STATES = (
        (NEW, _('New')),
        (ACCEPTED, _('Accepted')),
        (AWAITING_REVIEW, _('Awaiting Review')),
        (REVIEWED, _('Reviewed')),
        (COMPLETED, _('Completed')),
        (CANCELLED, _('Cancelled')),
        (REJECTED, _('Rejected')),
    )

    state = FSMField(_('state'), default=NEW, protected=True)

@khlumzeemee
Copy link
Author

I just realized maybe I should move this issue to the fsm-admin project

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Feb 24, 2016

Yep, if you see non translated button in admin, this is django-fsm-admin issue.

Probably, you can try to override fsm_submit_button.html template in your own project, with translated input value.

- <input type="submit" value="{{ button_value }}" ...
+ <input type="submit" value="{% trans button_value %}" ...

@khlumzeemee
Copy link
Author

Hi
I discussed the topic in the fsm-admin project, I got my answer for the buttons but not the the FSMField display itself.

I have a form (in the admin or in another custom template) but the value displayed for the FSMField is the key, not the value. It is not admin related I think.

What am I missing?

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Feb 26, 2016

Any state machine consist of STATE and EVENTS. In django-fsm implementation, STATES are some constant values used for the field. EVENTS are model methods. You need to translate not the STATE but the EVENT names, and django-fsm-admin gives you ability to specify button_name

@khlumzeemee
Copy link
Author

What if I want to display the current state label of the object in the user's preferred language? This is a variable, I cannot use {%trans model.state %} in the template...

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Feb 26, 2016

Actually you can use {%trans model.state %} trans tag works for variables also. This is noted in django docs!

Btw for the choices as the admin readonly field, it is the django-admin side restriction. They newer calls get_FIELD_choices

https://github.com/django/django/blob/master/django/contrib/admin/utils.py#L296

@ashwoods
Copy link
Contributor

ashwoods commented Mar 1, 2016

or use a translated string in the transition custom properties.

@khlumzeemee
Copy link
Author

I solved it by using the choices property FSMField inherits from the CharField ! :)
state = FSMField(_('state'), default=NEW, protected=True, choices=STATES)
And then using the get_state_display method in the template did the trick!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants