diff --git a/src/monaco/env.ts b/src/monaco/env.ts index 53593a01..63879ac9 100644 --- a/src/monaco/env.ts +++ b/src/monaco/env.ts @@ -131,6 +131,12 @@ export async function reloadVue(store: Store) { } } +export interface WorkerMessage { + event: 'init' + tsVersion: string + tsLocale?: string +} + export function loadMonacoEnv(store: Store) { ;(self as any).MonacoEnvironment = { async getWorker(_: any, label: string) { @@ -142,7 +148,11 @@ export function loadMonacoEnv(store: Store) { resolve() } }) - worker.postMessage({ event: 'init', tsVersion: store.state.typescriptVersion }) + worker.postMessage({ + event: 'init', + tsVersion: store.state.typescriptVersion, + tsLocale: store.state.typescriptLocale, + } satisfies WorkerMessage) }) await init return worker diff --git a/src/monaco/vue.worker.ts b/src/monaco/vue.worker.ts index 61777a77..75b90aac 100644 --- a/src/monaco/vue.worker.ts +++ b/src/monaco/vue.worker.ts @@ -12,7 +12,7 @@ import { createLanguageHost, createServiceEnvironment, } from '@volar/monaco/worker' -import type { WorkerHost } from './env' +import type { WorkerHost, WorkerMessage } from './env' export interface CreateData { tsconfig: { @@ -22,22 +22,28 @@ export interface CreateData { dependencies: {} } -const locale = navigator.language.toLowerCase() +let locale: string let ts: typeof import('typescript') let tsLocalized: any -self.onmessage = async (msg) => { +self.onmessage = async (msg: MessageEvent) => { if (msg.data?.event === 'init') { + if (msg.data.tsLocale) { + locale = msg.data.tsLocale + } + ;[ts, tsLocalized] = await Promise.all([ importTsFromCdn(msg.data.tsVersion), - fetchJson( - `https://cdn.jsdelivr.net/npm/typescript@${msg.data.tsVersion}/lib/${locale}/diagnosticMessages.generated.json` - ), + locale && + fetchJson( + `https://cdn.jsdelivr.net/npm/typescript@${msg.data.tsVersion}/lib/${locale}/diagnosticMessages.generated.json` + ), ]) self.postMessage('inited') return } + worker.initialize( ( ctx: monaco.worker.IWorkerContext, diff --git a/src/store.ts b/src/store.ts index 1b0518c9..422b245e 100644 --- a/src/store.ts +++ b/src/store.ts @@ -86,6 +86,7 @@ export interface StoreState { vueRuntimeURL: string vueServerRendererURL: string typescriptVersion: string + typescriptLocale: string | undefined // used to force reset the sandbox resetFlip: boolean } @@ -168,6 +169,7 @@ export class ReplStore implements Store { vueRuntimeURL: this.defaultVueRuntimeURL, vueServerRendererURL: this.defaultVueServerRendererURL, typescriptVersion: 'latest', + typescriptLocale: undefined, resetFlip: true, }) @@ -187,6 +189,7 @@ export class ReplStore implements Store { () => [ this.state.files[tsconfigFile]?.code, this.state.typescriptVersion, + this.state.typescriptLocale, ], () => reloadVue(this) )