Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/extensionManagement/common/extensionNls.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { isObject, isString } from '../../../base/common/types.js';
7
import { ILocalizedString } from '../../action/common/action.js';
8
import { IExtensionManifest } from '../../extensions/common/extensions.js';
9
import { localize } from '../../../nls.js';
10
import { ILogger } from '../../log/common/log.js';
11
12
export interface ITranslations {
13
[key: string]: string | { message: string; comment: string[] } | undefined;
14
}
15
16
export function localizeManifest(logger: ILogger, extensionManifest: IExtensionManifest, translations: ITranslations, fallbackTranslations?: ITranslations): IExtensionManifest {
17
try {
18
replaceNLStrings(logger, extensionManifest, translations, fallbackTranslations);
19
} catch (error) {
20
logger.error(error?.message ?? error);
21
/*Ignore Error*/
22
}
23
return extensionManifest;
24
}
25
26
/**
27
* This routine makes the following assumptions:
28
* The root element is an object literal
29
*/
30
function replaceNLStrings(logger: ILogger, extensionManifest: IExtensionManifest, messages: ITranslations, originalMessages?: ITranslations): void {
31
const processEntry = (obj: any, key: string | number, command?: boolean) => {
32
const value = obj[key];
33
if (isString(value)) {
34
const str = <string>value;
35
const length = str.length;
36
if (length > 1 && str[0] === '%' && str[length - 1] === '%') {
37
const messageKey = str.substr(1, length - 2);
38
let translated = messages[messageKey];
39
// If the messages come from a language pack they might miss some keys
40
// Fill them from the original messages.
41
if (translated === undefined && originalMessages) {
42
translated = originalMessages[messageKey];
43
}
44
const message: string | undefined = typeof translated === 'string' ? translated : translated?.message;
45
46
// This branch returns ILocalizedString's instead of Strings so that the Command Palette can contain both the localized and the original value.
47
const original = originalMessages?.[messageKey];
48
const originalMessage: string | undefined = typeof original === 'string' ? original : original?.message;
49
50
if (!message) {
51
if (!originalMessage) {
52
logger.warn(`[${extensionManifest.name}]: ${localize('missingNLSKey', "Couldn't find message for key {0}.", messageKey)}`);
53
}
54
return;
55
}
56
57
if (
58
// if we are translating the title or category of a command
59
command && (key === 'title' || key === 'category') &&
60
// and the original value is not the same as the translated value
61
originalMessage && originalMessage !== message
62
) {
63
const localizedString: ILocalizedString = {
64
value: message,
65
original: originalMessage
66
};
67
obj[key] = localizedString;
68
} else {
69
obj[key] = message;
70
}
71
}
72
} else if (isObject(value)) {
73
for (const k in value) {
74
if (value.hasOwnProperty(k)) {
75
k === 'commands' ? processEntry(value, k, true) : processEntry(value, k, command);
76
}
77
}
78
} else if (Array.isArray(value)) {
79
for (let i = 0; i < value.length; i++) {
80
processEntry(value, i, command);
81
}
82
}
83
};
84
85
for (const key in extensionManifest) {
86
if (extensionManifest.hasOwnProperty(key)) {
87
processEntry(extensionManifest, key);
88
}
89
}
90
}
91
92