npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monaco-editor-typescript-locales

v0.11.0

Published

A utility to translate diagnostic messages from the Monaco Editor TypeScript worker into different locales

Downloads

105

Readme

npm version

monaco-editor-typescript-locales

Problem

The locale of the Monaco editor can be configured during load, e.g.:

// set locale to German
require.config({ "vs/nls": { availableLanguages: { "*": "de" } } });

However, currently this only translates the Monaco editor UI itself and not the diagnostic messages from the Typescript worker that provides language functionality to JavaScript and TypeScript in the editor.

Below is an example of what this means, where the Monaco editor locale is set to German and an error exists in the JavaScript code. As you can see, the text for editor actions (ie from the Monaco editor UI) is correctly translated but the error message (ie diagnostic messages from the TypeScript worker) are still in English.

Monaco editor localised to German with error that is not translated

You can try this for yourself using the latest version of the Monaco editor using this interactive example, which is based on the official localisation example from the Monaco editor repo.

The interactive example above even includes the following code to set the JS compiler options locale to German, however this does nothing currently as the messages are hard coded to English in the Typescript worker:

const jsDefaults = monaco.languages.typescript.javascriptDefaults;
jsDefaults.setCompilerOptions({
  ...jsDefaults.getCompilerOptions(),
  locale: "de", // this does nothing currently
});

Solution

This package extends Monaco and adds functionality to translate JavaScript or TypeScript diagnostic messages based on the compiler options locale property.

Below is an example of what this looks like: Monaco editor with translated diagnostic message

ie the diagnostic message is now translated to German to match the Monaco editor locale.

Getting Started

Installation

npm install monaco-editor-typescript-locales

Usage

import { register } from "monaco-editor-typescript-locales";

// you load your monaco instance as normal
...

// then you register the monaco instance with this package
register(monaco);

// and then you can set locales for the JS and TS compiler options
// which will be used to translate the diagnostic messages, e.g. for JS:
const jsDefaults = monaco.languages.typescript.javascriptDefaults;
jsDefaults.setCompilerOptions({
  ...jsDefaults.getCompilerOptions(),
  locale: "de", // this will translate the JS diagnostic messages to German
});

The above will only set the translation language for the JS models, if you want the same language for TS models you will need to set it separately, e.g.:

const tsDefaults = monaco.languages.typescript.typescriptDefaults;
tsDefaults.setCompilerOptions({
  ...tsDefaults.getCompilerOptions(),
  locale: "de", // this will translate the TS diagnostic messages to German
});

Supported Locales

| | Code | Description | | --- | ----- | -------------------- | | 1 | cs | Czech | | 2 | de | German | | 3 | en | English | | 4 | es | Spanish | | 5 | fr | French | | 6 | it | Italian | | 7 | ja | Japanese | | 8 | ko | Korean | | 9 | pl | Polish | | 10 | pt-br | Brazilian Portuguese | | 11 | ru | Russian | | 12 | tr | Turkish | | 13 | zh-cn | Chinese (China) | | 14 | zh-tw | Chinese (Taiwan) |

Generated from TypeScript version 5.4.5

Notes

  • Since languages can be set independently for JS and TS, you will need to set the same language for both if you want them to match.
  • This assumes Typescript diagnostic messages content does not change across versions after being introduced into the TS compiler, which means this package just needs to be updated when new versions of Typescript are released and all previous versions are supported ie the messages are backward compatible.
  • This is a stop gap solution until the Monaco team implements a native Typescript worker localisation solution. The assumption of this package is this future functionality will hook into the language compiler options locale option, and so when that happens it should be a simple case of removing this package and the register calls to migrate to the official solution.
  • This only translates Typescript worker diagnostic messages, not the Monaco editor UI itself or messages from other languages or sources (e.g. linters or parsers).
  • Not all Monaco locales might be supported by Typescript and vice versa, so for some languages you might see a mix of translated and untranslated text even when using this package and setting the Monaco locale.