Language switch in django form

Hi,

I am building a form. Here is the code.
.
.

class UserRegistarForm(UserCreationForm):
    Select = "--"
    ENGLISH = 'EN'
    FRENCH = 'FR'
    ARABIC = 'AR'
    FARCHI = 'FA'
    YEAR_IN_SCHOOL_CHOICES = [
        (Select, 'Select'),
        (ENGLISH, 'English'),
        (FRENCH, 'French'),
        (ARABIC, 'عربي'),
        (FARCHI, 'فارسي'),
    ]
    email =forms.EmailField()
    language=forms.ChoiceField(choices=YEAR_IN_SCHOOL_CHOICES)

    class Meta:
        model = User
        fields = ['language', 'username', 'email', 'password1', 'password2']

.
.
Here is the layout of the form,


.
.
I would like to change the language of the form based on the user selection. If a user selects Arabic, the form should be in “RTL” directed and in “Arabic”. How I may achieve this?

Any help or suggestion would be much appreciated! Thanks.

If you’re talking about changing the form that is currently being displayed without making a page refresh, that would require JavaScript / AJAX.
The JavaScript code would issue a request for the new form from the server and inject it into the current page.
(Note - this does not necessary mean you would need to write the JavaScript code yourself. This is an ideal situation for HTMX. You could also do this using jQuery or any other JavaScript-framework-of-your-choice.)

If you don’t mind doing a full-page refresh, you still need JavaScript. However, what this JavaScript would do would be to redirect you to this page with the new language setting.

Thanks for your help.

It’s not a requirement here not to refresh the page. If I prefer to change the form with a page refresh, what would be the best approach here? May I get your suggestion here, please?

Note: the language preference of the user will be saved in the database, and the future visit or landing to the next screen by that user will be on that preferred language. Every time the user will log in, the app will be shown in that user’s selected language.

Then you need some JavaScript to listen for the On-change event of the selection box. When that box is changed, it should issue a request to the server with the new language as a parameter.
The view that handles that request would save that parameter in the user’s profile (or some other similarly appropriate location), and return a redirect to that login page.

However, I have no idea how you plan to show the login page in the desired language automatically if the user isn’t yet logged in. When someone goes to that page, how is the system supposed to know which language to present?

Then you need some JavaScript to listen for the On-change event of the selection box. When that box is changed, it should issue a request to the server with the new language as a parameter.
The view that handles that request would save that parameter in the user’s profile (or some other similarly appropriate location), and return a redirect to that login page.

If I’m getting it correct for each language, there should be a registration page? Like, for English there is one, for Arabic, there is one, So, when the user will select a language, we will redirect to that registration page. Is it something like this?

However, I have no idea how you plan to show the login page in the desired language automatically if the user isn’t yet logged in. When someone goes to that page, how is the system supposed to know which language to present?

For now, my plan is to store a parameter in Browser local storage for the selected language. Each time the user will go to the Login page, in Javascript system will check the local storage variable, if it’s there, the system will use it. If it isn’t there (maybe the user has flashed the browser or a new browser), then a popup will be on the login page to get the user’s language.

Thanks again for your kind assistance.

My understanding is that no - you don’t need a separate page for each language.

I believe that you can use Django’s translation features to alter the text on a page to match language selections.

See Translation | Django documentation | Django

(Someone with more knowledge of this would have to chime in with specific details, this is an area I’ve not done anything with.)