Localization as part of the API #159
Replies: 2 comments 2 replies
-
@isidorn one discussion that @philliphoff and I had yesterday relates to strings that are identical in, say, English, but not in other languages. These could be differentiated by using the However, suppose you are converting an existing extension that has been using |
Beta Was this translation helpful? Give feedback.
-
@isidorn could you fix the typos around calling the api |
Beta Was this translation helpful? Give feedback.
-
This iteration we are introducing a new API that is designed to be used by extensions to localize strings. This replaces the previous
vscode-nls
&vscode-nls-dev
modules that were used for localization in the past. Those will continue to work but will not receive any more features. The new API & tooling around it is designed to be familiar but also easier to use and more flexible. Additionally, including it as part of the API enables us to provide support for localizing your extensions for both VS Code for the desktop and VS Code for the web.Localization for VS Code extensions has 4 important parts:
The new
vscode.l10n
APIThe
vscode.l10n
Proposed API is a new namespace that is available. It provides a single functiont
that can be used to declare that a string should be localized. The function can be called with a string or an object with amessage
property. The function will return the localized string if it exists, otherwise it will return the original string. The function also supports arguments that can be used to format the string and comments that can be used to provide context for translators.A simple example of using the new API:
In this example, the string
Hello in {0}!
will be localized if a localization bundle exists for the current language. The{0}
will be replaced with the current language (en
by default, orfr
for French,pt-br
for Brazilian Portuguese, etc). If no localization bundle exists, the string will be returned as-is and formatted with the arguments. You may be wondering where these localization bundles come from. I'll cover that in the next section.The
vscode.10n
API also provides access to the bundle of localized strings or the URI to the bundle of strings. This is intended to be used in subprocess scenarios which I will cover later.The
l10n
property should be a relative path to the folder that contains the localization bundles.The
@vscode/l10n-dev
moduleThe
@vscode/l10n-dev
module is a new module that is used to generate the localization bundles. You can either use it as a command line tool or as a library. Both are used to generate the localization bundles by scanning forvscode.l10n.t(..)
calls from the source code provided.Here's a simple example of using the command line tool:
This will place a
bundle.l10n.json
file in the./l10n
folder. From there you can make abundle.l10n.LOCALE.json
file for each locale you want to support. For example, let's say that generates the followingbundle.l10n.json
file:If you wanted to support French, you would make this:
The
@vscode/l10n-dev
module can also be used to generate XLF files. The VS Code team generates XLF files that we then give to translators at Microsoft. The translators then give us back the translated XLF files. We then use the@vscode/l10n-dev
module to generate the localized bundles from the translated XLF files. At some point, we plan on writing a blog post that goes into more detail about our localization process as a whole.The
@vscode/l10n
moduleSince the
vscode.l10n
API is only available in the extension host, it cannot be used in subprocesses. For this reason, we have created a new module that can be used in subprocesses to load the localization bundles. The module is called@vscode/l10n
and it can be used like so:The idea is that your extension-side code who is responsible for spinning up the subprocesses will use the
vscode.l10n.contents
orvscode.l10n.uri
APIs to pass the bundle or the URI of the bundle to the subprocesses. The subprocesses can then use the@vscode/l10n
module to load the bundle and use thet
function to translate strings. Thet
function used by the@vscode/l10n
module will also be picked up in the@vscode/l10n-dev
module so that the strings can be extracted and localized using one process.The
package.nls.json
fileNothing has changed with respect to the
package.nls.json
file. It is still used to declare the default strings that should be localized and should be next to thepackage.json
. You still can havepackage.nls.LOCALE.json
(where LOCALE is something likede
orzh-cn
) and the strings declared in that file will be picked up first if the user has set VS Code to that locale. A small example:Your
package.json
:Your
package.nls.json
:Your
package.nls.de.json
:Summary
There's certainly a lot here to digest, but hopefully this gives you an idea of the direction we're trying to take with localization in VS Code extensions.
If you're interested in a full example, we have a sample that you can check out here.
If you have questions or feedback, please let us know in the following places:
vscode.l10n
vscode-l10n
repo (home of the@vscode/l10n-dev
and@vscode/l10n
modules)Try it out and let us know what you think!
fyi @TylerLeonhardt
Beta Was this translation helpful? Give feedback.
All reactions