umami/hooks/useLanguageNames.js

37 lines
813 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,
};
2023-05-18 08:20:06 +02:00
export function useLanguageNames(locale) {
2021-12-02 04:25:00 +01:00
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;
}
2023-05-18 08:20:06 +02:00
export default useLanguageNames;