Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Enable a mechanism for something to inject data into vscode-nls #42

Merged
merged 8 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 62 additions & 5 deletions src/browser/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,82 @@

import RAL from '../common/ral';

import { setPseudo, localize, Options, LocalizeInfo } from '../common/common';

import { setPseudo, localize, Options, LocalizeInfo, isString, MessageFormat, isNumber, format, LocalizeFunc } from '../common/common';
export { MessageFormat, BundleFormat, Options, LocalizeInfo, LocalizeFunc, LoadFunc, KeyInfo } from '../common/common';

export function loadMessageBundle(_file?: string) {
let nlsData: { [key: string]: string[] };
try {
// Requiring this file will be intercepted by VS Code and will contain actual NLS data.
// @ts-ignore
nlsData = require('vscode-nls-web-data');
} catch(e) {
console.error('Loading vscode-nls-web-data failed. Are you running this outside of VS Code? If so, you may need to intercept the import call with your bundled NLS data.');
nlsData = {};
}

interface InternalOptions {
locale: string | undefined;
language: string | undefined;
languagePackSupport: boolean;
cacheLanguageResolution: boolean;
messageFormat: MessageFormat;
languagePackId?: string;
cacheRoot?: string;
}

let options: InternalOptions;

export function loadMessageBundle(file?: string) {
if (!file) {
// No file. We are in dev mode. Return the default
// localize function.
return localize;
}
// Remove extension since we load json files.
if (file.endsWith('.js') || file.endsWith('.ts')) {
file = file.substring(0, file.length - 3);
}
if (file.startsWith('/')) {
file = file.substring(1);
}
if (nlsData && nlsData[file]) {
return createScopedLocalizeFunction(nlsData[file]);
}
return function (key: string | number | LocalizeInfo, message: string, ...args: any[]): string {
if (typeof key === 'number') {
throw new Error(`Browser implementation does currently not support externalized strings.`);
throw new Error('Externalized strings were not present in the environment.');
} else {
return localize(key, message, ...args);
}
};
}

export function config(options?: Options) {
// This API doesn't really do anything in practice because the message bundle _has_ to be loaded
// ahead of time via 'vscode-nls-web-data'.
export function config(opts?: Options) {
setPseudo(options?.locale?.toLowerCase() === 'pseudo');
return loadMessageBundle;
}

function createScopedLocalizeFunction(messages: string[]): LocalizeFunc {
TylerLeonhardt marked this conversation as resolved.
Show resolved Hide resolved
return function (key: any, message: string, ...args: any[]): string {
if (isNumber(key)) {
if (key >= messages.length) {
console.error(`Broken localize call found. Index out of bounds. Stacktrace is\n: ${(<any>new Error('')).stack}`);
return;
}
return format(messages[key], args);
} else {
if (isString(message)) {
console.warn(`Message ${message} didn't get externalized correctly.`);
return format(message, args);
} else {
console.error(`Broken localize call found. Stacktrace is\n: ${(<any>new Error('')).stack}`);
}
}
};
}

RAL.install(Object.freeze<RAL>({
loadMessageBundle: loadMessageBundle,
config: config
Expand Down
2 changes: 1 addition & 1 deletion src/browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"lib": [
"es5",
"es2015",
"dom"
],
"outDir": "../../lib/browser"
Expand Down
10 changes: 10 additions & 0 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ export function isDefined(value: any): boolean {
return typeof value !== 'undefined';
}

const toString = Object.prototype.toString;

export function isNumber(value: any): value is number {
return toString.call(value) === '[object Number]';
}

export function isString(value: any): value is string {
return toString.call(value) === '[object String]';
}

export let isPseudo = false;

export function setPseudo(pseudo: boolean) {
Expand Down
12 changes: 1 addition & 11 deletions src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@ import RAL from '../common/ral';

import {
format, localize, isDefined, setPseudo, isPseudo, MessageFormat, BundleFormat, Options, TranslationConfig, LanguageBundle, LocalizeFunc,
NlsBundle, MetaDataFile, MetadataHeader, I18nBundle, SingleFileJsonFormat, LoadFunc
NlsBundle, MetaDataFile, MetadataHeader, I18nBundle, SingleFileJsonFormat, LoadFunc, isString, isNumber
} from '../common/common';

export { MessageFormat, BundleFormat, Options, LocalizeInfo, LocalizeFunc, LoadFunc, KeyInfo } from '../common/common';

const toString = Object.prototype.toString;

function isNumber(value: any): value is number {
return toString.call(value) === '[object Number]';
}

function isString(value: any): value is string {
return toString.call(value) === '[object String]';
}

function isBoolean(value: any): value is boolean {
return value === true || value === false;
}
Expand Down