Repository navigation
Expand file tree
/
Copy pathregionalFlags.js
More file actions
125 lines (122 loc) · 6.15 KB
/
Copy pathregionalFlags.js
File metadata and controls
125 lines (122 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Regional variants of a locale's flag in the language switcher: a visitor
// in Australia, New Zealand, South Africa or the United Kingdom sees that
// country's flag next to "EN" instead of the US one, and a visitor in a
// Spanish-speaking country of the Americas (or in Equatorial Guinea) sees
// that country's flag next to "ES" instead of Spain's. Cosmetic only: the
// locale, its URL and its content are the same for everyone.
//
// How it works. The site is static, so every page is served with the default
// flag (inline SVG, in src/theme/NavbarItem/LocaleDropdownNavbarItem/flags.js).
// headScript() below is a few hundred bytes of inline script in <head> that
// runs before the first paint, picks a variant from what the browser says
// about itself, and sets <html data-flag-<locale>="<variant>">. The switcher's
// stylesheet (styles.module.css next to the component) then paints that
// variant's artwork over the default flag. Nothing is fetched, nothing is
// sent anywhere, and nothing moves on the page.
//
// It falls back to the default flag when the browser's region is not listed
// here, when the browser offers no language or time zone, when the script
// throws, and when a variant named here has no rule in the stylesheet.
//
// Adding a variant: one entry here (regions and, if the country has its own
// time zones, timeZones) and one pair of rules in the component's
// styles.module.css, which holds the artwork as an SVG data URI. Every byte
// here is in every page, so keep the lists short.
//
// CommonJS on purpose: docusaurus.config.js requires it.
// The two signals are combined, in this order:
// 1. regions a browser language (navigator.languages, "en-AU") that
// names one of these regions decides, wherever the
// machine is. Nobody sets en-AU by accident, so an en-AU
// browser in London still gets the Australian flag.
// 2. timeZones otherwise the time zone, which says where the machine
// is. Browsers in these countries are very often left on
// en-US or en-GB.
// 3. looseRegions otherwise a language that names one of these regions.
// en-GB is the setting much of the English-speaking world
// outside the US runs on, so it only counts when the time
// zone says nothing: a browser in Melbourne set to en-GB
// is in Australia (it showed the UK flag while en-GB
// decided on its own), one in New York set to en-GB gets
// the UK flag.
// Time zones are matched by IANA name prefix, not by UTC offset: an offset
// such as +11 is shared with other countries and moves with daylight saving.
//
// Spanish (es) works the same way with Spain as the default: es-MX decides
// wherever the machine is, otherwise a time zone of a Spanish-speaking
// country does, whatever the browser language (an en-US or es-ES browser in
// Bogota gets the Colombian flag, as an en-US browser in Sydney gets the
// Australian one). es-ES, es-419 (Latin America), es-US and a bare "es" name
// no country here, so outside those time zones they keep the flag of Spain.
// Mexico and Argentina have no common prefix, so every zone is listed;
// Chrome reports five Argentine zones under their pre-1993 names
// (America/Buenos_Aires), Firefox under America/Argentina/.
const REGIONAL_FLAGS = {
en: {
regions: { AU: 'au', NZ: 'nz', ZA: 'za' },
timeZones: [
['Australia/', 'au'],
['Pacific/Auckland', 'nz'],
['Pacific/Chatham', 'nz'],
['Africa/Johannesburg', 'za'],
['Europe/London', 'gb'],
],
looseRegions: { GB: 'gb' },
},
es: {
regions: {
MX: 'mx', AR: 'ar', CO: 'co', CL: 'cl', PE: 'pe', VE: 've', EC: 'ec',
GT: 'gt', CU: 'cu', BO: 'bo', DO: 'do', HN: 'hn', PY: 'py', SV: 'sv',
NI: 'ni', CR: 'cr', PA: 'pa', UY: 'uy', PR: 'pr', GQ: 'gq',
},
timeZones: [
['America/Mexico_City', 'mx'],
['America/Monterrey', 'mx'],
['America/Merida', 'mx'],
['America/Cancun', 'mx'],
['America/Tijuana', 'mx'],
['America/Hermosillo', 'mx'],
['America/Mazatlan', 'mx'],
['America/Chihuahua', 'mx'],
['America/Ciudad_Juarez', 'mx'],
['America/Ojinaga', 'mx'],
['America/Matamoros', 'mx'],
['America/Bahia_Banderas', 'mx'],
['America/Argentina/', 'ar'],
['America/Buenos_Aires', 'ar'],
['America/Cordoba', 'ar'],
['America/Mendoza', 'ar'],
['America/Catamarca', 'ar'],
['America/Jujuy', 'ar'],
['America/Bogota', 'co'],
['America/Santiago', 'cl'],
['America/Punta_Arenas', 'cl'],
['America/Coyhaique', 'cl'],
['Pacific/Easter', 'cl'],
['America/Lima', 'pe'],
['America/Caracas', 've'],
['America/Guayaquil', 'ec'],
['Pacific/Galapagos', 'ec'],
['America/Guatemala', 'gt'],
['America/Havana', 'cu'],
['America/La_Paz', 'bo'],
['America/Santo_Domingo', 'do'],
['America/Tegucigalpa', 'hn'],
['America/Asuncion', 'py'],
['America/El_Salvador', 'sv'],
['America/Managua', 'ni'],
['America/Costa_Rica', 'cr'],
['America/Panama', 'pa'],
['America/Montevideo', 'uy'],
['America/Puerto_Rico', 'pr'],
['Africa/Malabo', 'gq'],
],
},
};
// The inline script for <head>. Written for old browsers (no arrow
// functions, no optional chaining) and wrapped so that any error leaves the
// page exactly as it was served.
function headScript(config = REGIONAL_FLAGS) {
return `!function(){try{var c=${JSON.stringify(config)},d=document.documentElement,n=navigator,l=n.languages&&n.languages.length?n.languages:[n.language||""],z="";try{z=Intl.DateTimeFormat().resolvedOptions().timeZone||""}catch(e){}var g=function(k,m){for(var i=0,p;m&&i<l.length;i++){p=String(l[i]).split("-");if(p[0].toLowerCase()===k&&p[1]&&m[p[1].toUpperCase()])return m[p[1].toUpperCase()]}return""};for(var k in c){var f=g(k,c[k].regions),t=c[k].timeZones||[],i;for(i=0;i<t.length&&!f;i++)if(z.indexOf(t[i][0])===0)f=t[i][1];f=f||g(k,c[k].looseRegions);if(f)d.setAttribute("data-flag-"+k,f)}}catch(e){}}();`;
}
module.exports = { REGIONAL_FLAGS, headScript };