Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set the language for diagnostic messages #1585

Open
qq865738120 opened this issue Nov 20, 2024 · 2 comments
Open

How to set the language for diagnostic messages #1585

qq865738120 opened this issue Nov 20, 2024 · 2 comments

Comments

@qq865738120
Copy link

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();
@dsherret
Copy link
Owner

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

@dsherret
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants