根据用户动态设置或更改默认语言-Django [英] Set or change the default language dynamically according to the user - Django
问题描述
是否可以通过单击按钮(发送请求)动态更改settings.py中 LANGUAGE_CODE
变量的值?
Is there any way to change the value of LANGUAGE_CODE
variable in settings.py dynamically on clicking a button (sending a request)?
我希望用户为他们的帐户设置自己的默认语言".
I want the user to have their own "default language" set for their account.
现在,用户可以使用下拉列表选择他们喜欢的语言,并且网站得到了完美的翻译,并且由于Django选择了浏览器的语言,因此用户无需在重新打开网站后重新选择他们的语言从同一浏览器.
Right now the user can select their preferred language using a drop-down list, and the website gets translated perfectly, and because Django picks up the browser's language, the user need not to re-select their language after they reopen the website from the same browser.
但是当他们从其他浏览器打开网站 时,由于 LANGUAGE_CODE
变量设置为 en-us <,因此默认语言再次为英语"/code>在settings.py中.
But when they open the website from a different browser, the default language is again "English" because of the LANGUAGE_CODE
variable set to en-us
in settings.py.
所以我想做的是让每个用户都有一个选项来选择他们想要的语言作为他们的默认语言.我想通过创建另一个(类似)下拉菜单并要求用户选择他们想要的语言作为默认"并点击 save
按钮,然后在保存时更改 LANGUAGE_CODE
的值设置为用户选择的值(即动态更改).但是我不知道如何动态更改 LANGUAGE_CODE
的值.
So what I want to do is make each user have an option to select their desired language as default for them. I want to do this by making an another(similar) drop-down and ask user to select a language they want as "default" and hit the save
button, and on saving, I want to change LANGUAGE_CODE
's value to the one selected by the user (i.e change it dynamically). But I don't know how to change the value of LANGUAGE_CODE
dynamically.
此外,这种方法还有一个问题.假设即使我能够动态更改此 LANGUAGE_CODE
变量,它也将使网站将所选语言作为所有用户的默认语言,而不仅仅是该特定用户根据Django的文档,谁更改了它:
Also, there is one more problem with this approach. Say even if I was able to change this LANGUAGE_CODE
variable dynamically, it would make the website have the selected language as default for ALL THE USERS and not only for that one specific user who changed it, according to Django's documentation:
LANGUAGE_CODE:
LANGUAGE_CODE:
- 如果未使用语言环境中间件,它将决定为所有用户提供哪种翻译.
我做了很多研究,但找不到适合我的解决方案.我对国际化很陌生.请帮忙.
I researched a lot, but couldn't find a solution for me. I am very new to Internationalization. Please help.
推荐答案
当然有,这就是翻译的重点.您已经想到默认"语言了,这让您感到困惑.根据定义,他们选择的东西不是默认值.
Of course there is, that is the whole point of translations. You've confused yourself by thinking of the "default" language; something that they've chosen is not the default, by definition.
The Django docs explain how to set the active language for a user explicitly. You can set this on save exactly as you describe.
您可能还想通过再次从用户个人资料中传递值来重新设置登录时的值.
You probably also want to re-set this value on login, again by passing the value from the user's profile.
这篇关于根据用户动态设置或更改默认语言-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!