Implementing core of setting user languages and fetching language specific tasks

This commit is contained in:
Keith Stevens
2023-01-21 21:36:37 +09:00
parent 5f3f0776e9
commit 7274512c2f
10 changed files with 125 additions and 8 deletions
+15 -1
View File
@@ -1,6 +1,20 @@
import parser from "accept-language-parser";
import type { NextApiRequest } from "next";
import prisma from "src/lib/prismadb";
import type { BackendUserCore } from "src/types/Users";
const getUserLanguage = (req: NextApiRequest) => {
const cookieLanguage = req.cookies["NEXT_LOCALE"];
if (cookieLanguage) {
return cookieLanguage;
}
const headerLanguages = parser.parse(req.headers["accept-language"]);
if (headerLanguages.length > 0) {
return headerLanguages[0].code;
}
return "en";
};
/**
* Returns a `BackendUserCore` that can be used for interacting with the Backend service.
*
@@ -35,4 +49,4 @@ const getBackendUserCore = async (id: string) => {
} as BackendUserCore;
};
export { getBackendUserCore };
export { getBackendUserCore, getUserLanguage };