-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.html
123 lines (107 loc) · 3.66 KB
/
language.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Protomaps Language Switch</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='https://unpkg.com/[email protected]/dist/maplibre-gl.css' />
<script src='./dist/maplibre-gl-dev.js'></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
.dropdown-container {
position: absolute;
top: 10px;
left: 10px;
z-index: 1; /* Ensure the dropdown is above the map */
background-color: white;
padding: 10px;
border-radius: 4px;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="dropdown-container">
<select id="map-dropdown">
<option value="en">English</option>
<option value="it">Italian</option>
<option value="fr">French</option>
<option value="de">German</option>
<option value="el">Greek</option>
<option value="ar">Arabic</option>
<option value="bu">Bulgarian</option>
<option value="cn">Chinese</option>
<option value="ja">Japanese</option>
</select>
</div>
<div id="map"></div>
<script>
function getQueryParam(param) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
}
function setQueryParam(param, value) {
const url = new URL(window.location);
url.searchParams.set(param, value);
window.history.replaceState(null, '', url.toString());
}
var script = getQueryParam('script');
if (!script) {
setQueryParam('script', 'Latin');
script = 'Latin';
}
var language = getQueryParam('language');
if (!language) {
setQueryParam('language', 'en');
language = 'en';
}
async function run() {
maplibregl.setRTLTextPlugin(
'https://unpkg.com/@mapbox/[email protected]/mapbox-gl-rtl-text.min.js',
true
);
const response = await fetch('language-transform.js');
var transform = await response.text();
var settings = `var script = '${script}'; var language = '${language}';`;
const decodedString = decodeURIComponent(`${settings}${transform}`);
const blob = new Blob([decodedString], { type: 'application/javascript' });
const workerUrl = URL.createObjectURL(blob);
maplibregl.importScriptInWorkers(workerUrl);
const map = new maplibregl.Map({
container: 'map',
style: 'style-language.json',
center: [24, 38],
zoom: 8,
hash: "map"
});
}
run();
function dropdownChanged() {
const language = document.getElementById('map-dropdown').value;
var script = '';
if (['en', 'it', 'de', 'fr'].includes(language)) {
script = 'Latin';
}
else if (['el'].includes(language)) {
script = 'Greek';
}
else if (['ar'].includes(language)) {
script = 'Arabic';
}
else if (['bu'].includes(language)) {
script = 'Cyrillic';
}
else if (['cn'].includes(language)) {
script = 'Han';
}
else if (['ja'].includes(language)) {
script = 'Han';
}
window.location.href = `language?script=${script}&language=${language}${window.location.hash}`;
}
document.getElementById('map-dropdown').addEventListener('change', dropdownChanged);
document.getElementById('map-dropdown').value = language;
</script>
</body>
</html>