Respect user language preference in content endpoint (#15485)

* Respect user language preference in content endpoint

Content API now returns data in user's preferred language when authenticated without language parameter. No breaking changes - existing clients unaffected.

* lint fix
This commit is contained in:
Fiz
2025-08-21 16:44:15 -05:00
committed by Kalista Payne
parent 9c92bf73f5
commit fb730942a0
2 changed files with 35 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import nconf from 'nconf';
import { langCodes } from '../../libs/i18n';
import { serveContent } from '../../libs/content';
import { authWithHeaders } from '../../middlewares/auth';
const IS_PROD = nconf.get('IS_PROD');
@@ -66,12 +67,21 @@ api.getContent = {
method: 'GET',
url: '/content',
noLanguage: true,
middlewares: [authWithHeaders({ optional: true })],
async handler (req, res) {
let language = 'en';
const proposedLang = req.query.language;
if (proposedLang && langCodes.includes(proposedLang)) {
language = proposedLang;
} else if (res.locals.user
&& res.locals.user.preferences
&& res.locals.user.preferences.language
) {
const userLang = res.locals.user.preferences.language;
if (langCodes.includes(userLang)) {
language = userLang;
}
}
let filter = req.query.filter || '';