Problem/Motivation

Responses that contain translated interface strings (with t()) require the languages:language_interface cache context to correctly vary based on the current language.
This cache context is usually added by the rendering system so developers don't have to think about adding it explicitly.

But when returning a custom JSON response (without any rendering), you have to explicitly add the cache context:

$response = new CacheableAjaxResponse(['title' => t('foo')]);

IMHO this is bad DX and easy to forget.

Steps to reproduce

Proposed resolution

Maybe we could have something similar to EarlyRenderingControllerWrapperSubscriber that triggers an exception when using translated strings in a cacheable response that does not have the correct cache context.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

prudloff created an issue.