Localized country names.

This commit is contained in:
Mike Cao 2020-09-30 16:27:27 -07:00
parent e5cd162b83
commit fd69f0df24
22 changed files with 69 additions and 263 deletions

32
hooks/useCountryNames.js Normal file
View file

@ -0,0 +1,32 @@
import { useState, useEffect } from 'react';
import { get } from 'lib/web';
import enUS from 'public/country/en-US.json';
const countryNames = {
'en-US': enUS,
};
export default function useCountryNames(locale) {
const [list, setList] = useState(countryNames[locale] || enUS);
async function loadData(locale) {
const { ok, data } = await get(`/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;
}