Repository navigation
Description
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: themessageparameter 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. Thecommentarray will be an array of comments that will be included in the exported XLF files (the files sent to translators). Themessageis 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 usedi18n.uri: This will contain theUrito 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.*.jsonandbundle.nls.*.jsonfiles
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: "."]
Activity
wandyezj commentedon Aug 16, 2022
Is it possible to see which language is currently being used via the API?
Does anything need to be changed in an extension based on Left to Right languages (e.g. English) verses Right to Left languages (e.g. Arabic)? The main case I tend to see is layout, for example if there is a custom rendered view.
TylerLeonhardt commentedon Aug 16, 2022
wandyezj
VS Code doesn't support RTL: #11770 and I suspect that idea of LTR vs RTL would come in as an API separate to this proposal.
wandyezj commentedon Aug 16, 2022
I see:
vscode.env.languagevscode.end.languagesync up with language translation thevscode.env.i18nAPI returns?loads ${value}orHello ${name}, would you like ${item}?(Including the interpolated piece matters for translation, simply concatenating doesn't translate well).With
vscode.env.languageit should be simple enough for a developer to create their own function to map the provided language code to a direction if the feature is needed for the extension.TylerLeonhardt commentedon Aug 16, 2022
Yes.
vscode.env.i18n()will return the string in whatevervscode.env.languageis set to so long as there was a translation available for that string in that language. And fallback to English if not.Yes. This is what happens today and will continue to happen.
Not at this time. We are aligning to what
vscode-nlsalready does but in theory something like that could be added in the future.Yes but I think there's enough value here:
{0}vscode.env.i18ncalls still work) - this is especially important in the web where we must load strings via an async fetch callvscode-nlsuses) but additional formats could be addedTylerLeonhardt commentedon Sep 8, 2022
Updates from UX sync:
nlsisn't better thani18n... maybel10n?tfunction oni18ninstead of this half function/half object thing(key, ...args)or({ key: ..., args: ..., comments: ...})TylerLeonhardt commentedon Sep 8, 2022
Ok I have been playing with this a lot today… and here’s what I’d like to run:
vscode.l10n- The API for translating strings in your extension’s code@vscode/l10n-dev- The tooling used for extracting l10n strings from vscode extensions and working with XLF files@vscode/l10n- The library used for loading the translations into subprocesses of your extensionpackage.l10n.json- The file used for translating static contributions in your extension’s package.jsonvscode.l10nThis contains the following definition:
The new namespace keeps the
i18n.t(...)that we will see all over code short and sweet.@vscode/l10n-devTooling used for extracting l10n strings from vscode extensions and working with XLF files. Here's what the CLI looks like:
There is also a programatic way to interact with the tooling via JS.
@vscode/l10nLibrary used for loading the translations into subprocesses of your extension. It has an API of:
tfunctionconfigfunction that looks like this that will receive:package.l10n.json> NOTE: for backwards compatibility,package.nls.jsonis also supported.This file is used for translating static contributions in your extension'spackage.json.deferred
Niceness
The api being
vscode.l10n.t()pairs nicely with@vscode/l10nbecause you either do this in your extension code:or in a subprocess you do
and the API becomes the same:
TylerLeonhardt commentedon Sep 28, 2022
Moving to Oct and marking finalization. This has 2 TPIs associated with it:
#161870
https://github.com/microsoft/vscode-internalbacklog/issues/3167
jrieken commentedon Oct 4, 2022
api-notes
i10n.uriisundefinedt, eg{0}syntaxtfunction supportformat2TylerLeonhardt commentedon Oct 4, 2022
This isn't so simple. What does the change to the API proposal look like to support this?
Some scenarios:
I think that:
l10n.t('asdf {0}', 'world');looks nicer thanl10n.t('asdf {0}', ['world']);but I do want to support the object syntaxjrieken commentedon Oct 5, 2022
I agree with that. Could we be more strict and spec the var-args to not be
anybutstringornumber? That would allow you to disambiguate them and IMO it won't hurt any user of the var-args variantalexr00 commentedon Oct 11, 2022
I adopted this in GHPRI, ran into an issue, and discussed with TylerLeonhardt. Posting here for API discussion:
I ended up with the following compile error:
I assumed this was just a breaking API change so updated my loc API usage to this:
TylerLeonhardt saw this and commented that it's not the usual pattern for templates with a variable number of arguments.
jrieken commentedon Oct 11, 2022
export function t(message: string, ...args: Array<string | number>): string;overload to acceptundefinedor don't allow undefined to surface in messagescomment: string | string[]