Skip to content

Commit dd7417c

Browse files
author
Akos Kitta
committed
Customized the preload to rebind monaco NLS.
Signed-off-by: Akos Kitta <[email protected]>
1 parent af6ceb8 commit dd7417c

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

electron-app/patch/frontend/index.js

+54-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,53 @@ const {
1717
FrontendApplicationConfigProvider,
1818
} = require('@theia/core/lib/browser/frontend-application-config-provider');
1919

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+
2067
// It is a mighty hack to support theme updates in the bundled IDE2.
2168
// If the custom theme registration happens before the restoration of the existing monaco themes, then any custom theme changes will be ignored.
2269
// 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 {
2572
// This patch customizes the monaco theme service behavior before loading the DI containers via the preload.
2673
// 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.
2774
const preloader = require('@theia/core/lib/browser/preloader');
28-
const originalPreload = preloader.preload;
2975
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();
3083
const { MonacoThemingService } = require('@theia/monaco/lib/browser/monaco-theming-service');
3184
const { MonacoThemeServiceIsReady } = require('arduino-ide-extension/lib/browser/utils/window');
3285
const { Deferred } = require('@theia/core/lib/common/promise-util');
@@ -42,7 +95,6 @@ preloader.preload = async function () {
4295
await this.restore();
4396
ready.resolve();
4497
}.bind(MonacoThemingService);
45-
return originalPreload();
4698
}.bind(preloader);
4799

48100
const lightTheme = 'arduino-theme';

0 commit comments

Comments
 (0)