Skip to content

Commit

Permalink
feat: respect browser language
Browse files Browse the repository at this point in the history
close #123
  • Loading branch information
johnsoncodehk committed Jul 5, 2023
1 parent d925ba3 commit f9fedcd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/monaco/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import onigasmWasm from 'onigasm/lib/onigasm.wasm?url'
import { editor, languages, Uri } from 'monaco-editor-core'
import * as volar from '@volar/monaco'
import { Store } from '../store'
import { createJsDelivrDtsHost } from 'volar-service-typescript'
import { getOrCreateModel } from './utils'
import type { CreateData } from './vue.worker'

Expand Down Expand Up @@ -76,15 +75,30 @@ export class WorkerHost {
}
}

async function fetchJson<T>(url: string) {
try {
const res = await fetch(url);
if (res.status === 200) {
return await res.json();
}
} catch {
// ignore
}
}

let disposeVue: undefined | (() => void)
export async function reloadVue(store: Store) {
disposeVue?.()

const locale = navigator.language.toLowerCase()
const tsLocalized = await fetchJson(`https://cdn.jsdelivr.net/npm/typescript/lib/${locale}/diagnosticMessages.generated.json`)
const worker = editor.createWebWorker<any>({
moduleId: 'vs/language/vue/vueWorker',
label: 'vue',
host: new WorkerHost(),
createData: {
locale: locale,
tsLocalized: tsLocalized,
tsconfig: store.getTsConfig?.() || {},
dependencies: !store.vueVersion
? {}
Expand Down
11 changes: 10 additions & 1 deletion src/monaco/vue.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { createLanguageService, createLanguageHost, createServiceEnvironment } f
import type { WorkerHost } from './env'

export interface CreateData {
locale: string
tsLocalized: any
tsconfig: {
compilerOptions?: ts.CompilerOptions
vueCompilerOptions?: Partial<VueCompilerOptions>
Expand All @@ -19,7 +21,7 @@ self.onmessage = () => {
worker.initialize(
(
ctx: monaco.worker.IWorkerContext<WorkerHost>,
{ tsconfig, dependencies }: CreateData
{ tsconfig, dependencies, locale, tsLocalized }: CreateData
) => {
const { options: compilerOptions } = ts.convertCompilerOptionsFromJson(
tsconfig?.compilerOptions || {},
Expand All @@ -30,6 +32,13 @@ self.onmessage = () => {
const jsDelivrFs = createJsDelivrFs(ctx.host.onFetchCdnFile)
const jsDelivrUriResolver = createJsDelivrUriResolver('/node_modules', dependencies)

if (locale) {
env.locale = locale
}
if (tsLocalized) {
host.getLocalizedDiagnosticMessages = () => tsLocalized
}

decorateServiceEnvironment(env, jsDelivrUriResolver, jsDelivrFs)

return createLanguageService(
Expand Down

0 comments on commit f9fedcd

Please sign in to comment.