From 1a36c6126f0d35c8f0cfee01a79552856f290b91 Mon Sep 17 00:00:00 2001 From: Aiday Marlen Kyzy Date: Tue, 20 Aug 2024 17:48:48 +0200 Subject: [PATCH] extracting nls constants to functions --- .eslintrc.json | 2 +- src/typings/vscode-globals-nls.d.ts | 6 +++++- src/vs/base/browser/defaultWorkerFactory.ts | 5 +++-- src/vs/base/common/platform.ts | 2 +- src/vs/nls.messages.ts | 17 +++++++++++++++++ src/vs/nls.ts | 8 ++++++-- .../issue/electron-main/issueMainService.ts | 6 +++--- .../issue/electron-main/processMainService.ts | 6 +++--- .../windows/electron-main/windowsMainService.ts | 6 +++--- .../issue/electron-sandbox/issueFormService.ts | 6 +++--- .../browser/webWorkerExtensionHost.ts | 5 +++-- 11 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/vs/nls.messages.ts diff --git a/.eslintrc.json b/.eslintrc.json index c39a66311e4fc..73ad3a380343a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1024,7 +1024,7 @@ ] }, { - "target": "src/vs/{loader.d.ts,css.ts,css.build.ts,monaco.d.ts,nls.ts}", + "target": "src/vs/{loader.d.ts,css.ts,css.build.ts,monaco.d.ts,nls.messages.ts,nls.ts}", "restrictions": [] }, { diff --git a/src/typings/vscode-globals-nls.d.ts b/src/typings/vscode-globals-nls.d.ts index bc480767623d5..2b5d48e110c8c 100644 --- a/src/typings/vscode-globals-nls.d.ts +++ b/src/typings/vscode-globals-nls.d.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -// AMD2ESM mirgation relevant +// AMD2ESM migration relevant /** * NLS Globals: these need to be defined in all contexts that make @@ -24,10 +24,14 @@ declare global { * All NLS messages produced by `localize` and `localize2` calls * under `src/vs` translated to the language as indicated by * `_VSCODE_NLS_LANGUAGE`. + * + * Instead of accessing this global variable directly, use function getNLSMessages. */ var _VSCODE_NLS_MESSAGES: string[]; /** * The actual language of the NLS messages (e.g. 'en', de' or 'pt-br'). + * + * Instead of accessing this global variable directly, use function getNLSLanguage. */ var _VSCODE_NLS_LANGUAGE: string | undefined; } diff --git a/src/vs/base/browser/defaultWorkerFactory.ts b/src/vs/base/browser/defaultWorkerFactory.ts index 8f077b399aba0..b120d238013ef 100644 --- a/src/vs/base/browser/defaultWorkerFactory.ts +++ b/src/vs/base/browser/defaultWorkerFactory.ts @@ -8,6 +8,7 @@ import { onUnexpectedError } from 'vs/base/common/errors'; import { COI } from 'vs/base/common/network'; import { IWorker, IWorkerCallback, IWorkerFactory, logOnceWebWorkerWarning } from 'vs/base/common/worker/simpleWorker'; import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; +import { getNLSLanguage, getNLSMessages } from 'vs/nls'; // Reuse the trusted types policy defined from worker bootstrap // when available. @@ -80,8 +81,8 @@ export function getWorkerBootstrapUrl(scriptPath: string, label: string): string `/*${label}*/`, `globalThis.MonacoEnvironment = { baseUrl: '${workerBaseUrl}' };`, // VSCODE_GLOBALS: NLS - `globalThis._VSCODE_NLS_MESSAGES = ${JSON.stringify(globalThis._VSCODE_NLS_MESSAGES)};`, - `globalThis._VSCODE_NLS_LANGUAGE = ${JSON.stringify(globalThis._VSCODE_NLS_LANGUAGE)};`, + `globalThis._VSCODE_NLS_MESSAGES = ${JSON.stringify(getNLSMessages())};`, + `globalThis._VSCODE_NLS_LANGUAGE = ${JSON.stringify(getNLSLanguage())};`, `const ttPolicy = globalThis.trustedTypes?.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });`, `globalThis.workerttPolicy = ttPolicy;`, `importScripts(ttPolicy?.createScriptURL('${scriptPath}') ?? '${scriptPath}');`, diff --git a/src/vs/base/common/platform.ts b/src/vs/base/common/platform.ts index d931e64dfa9e6..8f16bcf1c8de9 100644 --- a/src/vs/base/common/platform.ts +++ b/src/vs/base/common/platform.ts @@ -104,7 +104,7 @@ else if (typeof navigator === 'object' && !isElectronRenderer) { _isMobile = _userAgent?.indexOf('Mobi') >= 0; _isWeb = true; // VSCODE_GLOBALS: NLS - _language = globalThis._VSCODE_NLS_LANGUAGE || LANGUAGE_DEFAULT; + _language = nls.getNLSLanguage() || LANGUAGE_DEFAULT; _locale = navigator.language.toLowerCase(); _platformLocale = _locale; } diff --git a/src/vs/nls.messages.ts b/src/vs/nls.messages.ts new file mode 100644 index 0000000000000..2e8b096686b9c --- /dev/null +++ b/src/vs/nls.messages.ts @@ -0,0 +1,17 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +/* + * This module exists so that the AMD build of the monaco editor can replace this with an async loader plugin. + * If you add new functions to this module make sure that they are also provided in the AMD build of the monaco editor. + */ + +export function getNLSMessages(): string[] { + return globalThis._VSCODE_NLS_MESSAGES; +} + +export function getNLSLanguage(): string | undefined { + return globalThis._VSCODE_NLS_LANGUAGE; +} diff --git a/src/vs/nls.ts b/src/vs/nls.ts index 5a546325fc7a2..d32b7e9e2f1ee 100644 --- a/src/vs/nls.ts +++ b/src/vs/nls.ts @@ -3,8 +3,12 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +// eslint-disable-next-line local/code-import-patterns +import { getNLSLanguage, getNLSMessages } from 'vs/nls.messages'; +export { getNLSLanguage, getNLSMessages }; + // VSCODE_GLOBALS: NLS -const isPseudo = globalThis._VSCODE_NLS_LANGUAGE === 'pseudo' || (typeof document !== 'undefined' && document.location && document.location.hash.indexOf('pseudo=true') >= 0); +const isPseudo = getNLSLanguage() === 'pseudo' || (typeof document !== 'undefined' && document.location && document.location.hash.indexOf('pseudo=true') >= 0); export interface ILocalizeInfo { key: string; @@ -88,7 +92,7 @@ export function localize(data: ILocalizeInfo | string /* | number when built */, */ function lookupMessage(index: number, fallback: string | null): string { // VSCODE_GLOBALS: NLS - const message = globalThis._VSCODE_NLS_MESSAGES?.[index]; + const message = getNLSMessages()?.[index]; if (typeof message !== 'string') { if (typeof fallback === 'string') { return fallback; diff --git a/src/vs/platform/issue/electron-main/issueMainService.ts b/src/vs/platform/issue/electron-main/issueMainService.ts index 2b42d690c91c8..fe0ac3fe32608 100644 --- a/src/vs/platform/issue/electron-main/issueMainService.ts +++ b/src/vs/platform/issue/electron-main/issueMainService.ts @@ -11,7 +11,7 @@ import { DisposableStore } from 'vs/base/common/lifecycle'; import { FileAccess } from 'vs/base/common/network'; import { IProcessEnvironment, isMacintosh } from 'vs/base/common/platform'; import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain'; -import { localize } from 'vs/nls'; +import { getNLSLanguage, getNLSMessages, localize } from 'vs/nls'; import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogMainService'; import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService'; import { IIssueMainService, OldIssueReporterData, OldIssueReporterWindowConfiguration } from 'vs/platform/issue/common/issue'; @@ -84,8 +84,8 @@ export class IssueMainService implements IIssueMainService { product, nls: { // VSCODE_GLOBALS: NLS - messages: globalThis._VSCODE_NLS_MESSAGES, - language: globalThis._VSCODE_NLS_LANGUAGE + messages: getNLSMessages(), + language: getNLSLanguage() } }); diff --git a/src/vs/platform/issue/electron-main/processMainService.ts b/src/vs/platform/issue/electron-main/processMainService.ts index 76da45d897cf9..edb8b2c94fe38 100644 --- a/src/vs/platform/issue/electron-main/processMainService.ts +++ b/src/vs/platform/issue/electron-main/processMainService.ts @@ -10,7 +10,7 @@ import { FileAccess } from 'vs/base/common/network'; import { IProcessEnvironment, isMacintosh } from 'vs/base/common/platform'; import { listProcesses } from 'vs/base/node/ps'; import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain'; -import { localize } from 'vs/nls'; +import { getNLSLanguage, getNLSMessages, localize } from 'vs/nls'; import { IDiagnosticsService, isRemoteDiagnosticError, PerformanceInfo, SystemInfo } from 'vs/platform/diagnostics/common/diagnostics'; import { IDiagnosticsMainService } from 'vs/platform/diagnostics/electron-main/diagnosticsMainService'; import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogMainService'; @@ -156,8 +156,8 @@ export class ProcessMainService implements IProcessMainService { product, nls: { // VSCODE_GLOBALS: NLS - messages: globalThis._VSCODE_NLS_MESSAGES, - language: globalThis._VSCODE_NLS_LANGUAGE + messages: getNLSMessages(), + language: getNLSLanguage() } }); diff --git a/src/vs/platform/windows/electron-main/windowsMainService.ts b/src/vs/platform/windows/electron-main/windowsMainService.ts index 070c7ae05d384..e7024636fa3b5 100644 --- a/src/vs/platform/windows/electron-main/windowsMainService.ts +++ b/src/vs/platform/windows/electron-main/windowsMainService.ts @@ -22,7 +22,7 @@ import { cwd } from 'vs/base/common/process'; import { extUriBiasedIgnorePathCase, isEqualAuthority, normalizePath, originalFSPath, removeTrailingPathSeparator } from 'vs/base/common/resources'; import { assertIsDefined } from 'vs/base/common/types'; import { URI } from 'vs/base/common/uri'; -import { localize } from 'vs/nls'; +import { getNLSLanguage, getNLSMessages, localize } from 'vs/nls'; import { IBackupMainService } from 'vs/platform/backup/electron-main/backup'; import { IEmptyWindowBackupInfo } from 'vs/platform/backup/node/backup'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; @@ -1446,8 +1446,8 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic nls: { // VSCODE_GLOBALS: NLS - messages: globalThis._VSCODE_NLS_MESSAGES, - language: globalThis._VSCODE_NLS_LANGUAGE + messages: getNLSMessages(), + language: getNLSLanguage() }, filesToOpenOrCreate: options.filesToOpen?.filesToOpenOrCreate, diff --git a/src/vs/workbench/contrib/issue/electron-sandbox/issueFormService.ts b/src/vs/workbench/contrib/issue/electron-sandbox/issueFormService.ts index 0ddec2689ddc8..aa218b5fe9125 100644 --- a/src/vs/workbench/contrib/issue/electron-sandbox/issueFormService.ts +++ b/src/vs/workbench/contrib/issue/electron-sandbox/issueFormService.ts @@ -7,7 +7,7 @@ import { safeInnerHtml } from 'vs/base/browser/dom'; import { DisposableStore } from 'vs/base/common/lifecycle'; import Severity from 'vs/base/common/severity'; import 'vs/css!./media/newIssueReporter'; -import { localize } from 'vs/nls'; +import { getNLSLanguage, getNLSMessages, localize } from 'vs/nls'; import { IMenuService, MenuId } from 'vs/platform/actions/common/actions'; import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IDialogService } from 'vs/platform/dialogs/common/dialogs'; @@ -110,8 +110,8 @@ export class IssueFormService2 implements IIssueFormService { product, nls: { // VSCODE_GLOBALS: NLS - messages: globalThis._VSCODE_NLS_MESSAGES, - language: globalThis._VSCODE_NLS_LANGUAGE + messages: getNLSMessages(), + language: getNLSLanguage() } }; diff --git a/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts b/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts index 8f4dc4a6b57a8..537d9ca906349 100644 --- a/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts +++ b/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts @@ -17,6 +17,7 @@ import { joinPath } from 'vs/base/common/resources'; import { URI } from 'vs/base/common/uri'; import { generateUuid } from 'vs/base/common/uuid'; import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc'; +import { getNLSLanguage, getNLSMessages } from 'vs/nls'; import { ILabelService } from 'vs/platform/label/common/label'; import { ILayoutService } from 'vs/platform/layout/browser/layoutService'; import { ILogService, ILoggerService } from 'vs/platform/log/common/log'; @@ -191,8 +192,8 @@ export class WebWorkerExtensionHost extends Disposable implements IExtensionHost workerUrl: require.toUrl(factoryModuleId), nls: { // VSCODE_GLOBALS: NLS - messages: globalThis._VSCODE_NLS_MESSAGES, - language: globalThis._VSCODE_NLS_LANGUAGE + messages: getNLSMessages(), + language: getNLSLanguage() } } }, '*');