Initial version of i18n-sample

To keep things simple, this doesn't use the nls-dev package. I think this demystifies a lot of things when you see what is actually going on behind the scenes.
This commit is contained in:
Nick Chen
2017-07-08 14:59:34 -07:00
parent ad39f8c2dd
commit 737d2db5b4
18 changed files with 234 additions and 0 deletions

2
i18n-sample/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
out
node_modules

17
i18n-sample/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
// A launch configuration that compiles the extension and then opens it inside a new window
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
"preLaunchTask": "npm"
}
]
}

9
i18n-sample/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,9 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
}
}

30
i18n-sample/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,30 @@
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
// A task runner that calls a custom npm script that compiles the extension.
{
"version": "0.1.0",
// we want to run npm
"command": "npm",
// the command is a shell script
"isShellCommand": true,
// show the output window only if unrecognized errors occur.
"showOutput": "silent",
// we run the custom script "compile" as defined in package.json
"args": ["run", "compile", "--loglevel", "silent"],
// The tsc compiler is started in watching mode
"isBackground": true,
// use the standard tsc in watch mode problem matcher to find compile problems in the output.
"problemMatcher": "$tsc-watch"
}

View File

@ -0,0 +1,9 @@
.vscode/**
.vscode-test/**
out/test/**
test/**
src/**
**/*.map
.gitignore
tsconfig.json
vsc-extension-quickstart.md

35
i18n-sample/README.md Normal file
View File

@ -0,0 +1,35 @@
# README
## This is the README for the "i18n-sample"
-------------------
This folder contains a sample VS code extension that shows how to use the
package.nls.json and vscode-nls library for localization. For this sample, it
shows two commands: Hello and Bye.
The text for the commands are defined in the top-level package.nls.json (for
English) and package.nls.ja.json (for Japanese). This is how you would localize
your text in the package.json file.
The actual text that is shown by the invocation of the commands are in
`src/extension.ts` and `src/command/sayBye.ts`. It shows how to use `nls.config`
and `nls.loadMessageBundle`.
# How to run locally
Localization values are only applied in the VSIX package.
1. Run `npm install` to bring in the dependencies.
1. Follow the steps at
https://code.visualstudio.com/docs/extensions/publish-extension to ensure
that you have installed vsce and have a publisher account.
1. Run `vsce package` to produce a .vsix file.
1. Install the .vsix file following the instructions at
https://code.visualstudio.com/docs/editor/extension-gallery#_install-from-a-vsix
1. Change your locale to Japanese by invoking "Configure Language" from the Command Palette.
# History
## 0.0.1:
Manually transform the calls to localize to illustrate explicitly what is going
on.

BIN
i18n-sample/demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 MiB

Binary file not shown.

46
i18n-sample/package.json Normal file
View File

@ -0,0 +1,46 @@
{
"name": "i18n-sample",
"displayName": "i18n-sample",
"description": "Sample that shows how to localize an extension",
"version": "0.0.1",
"publisher": "vazexqi",
"engines": {
"vscode": "^1.13.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:extension.sayHello",
"onCommand:extension.sayBye"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "%extension.sayHello.title%"
},
{
"command": "extension.sayBye",
"title": "%extension.sayBye.title%"
}
]
},
"scripts": {
"vscode:prepublish": "tsc -p ./ && mkdir -p out/src/command && cp src/*.json out/src && cp src/command/*.json out/src/command/",
"compile": "mkdir -p out/src/command && cp src/*.json out/src && cp src/command/*.json out/src/command/ && tsc -watch -p ./",
"clean": "rm -rf out",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"typescript": "^2.0.3",
"vscode": "^1.0.0",
"mocha": "^2.3.3",
"@types/node": "^6.0.40",
"@types/mocha": "^2.2.32"
},
"dependencies": {
"vscode-nls": "^2.0.2"
}
}

View File

@ -0,0 +1,4 @@
{
"extension.sayHello.title": "こんにちは",
"extension.sayBye.title": "さようなら"
}

View File

@ -0,0 +1,4 @@
{
"extension.sayHello.title": "Hello",
"extension.sayBye.title": "Bye"
}

View File

@ -0,0 +1 @@
["さようなら"]

View File

@ -0,0 +1,4 @@
{
"messages": ["Bye"],
"keys": ["sayBye.text"]
}

View File

@ -0,0 +1,15 @@
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
// This file shows how you would load the messageBundle from a separate file.
// const localize = nls.loadMessageBundle();
// becomes...
const localize: any = nls.loadMessageBundle(__filename);
export function sayByeCommand() {
// const message = localize('sayByetext', 'Bye')
// becomes...
const message = localize(0, null);
vscode.window.showInformationMessage(message);
}

View File

@ -0,0 +1 @@
["こんにちは"]

View File

@ -0,0 +1,4 @@
{
"messages": ["Hello"],
"keys": ["sayHello.text"]
}

View File

@ -0,0 +1,37 @@
// This file shows a few things:
// 0. How you *must* configure nls.config before your nls.loadMessageBundles
// (that could happen implicitly during import statements)
// 1. How to use the process.env.VSCODE_NLS_CONFIG to determine the current
// locale.
// 2. How the .ts file needs to be transformed to use vscode-nls.
//
// For step 2, there is a `processFile` function in
// https://github.com/Microsoft/vscode-nls-dev/blob/master/src/lib.ts#L642 that
// can do this for you but you have to hook it into your build pipeline
// See https://github.com/Microsoft/vscode/blob/master/build/gulpfile.extensions.js#L67
import * as nls from 'vscode-nls';
// const localize = nls.config(process.env.VSCODE_NLS_CONFIG)();
// becomes...
const localize: any = nls.config(process.env.VSCODE_NLS_CONFIG)(__filename);
import * as vscode from 'vscode';
import { sayByeCommand } from './command/sayBye';
export function activate(context: vscode.ExtensionContext) {
const helloCmd = vscode.commands.registerCommand('extension.sayHello', () => {
// localize('sayHello.text', 'Hello')
// becomes...
const message = localize(0, null);
vscode.window.showInformationMessage(message);
});
const byeCmd = vscode.commands.registerCommand(
'extension.sayBye',
sayByeCommand
);
context.subscriptions.push(helloCmd, byeCmd);
}
export function deactivate() {}

16
i18n-sample/tsconfig.json Normal file
View File

@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": "."
},
"exclude": [
"node_modules",
".vscode-test"
]
}