Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add nlsConfig to webWorker #192808 #199258

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/vs/base/worker/workerMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,27 @@
});
}

// stores the current language
let __locale__: string | undefined = undefined;

function configureAMDLoader() {
interface NLSConfig {
availableLanguages: { [key: string]: string };
}
const nlsConfig: { 'vs/nls'?: NLSConfig } = {};
if (__locale__ && __locale__ !== 'en') {
nlsConfig['vs/nls'] = {
availableLanguages: {
'*': __locale__,
},
};
}
require.config({
baseUrl: monacoBaseUrl,
catchError: true,
trustedTypesPolicy,
amdModulesPattern: /^vs\//
amdModulesPattern: /^vs\//,
...nlsConfig,
});
}

Expand Down Expand Up @@ -140,6 +155,14 @@
let isFirstMessage = true;
const beforeReadyMessages: MessageEvent[] = [];
globalThis.onmessage = (message: MessageEvent) => {
if (isFirstMessage && !__locale__) {
const locale = message.data?.match(/^worker:init-locale:(.+)$/);
if (locale?.[1]) {
__locale__ = locale[1];
return;
}
}

if (!isFirstMessage) {
beforeReadyMessages.push(message);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ export class WebWorkerExtensionHost extends Disposable implements IExtensionHost
}
COI.addSearchParam(suffixSearchParams, true, true);

const suffix = `?${suffixSearchParams.toString()}`;
let suffix = `?${suffixSearchParams.toString()}`;
Copy link
Member

@TylerLeonhardt TylerLeonhardt Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like you could just do this:

Suggested change
let suffix = `?${suffixSearchParams.toString()}`;
if (!platform.Language.isDefaultVariant()) {
suffixSearchParams.set('locale', platform.language);
}
const suffix = `?${suffixSearchParams.toString()}`;

and get rid of the other changes in this file.


const iframeModulePath = 'vs/workbench/services/extensions/worker/webWorkerExtensionHostIframe.html';
const locale = localStorage.getItem('vscode.nls.locale') || navigator.language.toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is running on the main side, I think you can just use language or Language.value():

export const language = _language;
export namespace Language {
export function value(): string {
return language;
}

the implementation of this will grab the local storage value or navigator value.

if (platform.isWeb) {
const webEndpointUrlTemplate = this._productService.webEndpointUrlTemplate;
const commit = this._productService.commit;
Expand All @@ -109,12 +110,19 @@ export class WebWorkerExtensionHost extends Disposable implements IExtensionHost
const res = new URL(`${baseUrl}/out/${iframeModulePath}${suffix}`);
res.searchParams.set('parentOrigin', mainWindow.origin);
res.searchParams.set('salt', stableOriginUUID);
if (locale !== 'en') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, also in platform there is a LANGUAGE_DEFAULT which you can use instead of hardcoding 'en'

res.searchParams.set('locale', locale);
}
return res.toString();
}

console.warn(`The web worker extension host is started in a same-origin iframe!`);
}

if (locale !== 'en') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too

suffixSearchParams.set('locale', locale);
suffix = `?${suffixSearchParams.toString()}`;
}
const relativeExtensionHostIframeSrc = FileAccess.asBrowserUri(iframeModulePath);
return `${relativeExtensionHostIframeSrc.toString(true)}${suffix}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
const name = searchParams.get('debugged') ? 'DebugWorkerExtensionHost' : 'WorkerExtensionHost';
const parentOrigin = searchParams.get('parentOrigin') || window.origin;
const salt = searchParams.get('salt');
const locale = searchParams.get('locale');

(async function () {
const hostnameValidationMarker = 'v--';
Expand Down Expand Up @@ -73,6 +74,10 @@
}

const worker = new Worker(workerUrl, { name });
if (locale) {
// send before `vs/workbench/api/worker/extensionHostWorker`load event.
worker.postMessage(`worker:init-locale:${locale}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the worker cannot read the localstorge directly and the iframe may not be in the same domain name as the main application, the method used is to obtain the language in the host, pass it to the iframe, and then pass it to the worker.

I don't know if this will be strange

}
worker.postMessage('vs/workbench/api/worker/extensionHostWorker');
const nestedWorkers = new Map();

Expand Down