umami/hooks/useLanguageNames.js

35 lines
787 B
JavaScript
Raw Normal View History

2021-12-02 04:25:00 +01:00
import { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
2022-08-29 05:20:54 +02:00
import { get } from 'next-basics';
2022-03-19 03:17:23 +01:00
import enUS from 'public/intl/language/en-US.json';
2021-12-02 04:25:00 +01:00
const languageNames = {
'en-US': enUS,
};
export default function useLanguageNames(locale) {
const [list, setList] = useState(languageNames[locale] || enUS);
const { basePath } = useRouter();
async function loadData(locale) {
const data = await get(`${basePath}/intl/language/${locale}.json`);
2021-12-02 04:25:00 +01:00
if (data) {
2021-12-02 04:25:00 +01:00
languageNames[locale] = data;
setList(languageNames[locale]);
} else {
setList(enUS);
}
}
useEffect(() => {
if (!languageNames[locale]) {
loadData(locale);
} else {
setList(languageNames[locale]);
}
}, [locale]);
return list;
}