export interface CountryData { id: string; twoLetters: string; text: string; }; export default class CountryUtilsBase { static getCountryCodeArr(langCode: string): CountryData[] { let localizedCountryList: CountryData[] = []; let countryNamesLocalizer: any; try { countryNamesLocalizer = new (Intl as any).DisplayNames([langCode], { type: 'region' }); } catch (error) { countryNamesLocalizer = null; } for (const item of CountryUtilsBase.buildBasicCountryCodeArr()) { let countryName: string = null; if (countryNamesLocalizer != null) { try { countryName = countryNamesLocalizer.of(item[1]); } catch (error) { countryName = null; } } if (countryName == null || countryName.length == 0) { countryName = item[2]; } if (item[1] == 'US') { countryName += ' (USA)'; } localizedCountryList.push({ id: item[0], twoLetters: item[1].toLowerCase(), text: countryName, }); } try { const collator = new Intl.Collator(langCode, { numeric: true, sensitivity: 'base' }); localizedCountryList = [...localizedCountryList].sort((a, b) => collator.compare(a.text, b.text)); } catch (error) { localizedCountryList = [...localizedCountryList].sort((a, b) => a.text.localeCompare(b.text)); } return localizedCountryList; } static buildBasicCountryCodeArr(): Array> { return [ [ 'AFG', 'AF', 'Afghanistan', ], [ 'ALA', 'AX', 'Åland Islands', ], [ 'ALB', 'AL', 'Albania', ], [ 'DZA', 'DZ', 'Algeria', ], [ 'ASM', 'AS', 'American Samoa', ], [ 'AND', 'AD', 'Andorra', ], [ 'AGO', 'AO', 'Angola', ], [ 'AIA', 'AI', 'Anguilla', ], [ 'ATA', 'AQ', 'Antarctica', ], [ 'ATG', 'AG', 'Antigua and Barbuda', ], [ 'ARG', 'AR', 'Argentina', ], [ 'ARM', 'AM', 'Armenia', ], [ 'ABW', 'AW', 'Aruba', ], [ 'AUS', 'AU', 'Australia', ], [ 'AUT', 'AT', 'Austria', ], [ 'AZE', 'AZ', 'Azerbaijan', ], [ 'BHS', 'BS', 'Bahamas', ], [ 'BHR', 'BH', 'Bahrain', ], [ 'BGD', 'BD', 'Bangladesh', ], [ 'BRB', 'BB', 'Barbados', ], [ 'BLR', 'BY', 'Belarus', ], [ 'BEL', 'BE', 'Belgium', ], [ 'BLZ', 'BZ', 'Belize', ], [ 'BEN', 'BJ', 'Benin', ], [ 'BMU', 'BM', 'Bermuda', ], [ 'BTN', 'BT', 'Bhutan', ], [ 'BOL', 'BO', 'Bolivia', ], [ 'BQ', 'BQ', 'Bonaire', ], [ 'BIH', 'BA', 'Bosnia and Herzegovina', ], [ 'BWA', 'BW', 'Botswana', ], [ 'BVT', 'BV', 'Bouvet Island', ], [ 'BRA', 'BR', 'Brazil', ], [ 'IOT', 'IO', 'British Indian Ocean Territory', ], [ 'BRN', 'BN', 'Brunei Darussalam', ], [ 'BGR', 'BG', 'Bulgaria', ], [ 'BFA', 'BF', 'Burkina Faso', ], [ 'BDI', 'BI', 'Burundi', ], [ 'KHM', 'KH', 'Cambodia', ], [ 'CMR', 'CM', 'Cameroon', ], [ 'CAN', 'CA', 'Canada', ], [ 'CPV', 'CV', 'Cape Verde', ], [ 'CYM', 'KY', 'Cayman Islands', ], [ 'CAF', 'CF', 'Central African Republic', ], [ 'TCD', 'TD', 'Chad', ], [ 'CHL', 'CL', 'Chile', ], [ 'CHN', 'CN', 'China', ], [ 'CXR', 'CX', 'Christmas Island', ], [ 'CCK', 'CC', 'Cocos (Keeling) Islands', ], [ 'COL', 'CO', 'Colombia', ], [ 'COM', 'KM', 'Comoros', ], [ 'CD', 'CD', 'Congo', ], [ 'COG', 'CG', 'Congo', ], [ 'COK', 'CK', 'Cook Islands', ], [ 'CRI', 'CR', 'Costa Rica', ], [ 'HRV', 'HR', 'Croatia', ], [ 'CUB', 'CU', 'Cuba', ], [ 'CUW', 'CW', 'Curaçao', ], [ 'CYP', 'CY', 'Cyprus', ], [ 'CZE', 'CZ', 'Czech Republic', ], [ 'DNK', 'DK', 'Denmark', ], [ 'DJI', 'DJ', 'Djibouti', ], [ 'DMA', 'DM', 'Dominica', ], [ 'DOM', 'DO', 'Dominican Republic', ], [ 'ECU', 'EC', 'Ecuador', ], [ 'EGY', 'EG', 'Egypt', ], [ 'SLV', 'SV', 'El Salvador', ], [ 'GNQ', 'GQ', 'Equatorial Guinea', ], [ 'ERI', 'ER', 'Eritrea', ], [ 'EST', 'EE', 'Estonia', ], [ 'ETH', 'ET', 'Ethiopia', ], [ 'FLK', 'FK', 'Falkland Islands (Malvinas)', ], [ 'FRO', 'FO', 'Faroe Islands', ], [ 'FJI', 'FJ', 'Fiji', ], [ 'FIN', 'FI', 'Finland', ], [ 'FRA', 'FR', 'France', ], [ 'GUF', 'GF', 'French Guiana', ], [ 'PYF', 'PF', 'French Polynesia', ], [ 'ATF', 'TF', 'French Southern Territories', ], [ 'GAB', 'GA', 'Gabon', ], [ 'GMB', 'GM', 'Gambia', ], [ 'GEO', 'GE', 'Georgia', ], [ 'DEU', 'DE', 'Germany', ], [ 'GHA', 'GH', 'Ghana', ], [ 'GIB', 'GI', 'Gibraltar', ], [ 'GRC', 'GR', 'Greece', ], [ 'GRL', 'GL', 'Greenland', ], [ 'GRD', 'GD', 'Grenada', ], [ 'GLP', 'GP', 'Guadeloupe', ], [ 'GUM', 'GU', 'Guam', ], [ 'GTM', 'GT', 'Guatemala', ], [ 'GGY', 'GG', 'Guernsey', ], [ 'GNB', 'GW', 'Guinea-Bissau', ], [ 'GIN', 'GN', 'Guinea', ], [ 'GUY', 'GY', 'Guyana', ], [ 'HTI', 'HT', 'Haiti', ], [ 'HMD', 'HM', 'Heard Island and McDonald Islands', ], [ 'VAT', 'VA', 'Holy See (Vatican City State)', ], [ 'HND', 'HN', 'Honduras', ], [ 'HKG', 'HK', 'Hong Kong', ], [ 'HUN', 'HU', 'Hungary', ], [ 'ISL', 'IS', 'Iceland', ], [ 'IND', 'IN', 'India', ], [ 'IDN', 'ID', 'Indonesia', ], [ 'IR', 'IR', 'Iran', ], [ 'IRQ', 'IQ', 'Iraq', ], [ 'IRL', 'IE', 'Ireland', ], [ 'IMN', 'IM', 'Isle of Man', ], [ 'ISR', 'IL', 'Israel', ], [ 'ITA', 'IT', 'Italy', ], [ 'CIV', 'CI', 'Ivory Coast', ], [ 'JAM', 'JM', 'Jamaica', ], [ 'JPN', 'JP', 'Japan', ], [ 'JEY', 'JE', 'Jersey', ], [ 'JOR', 'JO', 'Jordan', ], [ 'KAZ', 'KZ', 'Kazakhstan', ], [ 'KEN', 'KE', 'Kenya', ], [ 'KIR', 'KI', 'Kiribati', ], [ 'KP', 'KP', 'North Korea', ], [ 'KOR', 'KR', 'South Korea', ], [ 'XKX', 'XK', 'Kosovo', ], [ 'KWT', 'KW', 'Kuwait', ], [ 'KGZ', 'KG', 'Kyrgyzstan', ], [ 'LAO', 'LA', 'Lao People\'s Democratic Republic', ], [ 'LVA', 'LV', 'Latvia', ], [ 'LBN', 'LB', 'Lebanon', ], [ 'LSO', 'LS', 'Lesotho', ], [ 'LBR', 'LR', 'Liberia', ], [ 'LBY', 'LY', 'Libya', ], [ 'LIE', 'LI', 'Liechtenstein', ], [ 'LTU', 'LT', 'Lithuania', ], [ 'LUX', 'LU', 'Luxembourg', ], [ 'MAC', 'MO', 'Macao', ], [ 'MK', 'MK', 'Macedonia', ], [ 'MDG', 'MG', 'Madagascar', ], [ 'MWI', 'MW', 'Malawi', ], [ 'MYS', 'MY', 'Malaysia', ], [ 'MDV', 'MV', 'Maldives', ], [ 'MLI', 'ML', 'Mali', ], [ 'MLT', 'MT', 'Malta', ], [ 'MHL', 'MH', 'Marshall Islands', ], [ 'MTQ', 'MQ', 'Martinique', ], [ 'MRT', 'MR', 'Mauritania', ], [ 'MUS', 'MU', 'Mauritius', ], [ 'MYT', 'YT', 'Mayotte', ], [ 'MEX', 'MX', 'Mexico', ], [ 'FM', 'FM', 'Micronesia', ], [ 'MD', 'MD', 'Moldova', ], [ 'MCO', 'MC', 'Monaco', ], [ 'MNG', 'MN', 'Mongolia', ], [ 'MNE', 'ME', 'Montenegro', ], [ 'MSR', 'MS', 'Montserrat', ], [ 'MAR', 'MA', 'Morocco', ], [ 'MOZ', 'MZ', 'Mozambique', ], [ 'MMR', 'MM', 'Myanmar', ], [ 'NAM', 'NA', 'Namibia', ], [ 'NRU', 'NR', 'Nauru', ], [ 'NPL', 'NP', 'Nepal', ], [ 'ANT', 'AN', 'Netherlands Antilles', ], [ 'NLD', 'NL', 'Netherlands', ], [ 'NCL', 'NC', 'New Caledonia', ], [ 'NZL', 'NZ', 'New Zealand', ], [ 'NIC', 'NI', 'Nicaragua', ], [ 'NER', 'NE', 'Niger', ], [ 'NGA', 'NG', 'Nigeria', ], [ 'NIU', 'NU', 'Niue', ], [ 'NFK', 'NF', 'Norfolk Island', ], [ 'MNP', 'MP', 'Northern Mariana Islands', ], [ 'NOR', 'NO', 'Norway', ], [ 'OMN', 'OM', 'Oman', ], [ 'PAK', 'PK', 'Pakistan', ], [ 'PLW', 'PW', 'Palau', ], [ 'PS', 'PS', 'Palestinian Territory', ], [ 'PAN', 'PA', 'Panama', ], [ 'PNG', 'PG', 'Papua New Guinea', ], [ 'PRY', 'PY', 'Paraguay', ], [ 'PER', 'PE', 'Peru', ], [ 'PHL', 'PH', 'Philippines', ], [ 'PCN', 'PN', 'Pitcairn', ], [ 'POL', 'PL', 'Poland', ], [ 'PRT', 'PT', 'Portugal', ], [ 'PRI', 'PR', 'Puerto Rico', ], [ 'QAT', 'QA', 'Qatar', ], [ 'REU', 'RE', 'Réunion', ], [ 'ROU', 'RO', 'Romania', ], [ 'RUS', 'RU', 'Russian Federation', ], [ 'RWA', 'RW', 'Rwanda', ], [ 'BLM', 'BL', 'Saint Barthélemy', ], [ 'SH', 'SH', 'Saint Helena', ], [ 'KNA', 'KN', 'Saint Kitts and Nevis', ], [ 'LCA', 'LC', 'Saint Lucia', ], [ 'MAF', 'MF', 'Saint Martin (French part)', ], [ 'SPM', 'PM', 'Saint Pierre and Miquelon', ], [ 'VCT', 'VC', 'Saint Vincent and the Grenadines', ], [ 'WSM', 'WS', 'Samoa', ], [ 'SMR', 'SM', 'San Marino', ], [ 'STP', 'ST', 'Sao Tome and Principe', ], [ 'SAU', 'SA', 'Saudi Arabia', ], [ 'SEN', 'SN', 'Senegal', ], [ 'SRB', 'RS', 'Serbia', ], [ 'SYC', 'SC', 'Seychelles', ], [ 'SLE', 'SL', 'Sierra Leone', ], [ 'SGP', 'SG', 'Singapore', ], [ 'SXM', 'SX', 'Sint Maarten (Dutch part)', ], [ 'SVK', 'SK', 'Slovakia', ], [ 'SVN', 'SI', 'Slovenia', ], [ 'SLB', 'SB', 'Solomon Islands', ], [ 'SOM', 'SO', 'Somalia', ], [ 'ZAF', 'ZA', 'South Africa', ], [ 'SGS', 'GS', 'South Georgia and the South Sandwich Islands', ], [ 'SSD', 'SS', 'South Sudan', ], [ 'ESP', 'ES', 'Spain', ], [ 'LKA', 'LK', 'Sri Lanka', ], [ 'SDN', 'SD', 'Sudan', ], [ 'SUR', 'SR', 'Suriname', ], [ 'SJM', 'SJ', 'Svalbard and Jan Mayen', ], [ 'SWZ', 'SZ', 'Swaziland', ], [ 'SWE', 'SE', 'Sweden', ], [ 'CHE', 'CH', 'Switzerland', ], [ 'SYR', 'SY', 'Syrian Arab Republic', ], [ 'TWN', 'TW', 'Taiwan', ], [ 'TJK', 'TJ', 'Tajikistan', ], [ 'TZ', 'TZ', 'Tanzania', ], [ 'THA', 'TH', 'Thailand', ], [ 'TLS', 'TL', 'Timor-Leste', ], [ 'TGO', 'TG', 'Togo', ], [ 'TKL', 'TK', 'Tokelau', ], [ 'TON', 'TO', 'Tonga', ], [ 'TTO', 'TT', 'Trinidad and Tobago', ], [ 'TUN', 'TN', 'Tunisia', ], [ 'TUR', 'TR', 'Turkey', ], [ 'TKM', 'TM', 'Turkmenistan', ], [ 'TCA', 'TC', 'Turks and Caicos Islands', ], [ 'TUV', 'TV', 'Tuvalu', ], [ 'UGA', 'UG', 'Uganda', ], [ 'UKR', 'UA', 'Ukraine', ], [ 'ARE', 'AE', 'United Arab Emirates', ], [ 'GBR', 'GB', 'United Kingdom', ], [ 'UMI', 'UM', 'United States Minor Outlying Islands', ], [ 'USA', 'US', 'United States', ], [ 'URY', 'UY', 'Uruguay', ], [ 'UZB', 'UZ', 'Uzbekistan', ], [ 'VUT', 'VU', 'Vanuatu', ], [ 'VEN', 'VE', 'Venezuela', ], [ 'VNM', 'VN', 'Vietnam', ], [ 'VG', 'VG', 'Virgin Islands', ], [ 'VI', 'VI', 'Virgin Islands', ], [ 'WLF', 'WF', 'Wallis and Futuna', ], [ 'ESH', 'EH', 'Western Sahara', ], [ 'YEM', 'YE', 'Yemen', ], [ 'ZMB', 'ZM', 'Zambia', ], [ 'ZWE', 'ZW', 'Zimbabwe', ], ]; } static getByThreeLetterCode(threeLetterCode: string, langCode: string): CountryData { if (threeLetterCode == null) { return null; } else { threeLetterCode = threeLetterCode.toUpperCase(); } return CountryUtilsBase.getCountryCodeArr(langCode).find(p => p.id == threeLetterCode); } static getByTwoLetterCode(twoLetterCode: string, langCode: string): CountryData { if (twoLetterCode == null) { return null; } else { twoLetterCode = twoLetterCode.toLowerCase(); } return CountryUtilsBase.getCountryCodeArr(langCode).find(p => p.twoLetters == twoLetterCode); } static getTwoLetterCodeFromThreeLetterCode(threeLetterCode: string, langCode: string): string { return CountryUtilsBase.getByThreeLetterCode(threeLetterCode, langCode)?.twoLetters; } static getThreeLetterCodeFromTwoLetterCode(twoLetterCode: string, langCode: string): string { return CountryUtilsBase.getByTwoLetterCode(twoLetterCode, langCode)?.id; } }