mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
35 lines
779 B
JavaScript
35 lines
779 B
JavaScript
|
import { useState, useEffect } from 'react';
|
||
|
import { useRouter } from 'next/router';
|
||
|
import { get } from 'lib/web';
|
||
|
import enUS from 'public/language/en-US.json';
|
||
|
|
||
|
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 { ok, data } = await get(`${basePath}/language/${locale}.json`);
|
||
|
|
||
|
if (ok) {
|
||
|
languageNames[locale] = data;
|
||
|
setList(languageNames[locale]);
|
||
|
} else {
|
||
|
setList(enUS);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
useEffect(() => {
|
||
|
if (!languageNames[locale]) {
|
||
|
loadData(locale);
|
||
|
} else {
|
||
|
setList(languageNames[locale]);
|
||
|
}
|
||
|
}, [locale]);
|
||
|
|
||
|
return list;
|
||
|
}
|