-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (120 loc) · 3.53 KB
/
index.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const html = `<!doctype html>
<html>
<head>
<style>
.localeTitle {
font-weight: bold;
width: 70px;
}
</style>
</head>
<div id="container">
<h1>Formatting with Intl</h1>
<h3>Formatting today's date via toLocaleDateString</h3>
<table>
<thead>
<tr>
<td><h4>Locale</h4></td>
<td><h4>Formatted Date</h4></td>
</tr>
</thead>
<tbody id="datesTable">
</tbody>
</table>
<h3>Formatting the current time via toLocaleTimeString</h3>
<table>
<thead>
<tr>
<td><h4>Locale</h4></td>
<td><h4>Formatted Time</h4></td>
</tr>
</thead>
<tbody id="timesTable">
</tbody>
</table>
<h3>Formatting a large number</h3>
<table>
<thead>
<tr>
<td><h4>Locale</h4></td>
<td><h4>Formatted Numbers</h4></td>
</tr>
</thead>
<tbody id="numbersTable">
</tbody>
</table>
</div>
<!-- ... -->
<script>
var locales = ['de-DE', 'en-US', 'en-GB', 'es-ES', 'es-CL', 'fr-FR', 'hi-IN', 'in-ID', 'ja-JP', 'ko-KR', 'pt-BR', 'pt-PT', 'ru-RU'];
// Dates
var localeDates = {};
var now = new Date(2020, 2, 4);
var datesTable = document.querySelector("#datesTable");
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];
localeDates[locale] = now.toLocaleDateString(locale);
}
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];
var row = document.createElement('tr');
var title = document.createElement('td');
title.textContent = locale;
title.setAttribute('class', 'localeTitle');
row.appendChild(title);
var value = document.createElement('td');
value.textContent = localeDates[locale];
row.appendChild(value);
datesTable.appendChild(row);
}
// Times
var localeTimes = {};
var now = new Date();
var timesTable = document.querySelector("#timesTable");
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];
localeTimes[locale] = now.toLocaleTimeString(locale);
}
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];
var row = document.createElement('tr');
var title = document.createElement('td');
title.textContent = locale;
title.setAttribute('class', 'localeTitle');
row.appendChild(title);
var value = document.createElement('td');
value.textContent = localeTimes[locale];
row.appendChild(value);
timesTable.appendChild(row);
}
// Numbers
var localeNumbers = {};
var number = 300000.03
var numbersTable = document.querySelector("#numbersTable");
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];
localeNumbers[locale] = number.toLocaleString(locale);
}
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];
var row = document.createElement('tr');
var title = document.createElement('td');
title.textContent = locale;
title.setAttribute('class', 'localeTitle');
row.appendChild(title);
var value = document.createElement('td');
value.textContent = localeNumbers[locale];
row.appendChild(value);
numbersTable.appendChild(row);
}
</script>
</html>
`
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest() {
return new Response(html, {
headers: { 'content-type': 'text/html' },
})
}