Files
MyIP/common/country-name.js
jason5ng32andGPT-6 cc0053ac16 Refactor(ipinfo): share Intl country names with the frontend
Replace country-code-lookup with the existing Intl.DisplayNames helper.

Co-Authored-By: GPT-6 <noreply@openai.com>
2026-09-16 18:29:19 +08:00

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;