mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-25 02:36:20 +01:00
35 lines
784 B
JavaScript
35 lines
784 B
JavaScript
import { useState, useEffect } from 'react';
|
|
import { useRouter } from 'next/router';
|
|
import { get } from 'next-basics';
|
|
import enUS from 'public/intl/country/en-US.json';
|
|
|
|
const countryNames = {
|
|
'en-US': enUS,
|
|
};
|
|
|
|
export default function useCountryNames(locale) {
|
|
const [list, setList] = useState(countryNames[locale] || enUS);
|
|
const { basePath } = useRouter();
|
|
|
|
async function loadData(locale) {
|
|
const { ok, data } = await get(`${basePath}/intl/country/${locale}.json`);
|
|
|
|
if (ok) {
|
|
countryNames[locale] = data;
|
|
setList(countryNames[locale]);
|
|
} else {
|
|
setList(enUS);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (!countryNames[locale]) {
|
|
loadData(locale);
|
|
} else {
|
|
setList(countryNames[locale]);
|
|
}
|
|
}, [locale]);
|
|
|
|
return list;
|
|
}
|