[READ-ONLY] Mirror of https://github.com/mrgnw/ananas. learn multiple languages at once
ananas.xcc.es
1// Map of country names to ISO 3166-1 alpha-2 codes
2const countryNameToCode: Record<string, string> = {
3 'People\'s Republic of China': 'CN',
4 'Taiwan': 'TW',
5 'United States': 'US',
6 'United Kingdom': 'GB',
7 'Japan': 'JP',
8 'Korea': 'KR',
9 'South Korea': 'KR',
10 'North Korea': 'KP',
11 'France': 'FR',
12 'Germany': 'DE',
13 'Italy': 'IT',
14 'Spain': 'ES',
15 'Russia': 'RU',
16 'Brazil': 'BR',
17 'India': 'IN',
18 'Indonesia': 'ID',
19 'Vietnam': 'VN',
20 'Thailand': 'TH',
21 'Turkey': 'TR',
22 'Iran': 'IR',
23 'Saudi Arabia': 'SA',
24 'Egypt': 'EG',
25 'Nigeria': 'NG',
26 'Pakistan': 'PK',
27 'Bangladesh': 'BD',
28 'Mexico': 'MX',
29 'Philippines': 'PH',
30 'Ethiopia': 'ET',
31 'Malaysia': 'MY',
32 'Ukraine': 'UA',
33 // Add more as needed
34};
35
36/**
37 * Convert a country code to an emoji flag
38 * @param countryCode - ISO 3166-1 alpha-2 country code
39 * @returns Emoji flag for the country
40 */
41export function getCountryFlag(countryCode: string): string {
42 const base = 127397; // Regional Indicator Symbol Letter A
43 const chars = countryCode.toUpperCase().split('');
44 return String.fromCodePoint(
45 ...chars.map(char => char.charCodeAt(0) + base)
46 );
47}
48
49/**
50 * Get the primary flag emoji for a language based on its ISO code
51 * @param iso - ISO 639-3 code
52 * @param countries - Array of country names
53 * @returns An emoji flag or undefined if no matching country found
54 */
55export function getLanguageFlag(iso: string, countries: string[]): string | undefined {
56 if (!countries.length) return undefined;
57
58 // Get the first country that has a known country code
59 for (const country of countries) {
60 const countryCode = countryNameToCode[country];
61 if (countryCode) {
62 return getCountryFlag(countryCode);
63 }
64 }
65
66 return undefined;
67}
68
69/**
70 * Language to flag mapping
71 * Key: ISO code
72 * Value: Emoji flag
73 */
74export const languageFlags: Record<string, string> = {
75 // Some common overrides where we want to specify a particular flag
76 'en': getCountryFlag('GB'), // English -> UK flag
77 'es': getCountryFlag('ES'), // Spanish -> Spain flag
78 'pt': getCountryFlag('PT'), // Portuguese -> Portugal flag
79 'zh': getCountryFlag('CN'), // Chinese -> China flag
80 'ja': getCountryFlag('JP'), // Japanese -> Japan flag
81 'ko': getCountryFlag('KR'), // Korean -> South Korea flag
82 'hi': getCountryFlag('IN'), // Hindi -> India flag
83 'ar': getCountryFlag('SA'), // Arabic -> Saudi Arabia flag
84 'bn': getCountryFlag('BD'), // Bengali -> Bangladesh flag
85 'ru': getCountryFlag('RU'), // Russian -> Russia flag
86 // Add more manual mappings as needed
87};
88
89/**
90 * Get a flag emoji for a language
91 * @param iso - ISO code (639-3 or 639-1)
92 * @param countries - Optional array of countries where the language is spoken
93 * @returns An emoji flag or undefined if no flag could be determined
94 */
95export function getFlag(iso: string, countries?: string[]): string | undefined {
96 // Check for manual override first
97 if (iso in languageFlags) {
98 return languageFlags[iso];
99 }
100
101 // If countries are provided, try to get a flag based on the countries
102 if (countries?.length) {
103 return getLanguageFlag(iso, countries);
104 }
105
106 return undefined;
107}