@@ -17,6 +17,53 @@ const {
17
17
FrontendApplicationConfigProvider,
18
18
} = require ( '@theia/core/lib/browser/frontend-application-config-provider' ) ;
19
19
20
+ function fetchFrom ( path ) {
21
+ const { Endpoint } = require ( '@theia/core/lib/browser/endpoint' ) ;
22
+ const endpoint = new Endpoint ( { path } ) . getRestUrl ( ) . toString ( ) ;
23
+ return fetch ( endpoint ) ;
24
+ }
25
+
26
+ async function loadTranslations ( ) {
27
+ const { nls } = require ( '@theia/core/lib/common/nls' ) ;
28
+ const defaultLocale = typeof window === 'object' && window && window . localStorage . getItem ( nls . localeId ) || '' ;
29
+ if ( defaultLocale && ! nls . locale ) {
30
+ Object . assign ( nls , {
31
+ locale : defaultLocale
32
+ } ) ;
33
+ }
34
+ if ( nls . locale ) {
35
+ const response = await fetchFrom ( `/i18n/${ nls . locale } ` ) ;
36
+ nls . localization = await response . json ( ) ;
37
+ }
38
+ }
39
+
40
+ async function loadBackendOS ( ) {
41
+ const response = await fetchFrom ( '/os' ) ;
42
+ const osType = await response . text ( ) ;
43
+ const isWindows = osType === 'Windows' ;
44
+ const isOSX = osType === 'OSX' ;
45
+ OS . backend . isOSX = isOSX ;
46
+ OS . backend . isWindows = isWindows ;
47
+ OS . backend . type = ( ) => osType ;
48
+ }
49
+
50
+ function customizeMonacoNls ( ) {
51
+ const MonacoNls = require ( '@theia/monaco-editor-core/esm/vs/nls' ) ;
52
+ const { nls : TheiaNls } = require ( '@theia/core/lib/common/nls' ) ;
53
+ const { Localization } = require ( '@theia/core/lib/common/i18n/localization' ) ;
54
+ Object . assign ( MonacoNls , {
55
+ localize ( _ , label , ...args ) {
56
+ if ( TheiaNls . locale ) {
57
+ const defaultKey = TheiaNls . getDefaultKey ( label ) ;
58
+ if ( defaultKey ) {
59
+ return TheiaNls . localize ( defaultKey , label , ...args ) ;
60
+ }
61
+ }
62
+ return Localization . format ( label , args ) ;
63
+ }
64
+ } ) ;
65
+ }
66
+
20
67
// It is a mighty hack to support theme updates in the bundled IDE2.
21
68
// If the custom theme registration happens before the restoration of the existing monaco themes, then any custom theme changes will be ignored.
22
69
// This patch introduces a static deferred promise in the monaco-theming service that will be resolved when the restoration is ready.
@@ -25,8 +72,14 @@ const {
25
72
// This patch customizes the monaco theme service behavior before loading the DI containers via the preload.
26
73
// The preload is called only once before the app loads. The Theia extensions are not loaded at that point, but the app config provider is ready.
27
74
const preloader = require ( '@theia/core/lib/browser/preloader' ) ;
28
- const originalPreload = preloader . preload ;
29
75
preloader . preload = async function ( ) {
76
+ // Must require the monaco frontend module to activate the NLS customization for monaco.
77
+ // Otherwise, the NLS customization would trigger after the monaco UI components with all their translations are already loaded.
78
+ await Promise . allSettled ( [
79
+ loadTranslations ( ) ,
80
+ loadBackendOS ( ) ,
81
+ ] ) ;
82
+ customizeMonacoNls ( ) ;
30
83
const { MonacoThemingService } = require ( '@theia/monaco/lib/browser/monaco-theming-service' ) ;
31
84
const { MonacoThemeServiceIsReady } = require ( 'arduino-ide-extension/lib/browser/utils/window' ) ;
32
85
const { Deferred } = require ( '@theia/core/lib/common/promise-util' ) ;
@@ -42,7 +95,6 @@ preloader.preload = async function () {
42
95
await this . restore ( ) ;
43
96
ready . resolve ( ) ;
44
97
} . bind ( MonacoThemingService ) ;
45
- return originalPreload ( ) ;
46
98
} . bind ( preloader ) ;
47
99
48
100
const lightTheme = 'arduino-theme' ;
0 commit comments