简体   繁体   English

Django REST Framework 翻译不起作用

[英]Django REST Framework translation does not work

I have a problem with translating custom exception in Django rest, the problem is when I change the LANGUAGE_CODE in the settings, every things work fine and i get correct translation for both of my languages, but when I tried to use Accept-Language in the header to change the translation, it does not work as it should!我在 Django rest 中翻译自定义异常时遇到问题,问题是当我在设置中更改 LANGUAGE_CODE 时,一切正常,我得到了两种语言的正确翻译,但是当我尝试在标题更改翻译,它不应该工作! my settings is:我的设置是:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'core.middleware.auth_middleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'core.middleware.dates_middleware',
    'core.middleware.translation_middleware',
]
#LANGUAGE_CODE = 'fa-ir'
LANGUAGE_CODE = 'en-us'

LANGUAGES = [
    ('fa', _('Farsi')),
    ('en', _('English')),
]

TIME_ZONE = 'UTC'


USE_I18N = True

USE_L10N = True

my exception:我的例外:

from rest_framework.exceptions import APIException
from django.utils.translation import ugettext as _
class WrongUsernamePassword(APIException):
    status_code = 401
    default_detail = _('username or password is wrong')
    default_code = '401'

my view:我的看法:

class UserViewSet(viewsets.ModelViewSet):
    ...

    @list_route(methods=['POST'], permission_classes=[AllowAny])
    def app_login(self, request):
        lang = translation.get_language() # here the value is fa
        raise WrongUsernamePassword

when I send the request with this header (Accept-Language=fa-ir) I can see that the lang variable value is fa so framework know that I change the language but the response is still in english:当我发送带有此标头的请求(Accept-Language=fa-ir)时,我可以看到 lang 变量值为 fa,因此框架知道我更改了语言,但响应仍为英文:

{
    "detail": "username or password is wrong"
}

any idea?任何的想法?

Today I dealt with the same problem.今天我处理了同样的问题。 I found this post and saw that it was not solved.我找到了这个帖子,看到它没有解决。

It occurs because those error messages are defined in a class variable, therefore, they are defined when the server is loaded.这是因为这些错误消息是在类变量中定义的,因此,它们是在加载服务器时定义的。 So, at that moment, there is not an Accept-Language in a header because there is not a request.因此,此时,由于没有请求,因此标头中没有Accept-Language

To solve that problem, I used gettext_lazy instead of gettext because that function translates the string in a lazy fashion.为了解决这个问题,我使用了gettext_lazy而不是gettext因为该函数以惰性方式转换字符串。 You should use gettext_lazy.您应该使用 gettext_lazy。 In ugettext() vs. ugettext_lazy() there is a very good explanation about ugettext vs ugettext_lazy which made me understand the problem:ugettext() vs. ugettext_lazy() 中有一个关于ugettext vs ugettext_lazy很好的解释,这让我理解了这个问题:

For your code sample:对于您的代码示例:

from rest_framework.exceptions import APIException
from django.utils.translation import ugettext_lazy as _
class WrongUsernamePassword(APIException):
    status_code = 401
    default_detail = _('username or password is wrong')
    default_code = '401'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM