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

Commit

Permalink
Revert "Enable a mechanism for something to inject data into vscode-n…
Browse files Browse the repository at this point in the history
…ls (#42)" due to breaking extensions.

This reverts commit 0e2dbdb.
  • Loading branch information
TylerLeonhardt committed Aug 29, 2022
1 parent 9b02994 commit e1991bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 74 deletions.
67 changes: 5 additions & 62 deletions src/browser/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,25 @@

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

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

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 = {};
}
import { setPseudo, localize, Options, LocalizeInfo } from '../common/common';

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

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

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]);
}
export function loadMessageBundle(_file?: string) {
return function (key: string | number | LocalizeInfo, message: string, ...args: any[]): string {
if (typeof key === 'number') {
throw new Error('Externalized strings were not present in the environment.');
throw new Error(`Browser implementation does currently not support externalized strings.`);
} else {
return localize(key, message, ...args);
}
};
}

// 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) {
export function config(options?: Options) {
setPseudo(options?.locale?.toLowerCase() === 'pseudo');
return loadMessageBundle;
}

function createScopedLocalizeFunction(messages: string[]): LocalizeFunc {
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": [
"es2015",
"es5",
"dom"
],
"outDir": "../../lib/browser"
Expand Down
10 changes: 0 additions & 10 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ 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: 11 additions & 1 deletion src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import RAL from '../common/ral';

import {
format, localize, isDefined, setPseudo, isPseudo, MessageFormat, BundleFormat, Options, TranslationConfig, LanguageBundle, LocalizeFunc,
NlsBundle, MetaDataFile, MetadataHeader, I18nBundle, SingleFileJsonFormat, LoadFunc, isString, isNumber
NlsBundle, MetaDataFile, MetadataHeader, I18nBundle, SingleFileJsonFormat, LoadFunc
} 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

0 comments on commit e1991bd

Please sign in to comment.