Django:尽管选择了语言,表单仍然依赖 LANGUAGE_CODE 来格式化日期时间(包括演示) [英] Django: despite selected language, Forms still rely on LANGUAGE_CODE to format datetime (demo included)

查看:5
本文介绍了Django:尽管选择了语言,表单仍然依赖 LANGUAGE_CODE 来格式化日期时间(包括演示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小演示来展示这个问题.

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.

解决此问题的唯一方法是在 settings.py 中将 LANGUAGE_CODE = 'en-us' 更改为 LANGUAGE_CODE = 'en-gb'.这种方法显然是无用的,因为它不再是动态的,并且有利于一个群体而不是另一个群体.这应该是最后一个优先级,因为所选语言应该具有更高的优先级.

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.

我创建了自定义 formats.py 来覆盖 enen_GB 的日期和时间格式,如 文档 所以我不知道我还能做什么.

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.

请从 我的保管箱:您所要做的就是编辑 settings.py 并调整 sqlite.db 的路径.

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.

我是否忽略了什么或者这是一个 Django 错误?

Have I overlooked something or is this a Django bug?

推荐答案

已修复!!嗨 Kave,经过一段时间的调查,我终于找到了解决方案.

Fixed!! Hi Kave, after some time looking into your problem, finally I have found a solution.

  • 首先,您应该使用 activate(language_code) 切换到新语言.
  • 您必须同时设置为本地化:字段和小部件:

示例切换语言:

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:尽管选择了语言,表单仍然依赖 LANGUAGE_CODE 来格式化日期时间(包括演示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆