2021-12-02 04:25:00 +01:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { get } from 'lib/web';
|
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) {
|
2022-03-19 03:17:23 +01:00
|
|
|
const { ok, data } = await get(`${basePath}/intl/language/${locale}.json`);
|
2021-12-02 04:25:00 +01:00
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
languageNames[locale] = data;
|
|
|
|
setList(languageNames[locale]);
|
|
|
|
} else {
|
|
|
|
setList(enUS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!languageNames[locale]) {
|
|
|
|
loadData(locale);
|
|
|
|
} else {
|
|
|
|
setList(languageNames[locale]);
|
|
|
|
}
|
|
|
|
}, [locale]);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|