django-parler-rest icon indicating copy to clipboard operation
django-parler-rest copied to clipboard

Use request.LANGUAGE_CODE in API

Open georgedorn opened this issue 7 years ago • 1 comments

I'm writing an API that needs to return a field localized for the request.

I'm using Django's LocaleMiddleware, which negotiates the HTTP Accept-language header against settings.LANGUAGE_CODES and sets the result as request.LANGUAGE_CODE. Is there a pattern or mixin I should be using on my ViewSets to activate that language for the response?

georgedorn avatar Dec 22 '17 01:12 georgedorn

Not that I'm aware of.. Django's LocaleMiddleware also calls translation.activate() so the current Django language will be set. django-parler uses django.utils.translation.get_language() by default when objects are fetched from the database.

You can set the language explicitly off course, using Model.objects.language(..)

vdboor avatar Jan 02 '18 12:01 vdboor