feat: localization (#196)

* Started adding locale translations :)

* Added some more translations

* Working on translating even more pages

* More translations

* Added test default locale retrieval

* replace `intl.formatMessage` with custom `t` hook

* add more translations

* improve title syntax

* add more translations

* translate admin config page

* translated error messages

* add language selecter

* minor fixes

* improve language handling

* add upcoming languages

* add `crowdin.yml`

* run formatter

---------

Co-authored-by: Steve Tautonico <stautonico@gmail.com>
This commit is contained in:
Elias Schneider
2023-07-20 15:32:07 +02:00
committed by GitHub
parent 7c5ec8d0ea
commit b9f6e3bd08
68 changed files with 4712 additions and 461 deletions

View File

@@ -0,0 +1,42 @@
import { setCookie } from "cookies-next";
import { LOCALES } from "../i18n/locales";
const getLocaleByCode = (code: string) => {
return Object.values(LOCALES).find((l) => l.code === code) ?? LOCALES.ENGLISH;
};
// Parse the Accept-Language header and return the first supported language
const getLanguageFromAcceptHeader = (acceptLanguage?: string) => {
if (!acceptLanguage) return "en";
const languages = acceptLanguage.split(",").map((l) => l.split(";")[0]);
const supportedLanguages = Object.values(LOCALES).map((l) => l.code);
for (const language of languages) {
// Try to match the full language code first, then the language code without the region
if (supportedLanguages.includes(language)) {
return language;
} else if (supportedLanguages.includes(language.split("-")[0])) {
return language.split("-")[0];
}
}
return "en";
};
const isLanguageSupported = (code: string) => {
return Object.values(LOCALES).some((l) => l.code === code);
};
const setLanguageCookie = (code: string) => {
setCookie("language", code, {
sameSite: "lax",
expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)),
});
};
export default {
getLocaleByCode,
getLanguageFromAcceptHeader,
isLanguageSupported,
setLanguageCookie,
};