Skip to content

Commit

Permalink
feat: expose TS localized languages
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 5, 2023
1 parent 97f698f commit a52dd14
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/monaco/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions src/monaco/vue.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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<WorkerMessage>) => {
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<WorkerHost>,
Expand Down
3 changes: 3 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface StoreState {
vueRuntimeURL: string
vueServerRendererURL: string
typescriptVersion: string
typescriptLocale: string | undefined
// used to force reset the sandbox
resetFlip: boolean
}
Expand Down Expand Up @@ -168,6 +169,7 @@ export class ReplStore implements Store {
vueRuntimeURL: this.defaultVueRuntimeURL,
vueServerRendererURL: this.defaultVueServerRendererURL,
typescriptVersion: 'latest',
typescriptLocale: undefined,
resetFlip: true,
})

Expand All @@ -187,6 +189,7 @@ export class ReplStore implements Store {
() => [
this.state.files[tsconfigFile]?.code,
this.state.typescriptVersion,
this.state.typescriptLocale,
],
() => reloadVue(this)
)
Expand Down

0 comments on commit a52dd14

Please sign in to comment.