ts-morph
ts-morph copied to clipboard
How to set the language for diagnostic messages
I want to dynamically set the language of diagnostic information based on the user's region.
But I didn't find the corresponding API.
I know using ts.validateLocaleAndSetLanguage.
it doesn't work in ts-morph.
But it is indeed valid in typescript.
Is there any other method provided in ts-morph that I don't know about?
import { Project, ts } from 'ts-morph';
ts.validateLocaleAndSetLanguage('zh-cn', ts.sys);
export const project = new Project({
compilerOptions: {
target: ScriptTarget.ES2015,
},
});
const sourceFile = project.createSourceFile('test.ts', 'const a;', { overwrite: true });
const diagonstics = sourceFile.getPreEmitDiagnostics();
diagonstics[0].getMessageText();
Does it work if you set the locale in the compiler options?
Oh, actually, that wouldn't work because ts-morph doesn't distribute the locale files that are found in the typescript compiler.
Probably you can make it work by downloading the json file you need from the typescript package on npm and then providing the second argument to ts.validateLocaleAndSetLanguage that will load from that json file instead of whereever it's searching for. Look at the implementation of validateLocaleAndSetLanguage for details.