feat(i18n): translate library page

This commit is contained in:
Julien Froidefond
2025-02-27 12:29:15 +01:00
parent ed8817d76c
commit 52a212ef07
9 changed files with 147 additions and 56 deletions

View File

@@ -1,12 +1,32 @@
import { useTranslation } from "react-i18next";
export function useTranslate() {
const { t, i18n } = useTranslation("common");
const { t: tBase, i18n } = useTranslation("common");
const changeLanguage = (lang: string) => {
i18n.changeLanguage(lang);
};
const t = (translationKey: string, values?: { [key: string]: number | string }) => {
if (values && Object.keys(values).length > 0) {
const translatedText = tBase(translationKey, values);
const placeholderRegex = new RegExp(`(\{${Object.keys(values).join("}|{")}\})`, "g");
const parts = translatedText.split(placeholderRegex);
return parts
.map((part) => {
const key = part.replace(/[{}]/g, "");
if (values[key] !== undefined) {
return values[key];
}
return part;
})
.join("");
}
return tBase(translationKey, values);
};
return {
t,
i18n,