ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

How to set the language for diagnostic messages

Open qq865738120 opened this issue 1 year ago • 2 comments

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();

qq865738120 avatar Nov 20 '24 02:11 qq865738120

Does it work if you set the locale in the compiler options?

dsherret avatar Dec 21 '24 20:12 dsherret

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.

dsherret avatar Dec 21 '24 20:12 dsherret