forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguages.ts
38 lines (32 loc) · 1.07 KB
/
languages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextApiRequest } from "next";
import { OasstError } from "./oasst_api_client";
const missingDisplayNamesForLocales = {
eu: "Euskara",
gl: "Galego",
};
/**
* Returns the locale's name.
*/
export const getLocaleDisplayName = (locale: string, displayLocale?: string) => {
// Intl defaults to English for locales that are not officially translated
if (missingDisplayNamesForLocales[locale]) {
return missingDisplayNamesForLocales[locale];
}
const displayName = new Intl.DisplayNames([displayLocale || locale], { type: "language" }).of(locale)!;
// Return the Titlecased version of the language name.
return displayName.charAt(0).toLocaleUpperCase() + displayName.slice(1);
};
export const getLanguageFromRequest = (req: NextApiRequest) => {
const body = req.method === "GET" ? req.query : req.body;
const lang = body["lang"];
if (!lang || typeof lang !== "string") {
throw new OasstError({
message: "Invalid language",
httpStatusCode: -1,
errorCode: -1,
path: req.url!,
method: req.method!,
});
}
return lang;
};