stash/ui/v2.5/src/utils/country.ts
InfiniteTF 7b7d6758ef
Change performer country value to be ISO code (#1922)
* Change performer country value to be ISO code
* Localize country names
* Use country select for filter

Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
2022-10-28 16:37:57 +11:00

32 lines
740 B
TypeScript

import Countries from "i18n-iso-countries";
import { getLocaleCode } from "src/locales";
export const getCountryByISO = (
iso: string | null | undefined,
locale: string = "en"
): string | undefined => {
if (!iso) return;
const ret = Countries.getName(iso, getLocaleCode(locale));
if (ret) {
return ret;
}
// fallback to english if locale is not en
if (locale !== "en") {
return Countries.getName(iso, "en");
}
};
export const getCountries = (locale: string = "en") => {
let countries = Countries.getNames(getLocaleCode(locale));
if (!countries.length) {
countries = Countries.getNames("en");
}
return Object.entries(countries).map(([code, name]) => ({
label: name,
value: code,
}));
};