问题描述
我已经创建了一个小型演示来显示问题。
当您点击英国英语时,您可以看到日期和时间格式如何相应改变,这是非常好的。
现在,如果您单击添加,您将看到如何填充当前的日期和时间。然而,他们仍然携带美国的日期格式,而不是选定的英国语言。
解决此问题的唯一方法是更改 LANGUAGE_CODE ='en -us'
到 LANGUAGE_CODE ='en-gb'
在settings.py中。这种做法显然是无用的,因为它不再是动态的,有利于一个群体。这应该是最优先的,因为所选择的语言应该有更高的优先级。
我已经创建了自定义的 formats.py
以覆盖 en
和 en_GB
的日期和时间格式,如,所以我无所事事,我还能做些什么。 >
请妥善下载我的演示(22 kb)从:
您只需编辑settings.py并调整sqlite.db的路径。
我忽略了一些东西还是Django的错误?
修正了!嗨Kave,经过一段时间研究你的问题,最后我找到了一个解决方案。
- 首先,你应该使用activate(language_code
- 您必须设置为本地化:字段和小部件:
示例开关语言:
def display_current_language(request):
if request.LANGUAGE_CODE =='en-gb ':
lang =你更喜欢阅读英国英语{code}。格式(
code = request.LANGUAGE_CODE)
activate(request.LANGUAGE_CODE)
elif请求。 LANGUAGE_CODE =='en-us':
lang =您更喜欢阅读美国英语{code}。格式(
code = request.LANGUAGE_CODE)
activate('en-us ')
else:
lang =您更喜欢阅读Deutsch {code}格式(
code = request.LANGUAGE_CODE)
activate(request.LANGUAGE_CODE)
return lang
使用您的模型(公司)和您的字段(日期) / p>
class CompanyForm(ModelForm):
class Meta:
model = Company
def __init __(self,* args,** kwargs):
super(CompanyForm,self).__ init __(* args,** kwargs)
self.fields ['date']。localize = True
self.fields ['date']。widget.is_localized = True
I have created a small demo to show the problem.
When you click on British English, you can see how both the date- and time format change accordingly, which is great.
Now if you click on Add, you will see how both the current date and time are populated for you. However they still carry the American date format, instead of the selected British language.
The only way to fix this is to change LANGUAGE_CODE = 'en-us'
to LANGUAGE_CODE = 'en-gb'
in settings.py. This approach would be obviously useless as its no longer dynamic and favors one group over the other. This should be the last priority since the selected language should have a higher priority.
I have created custom formats.py
to override the date and time formats for en
and en_GB
as described in the documentation so I am clueless what else I could do.
Please be so kind and download my demo (22 kb) from my dropbox: All you have to do is to edit settings.py and adjust the path to sqlite.db.
Have I overlooked something or is this a Django bug?
Fixed!! Hi Kave, after some time looking into your problem, finally I have found a solution.
- First of all, you should use activate( language_code ) to switch to new language.
- You must set as localized both: field and widget:
Sample switch language:
def display_current_language(request):
if request.LANGUAGE_CODE == 'en-gb':
lang = "You prefer to read British English {code}.".format(
code=request.LANGUAGE_CODE )
activate(request.LANGUAGE_CODE)
elif request.LANGUAGE_CODE == 'en-us':
lang = "You prefer to read American English {code}.".format(
code=request.LANGUAGE_CODE )
activate('en-us')
else:
lang = "You prefer to read Deutsch {code}.".format(
code=request.LANGUAGE_CODE )
activate(request.LANGUAGE_CODE)
return lang
Sample using your model (company) and your field ( date ):
class CompanyForm(ModelForm):
class Meta:
model = Company
def __init__(self, *args, **kwargs):
super(CompanyForm, self).__init__(*args, **kwargs)
self.fields['date'].localize = True
self.fields['date'].widget.is_localized = True
这篇关于Django:尽管选择语言,Forms仍然依赖LANGUAGE_CODE来格式化datetime(包含演示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!