@@ -5,18 +5,63 @@ const options = getOptions()
5
5
// TODO: Add proper types.
6
6
/* eslint-disable @typescript-eslint/no-explicit-any */
7
7
8
- let nlsConfig : any
8
+ // NOTE@jsjoeio
9
+ // This lives here ../../../lib/vscode/src/vs/base/common/platform.ts#L106
10
+ export const nlsConfigElementId = "vscode-remote-nls-configuration"
11
+
12
+ type NlsConfiguration = {
13
+ locale : string
14
+ availableLanguages : { [ key : string ] : string } | { }
15
+ _languagePackId ?: string
16
+ _translationsConfigFile ?: string
17
+ _cacheRoot ?: string
18
+ _resolvedLanguagePackCoreLocation ?: string
19
+ _corruptedFile ?: string
20
+ _languagePackSupport ?: boolean
21
+ loadBundle ?: any
22
+ }
23
+
24
+ /**
25
+ * A helper function to get the NLS Configuration settings.
26
+ *
27
+ * This is used by VSCode for localizations (i.e. changing
28
+ * the display language).
29
+ *
30
+ * Make sure to wrap this in a try/catch block when you call it.
31
+ **/
32
+ export function getNlsConfiguration ( document : Document ) {
33
+ const errorMsgPrefix = "[vscode]"
34
+ const nlsConfigElement = document ?. getElementById ( nlsConfigElementId )
35
+ const nlsConfig = nlsConfigElement ?. getAttribute ( "data-settings" )
36
+
37
+ if ( ! document ) {
38
+ throw new Error ( `${ errorMsgPrefix } Could not parse NLS configuration. document is undefined.` )
39
+ }
40
+
41
+ if ( ! nlsConfigElement ) {
42
+ throw new Error (
43
+ `${ errorMsgPrefix } Could not parse NLS configuration. Could not find nlsConfigElement with id: ${ nlsConfigElementId } ` ,
44
+ )
45
+ }
46
+
47
+ if ( ! nlsConfig ) {
48
+ return undefined
49
+ }
50
+
51
+ return JSON . parse ( nlsConfig ) as NlsConfiguration
52
+ }
53
+
9
54
try {
10
- nlsConfig = JSON . parse ( document . getElementById ( "vscode-remote-nls-configuration" ) ! . getAttribute ( "data-settings" ) ! )
11
- if ( nlsConfig . _resolvedLanguagePackCoreLocation ) {
55
+ const nlsConfig = getNlsConfiguration ( document )
56
+ if ( nlsConfig ? ._resolvedLanguagePackCoreLocation ) {
12
57
const bundles = Object . create ( null )
13
58
nlsConfig . loadBundle = ( bundle : any , _language : any , cb : any ) : void => {
14
59
const result = bundles [ bundle ]
15
60
if ( result ) {
16
61
return cb ( undefined , result )
17
62
}
18
63
// FIXME: Only works if path separators are /.
19
- const path = nlsConfig . _resolvedLanguagePackCoreLocation + "/" + bundle . replace ( / \/ / g, "!" ) + ".nls.json"
64
+ const path = nlsConfig ? ._resolvedLanguagePackCoreLocation + "/" + bundle . replace ( / \/ / g, "!" ) + ".nls.json"
20
65
fetch ( `${ options . base } /vscode/resource/?path=${ encodeURIComponent ( path ) } ` )
21
66
. then ( ( response ) => response . json ( ) )
22
67
. then ( ( json ) => {
27
72
}
28
73
}
29
74
} catch ( error ) {
75
+ console . error ( error )
30
76
/* Probably fine. */
31
77
}
32
78
0 commit comments