-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlanguage.ts
71 lines (60 loc) · 1.98 KB
/
language.ts
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
import { Language as LanguageFromClient, getLanguageInfoFromCode, type LanguageInfo } from "@maptiler/client";
// Adding some language entries that are specific to the SDK
const Language = {
/**
* Language mode to display labels in both the local language and the language of the visitor's device, concatenated.
* Note that if those two languages are the same, labels won't be duplicated.
*/
VISITOR: {
code: null,
flag: "visitor",
name: "Visitor",
latin: true,
isMode: true,
geocoding: false,
} as LanguageInfo,
/**
* Language mode to display labels in both the local language and English, concatenated.
* Note that if those two languages are the same, labels won't be duplicated.
*/
VISITOR_ENGLISH: {
code: null,
flag: "visitor_en",
name: "Visitor English",
latin: true,
isMode: true,
geocoding: false,
} as LanguageInfo,
/**
* Language mode to display labels in a language enforced in the style.
*/
STYLE: { code: null, flag: "style", name: "Style", latin: false, isMode: true, geocoding: false } as LanguageInfo,
/**
* Language mode to display labels in a language enforced in the style. The language cannot be further modified.
*/
STYLE_LOCK: {
code: null,
flag: "style_lock",
name: "Style Lock",
latin: false,
isMode: true,
geocoding: false,
} as LanguageInfo,
...LanguageFromClient,
} as const;
/**
* Get the browser language
*/
export function getBrowserLanguage(): LanguageInfo {
if (typeof navigator === "undefined") {
const code = Intl.DateTimeFormat().resolvedOptions().locale.split("-")[0];
const langInfo = getLanguageInfoFromCode(code);
if (langInfo) return langInfo;
return Language.ENGLISH;
}
const canditatelangs = Array.from(new Set(navigator.languages.map((l) => l.split("-")[0])))
.map((code) => getLanguageInfoFromCode(code))
.filter((li) => li);
return canditatelangs[0] ?? Language.LOCAL;
}
export { Language, type LanguageInfo };