@sussudio/platform
Version:
Internal APIs for VS Code's service injection the base services.
85 lines (84 loc) • 3.01 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isObject, isString } from '@sussudio/base/common/types.mjs';
import { localize } from 'vscode-nls.mjs';
export function localizeManifest(extensionManifest, translations, fallbackTranslations) {
try {
replaceNLStrings(extensionManifest, translations, fallbackTranslations);
} catch (error) {
console.error(error?.message ?? error);
/*Ignore Error*/
}
return extensionManifest;
}
/**
* This routine makes the following assumptions:
* The root element is an object literal
*/
function replaceNLStrings(extensionManifest, messages, originalMessages) {
const processEntry = (obj, key, command) => {
const value = obj[key];
if (isString(value)) {
const str = value;
const length = str.length;
if (length > 1 && str[0] === '%' && str[length - 1] === '%') {
const messageKey = str.substr(1, length - 2);
let translated = messages[messageKey];
// If the messages come from a language pack they might miss some keys
// Fill them from the original messages.
if (translated === undefined && originalMessages) {
translated = originalMessages[messageKey];
}
const message = typeof translated === 'string' ? translated : translated?.message;
// This branch returns ILocalizedString's instead of Strings so that the Command Palette can contain both the localized and the original value.
const original = originalMessages?.[messageKey];
const originalMessage = typeof original === 'string' ? original : original?.message;
if (!message) {
if (!originalMessage) {
console.warn(
`[${extensionManifest.name}]: ${localize(
'missingNLSKey',
"Couldn't find message for key {0}.",
messageKey,
)}`,
);
}
return;
}
if (
// if we are translating the title or category of a command
command &&
(key === 'title' || key === 'category') &&
// and the original value is not the same as the translated value
originalMessage &&
originalMessage !== message
) {
const localizedString = {
value: message,
original: originalMessage,
};
obj[key] = localizedString;
} else {
obj[key] = message;
}
}
} else if (isObject(value)) {
for (const k in value) {
if (value.hasOwnProperty(k)) {
k === 'commands' ? processEntry(value, k, true) : processEntry(value, k, command);
}
}
} else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
processEntry(value, i, command);
}
}
};
for (const key in extensionManifest) {
if (extensionManifest.hasOwnProperty(key)) {
processEntry(extensionManifest, key);
}
}
}