Skip to content

Commit

Permalink
extracting nls constants to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aiday-mar committed Aug 20, 2024
1 parent fee1edb commit 1a36c61
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
},
{
Expand Down
6 changes: 5 additions & 1 deletion src/typings/vscode-globals-nls.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/vs/base/browser/defaultWorkerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}');`,
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions src/vs/nls.messages.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 6 additions & 2 deletions src/vs/nls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/issue/electron-main/issueMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/issue/electron-main/processMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/windows/electron-main/windowsMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
}
}
}, '*');
Expand Down

0 comments on commit 1a36c61

Please sign in to comment.