Skip to content

Bring in localization into the API #158213

Description

@TylerLeonhardt

vscode-nls and it's build time companion vscode-nls-dev has existed for quite a while now... and in its original design it was very filepath driven. We felt this pain when bundling extensions became the norm and introduced a webpack loader and bundler that baked filepaths into source code...

That mitigation of rewriting JS code worked... but this all became more complicated when talking about the web because the web has no "nice" way of loading strings synchronously. And even if there was such a nice thing, we wouldn't want synchronous loading from the internet... so instead we are moving in a direction where Core is responsible for loading the bundle of strings for an extension, asynchronously, before an extension is loaded and then those strings are provided to the extension via an API.

declare module 'vscode' {
	export namespace env {
		export function nls(message: string, ...args: string[]): string;
		export function nls(options: { message: string; comment: string[] }, ...args: string[]): string;
		export interface nls {
			contents: { [key: string ]: string };
			uri: Uri;
		}
	}
}

In addition to this, we will have an additional field in the package.json: nlsBundleLocation which is at the root next to main and browser. This is useful for:

  • being explicit in an extension that supports localized strings
  • allows VS Code to not have to "guess" where the localized strings are in the extension (this is especially important in web where we don't have a filesystem that we can crawl)

Important definitions:

  • message: the message parameter is the English string. This is used for a few things:
    • It's the string used when VS Code is in the default (English) language
    • It's the fallback string when a bundle doesn't contain a translation for a particular string
    • It's the key in the bundle (which is essentially just a big dictionary) who's value will be the translated version of itself in whatever language is chosen
  • options: This follows the same format we have when you wanna add a comment in a package.nls.json. The comment array will be an array of comments that will be included in the exported XLF files (the files sent to translators). The message is the same as defined above.
  • i18n.contents: This will contain the key-value pairs of the strings that VS Code loaded. This is useful if you want to pass a subprocess, a web worker, or a webview the contents of the bundle so that that environment can have all the translations that are currently being used
  • i18n.uri: This will contain the Uri to the bundle that was loaded. This is useful if you want to pass a subprocess, a web worker, or a webview the Uri of the bundle so that that environment can load the translations that are currently being used.

Along side of this work, we will ship a very small helper package called @vscode/nls or something like that that can be given the contents or uri value and load the bundle and handle string interpolation. This library will really only be used for subprocesses/workers/webviews that can't depend on the vscode API directly.

@vscode/nls

Current API proposal:

export function config(config: { bundleUri: string; } | { bundleContents: string | i18nJsonFormat; }): void;

export function nls(message: string, ...args: string[]): string;
export function nls(options: { message: string; comment: string[] }, ...args: string[]): string;

@vscode/nls-dev

This is a CLI & library for managing the usages of vscode.env.nls. It has two main usecases:

  • Export: export the English strings out of the code and put them in a familiar JSON or XLF format
  • Import: imports XLF files and turns them into the respective package.nls.*.json and bundle.nls.*.json files

It currently has three commands:

  • Export
  cli.js export [args] <pattern..>    Export strings from source files

Positionals:
  pattern  Glob pattern of ts files to include  [array] [required] [default: []]

Options:
      --version  Show version number                                   [boolean]
      --help     Show help                                             [boolean]
  -o, --outDir   Output directory                        [string] [default: "."]
  • Generate XLF
  cli.js generate-xlf [args]          Generate an XLF file from a JSON i18n file

Options:
      --version             Show version number                        [boolean]
      --help                Show help                                  [boolean]
  -p, --packageNlsJsonPath  JSON i18n file to generate XLF from
                                                             [string] [required]
  -b, --i18nBundleJsonPath  JSON i18n file to generate XLF from
                                                             [string] [required]
  -o, --outFile             Output file                      [string] [required]
  • Import XLF
  cli.js import-xlf [args] <xlfPath>  Import an XLF file into a JSON i18n file

Positionals:
  xlfPath  Glob pattern of ts files to include               [string] [required]

Options:
      --version  Show version number                                   [boolean]
      --help     Show help                                             [boolean]
  -o, --outDir   Output directory that will contain the i18n.<locale>.json file
                 and the package.nls.json file           [string] [default: "."]

Metadata

Metadata

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions