diff --git a/i18n-sample/.gitignore b/i18n-sample/.gitignore new file mode 100644 index 00000000..8e5962ee --- /dev/null +++ b/i18n-sample/.gitignore @@ -0,0 +1,2 @@ +out +node_modules \ No newline at end of file diff --git a/i18n-sample/.vscode/launch.json b/i18n-sample/.vscode/launch.json new file mode 100644 index 00000000..30625e88 --- /dev/null +++ b/i18n-sample/.vscode/launch.json @@ -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" + } + ] +} diff --git a/i18n-sample/.vscode/settings.json b/i18n-sample/.vscode/settings.json new file mode 100644 index 00000000..d1371333 --- /dev/null +++ b/i18n-sample/.vscode/settings.json @@ -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 + } +} \ No newline at end of file diff --git a/i18n-sample/.vscode/tasks.json b/i18n-sample/.vscode/tasks.json new file mode 100644 index 00000000..1e37eb7b --- /dev/null +++ b/i18n-sample/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/i18n-sample/.vscodeignore b/i18n-sample/.vscodeignore new file mode 100644 index 00000000..5ff3c193 --- /dev/null +++ b/i18n-sample/.vscodeignore @@ -0,0 +1,9 @@ +.vscode/** +.vscode-test/** +out/test/** +test/** +src/** +**/*.map +.gitignore +tsconfig.json +vsc-extension-quickstart.md diff --git a/i18n-sample/README.md b/i18n-sample/README.md new file mode 100644 index 00000000..f7bab00d --- /dev/null +++ b/i18n-sample/README.md @@ -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. \ No newline at end of file diff --git a/i18n-sample/demo.gif b/i18n-sample/demo.gif new file mode 100644 index 00000000..8b191ce1 Binary files /dev/null and b/i18n-sample/demo.gif differ diff --git a/i18n-sample/i18n-sample-0.0.1.vsix b/i18n-sample/i18n-sample-0.0.1.vsix new file mode 100644 index 00000000..b4345ef8 Binary files /dev/null and b/i18n-sample/i18n-sample-0.0.1.vsix differ diff --git a/i18n-sample/package.json b/i18n-sample/package.json new file mode 100644 index 00000000..7db6cc67 --- /dev/null +++ b/i18n-sample/package.json @@ -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" + } +} diff --git a/i18n-sample/package.nls.ja.json b/i18n-sample/package.nls.ja.json new file mode 100644 index 00000000..21ddd66d --- /dev/null +++ b/i18n-sample/package.nls.ja.json @@ -0,0 +1,4 @@ +{ + "extension.sayHello.title": "こんにちは", + "extension.sayBye.title": "さようなら" +} diff --git a/i18n-sample/package.nls.json b/i18n-sample/package.nls.json new file mode 100644 index 00000000..a4f15be4 --- /dev/null +++ b/i18n-sample/package.nls.json @@ -0,0 +1,4 @@ +{ + "extension.sayHello.title": "Hello", + "extension.sayBye.title": "Bye" +} diff --git a/i18n-sample/src/command/sayBye.nls.ja.json b/i18n-sample/src/command/sayBye.nls.ja.json new file mode 100644 index 00000000..491dfe9d --- /dev/null +++ b/i18n-sample/src/command/sayBye.nls.ja.json @@ -0,0 +1 @@ +["さようなら"] diff --git a/i18n-sample/src/command/sayBye.nls.json b/i18n-sample/src/command/sayBye.nls.json new file mode 100644 index 00000000..05365ab3 --- /dev/null +++ b/i18n-sample/src/command/sayBye.nls.json @@ -0,0 +1,4 @@ +{ + "messages": ["Bye"], + "keys": ["sayBye.text"] +} diff --git a/i18n-sample/src/command/sayBye.ts b/i18n-sample/src/command/sayBye.ts new file mode 100644 index 00000000..2258d66d --- /dev/null +++ b/i18n-sample/src/command/sayBye.ts @@ -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); +} diff --git a/i18n-sample/src/extension.nls.ja.json b/i18n-sample/src/extension.nls.ja.json new file mode 100644 index 00000000..5206d922 --- /dev/null +++ b/i18n-sample/src/extension.nls.ja.json @@ -0,0 +1 @@ +["こんにちは"] diff --git a/i18n-sample/src/extension.nls.json b/i18n-sample/src/extension.nls.json new file mode 100644 index 00000000..419c5cc8 --- /dev/null +++ b/i18n-sample/src/extension.nls.json @@ -0,0 +1,4 @@ +{ + "messages": ["Hello"], + "keys": ["sayHello.text"] +} diff --git a/i18n-sample/src/extension.ts b/i18n-sample/src/extension.ts new file mode 100644 index 00000000..d205b425 --- /dev/null +++ b/i18n-sample/src/extension.ts @@ -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() {} diff --git a/i18n-sample/tsconfig.json b/i18n-sample/tsconfig.json new file mode 100644 index 00000000..11282c9a --- /dev/null +++ b/i18n-sample/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "outDir": "out", + "lib": [ + "es6" + ], + "sourceMap": true, + "rootDir": "." + }, + "exclude": [ + "node_modules", + ".vscode-test" + ] +} \ No newline at end of file