mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
Replace country-code-lookup with the existing Intl.DisplayNames helper. Co-Authored-By: GPT-6 <noreply@openai.com>
32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
// Localized country/region names via the built-in Intl.DisplayNames API.
|
|
// Names come from the runtime's CLDR/ICU data, so any ISO 3166-1 alpha-2 code
|
|
// resolves in any supported locale with zero maintenance and zero bundle weight.
|
|
|
|
const displayNamesCache = new Map();
|
|
|
|
const getDisplayNames = (lang) => {
|
|
if (!displayNamesCache.has(lang)) {
|
|
let displayNames = null;
|
|
try {
|
|
// fallback: 'none' → unknown-but-well-formed codes yield undefined
|
|
// instead of echoing the code back, so callers get '' as before
|
|
displayNames = new Intl.DisplayNames([lang], { type: 'region', fallback: 'none' });
|
|
} catch {
|
|
// malformed / unsupported locale — cache the null so we don't retry
|
|
}
|
|
displayNamesCache.set(lang, displayNames);
|
|
}
|
|
return displayNamesCache.get(lang);
|
|
};
|
|
|
|
const getCountryName = (abbr, lang) => {
|
|
if (typeof abbr !== 'string' || typeof lang !== 'string') return '';
|
|
const code = abbr.toUpperCase();
|
|
// ZZ is the ISO "unknown region" placeholder; return '' so callers can fall back
|
|
if (!/^[A-Z]{2}$/.test(code) || code === 'ZZ') return '';
|
|
const displayNames = getDisplayNames(lang);
|
|
return (displayNames && displayNames.of(code)) || '';
|
|
};
|
|
|
|
export default getCountryName;
|