draw/src/lib/i18n/useI18n.ts
Adrián Borrageiros Mourelos ccecbe7c1f SAVE
2025-05-13 12:02:02 +02:00

40 lines
916 B
TypeScript

"use client";
import { useState, useEffect } from "react";
import { useLanguage } from "./language-context";
import { getTranslation } from "./i18n";
export function useI18n() {
const { locale } = useLanguage();
const [t, setT] = useState<(key: string) => string | undefined>(null!);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
let isMounted = true;
const loadTranslations = async () => {
setIsLoading(true);
try {
const translation = await getTranslation(locale);
if (isMounted) {
setT(() => translation.t);
}
} catch (error) {
console.error("Error loading translations:", error);
} finally {
if (isMounted) {
setIsLoading(false);
}
}
};
loadTranslations();
return () => {
isMounted = false;
};
}, [locale]);
return { t, isLoading, locale };
}