Skip to content

Commit 9d55dde

Browse files
* The true fix * some code clean up
1 parent 2e37ceb commit 9d55dde

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

src/vs/platform/extensionManagement/common/extensionNls.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import { IExtensionManifest } from 'vs/platform/extensions/common/extensions';
99
import { localize } from 'vs/nls';
1010

1111
export interface ITranslations {
12-
[key: string]: string | { message: string; comment: string[] };
12+
[key: string]: string | { message: string; comment: string[] } | undefined;
1313
}
1414

1515
export function localizeManifest(extensionManifest: IExtensionManifest, translations: ITranslations, fallbackTranslations?: ITranslations): IExtensionManifest {
1616
try {
1717
replaceNLStrings(extensionManifest, translations, fallbackTranslations);
1818
} catch (error) {
19+
console.error(error?.message ?? error);
1920
/*Ignore Error*/
2021
}
2122
return extensionManifest;
@@ -39,27 +40,32 @@ function replaceNLStrings(extensionManifest: IExtensionManifest, messages: ITran
3940
if (translated === undefined && originalMessages) {
4041
translated = originalMessages[messageKey];
4142
}
42-
const message: string | undefined = typeof translated === 'string' ? translated : translated.message;
43-
if (message !== undefined) {
44-
// This branch returns ILocalizedString's instead of Strings so that the Command Palette can contain both the localized and the original value.
45-
const original = originalMessages?.[messageKey];
46-
const originalMessage: string | undefined = typeof original === 'string' ? original : original?.message;
47-
if (
48-
// if we are translating the title or category of a command
49-
command && (key === 'title' || key === 'category') &&
50-
// and the original value is not the same as the translated value
51-
originalMessage && originalMessage !== message
52-
) {
53-
const localizedString: ILocalizedString = {
54-
value: message,
55-
original: originalMessage
56-
};
57-
obj[key] = localizedString;
58-
} else {
59-
obj[key] = message;
43+
const message: string | undefined = typeof translated === 'string' ? translated : translated?.message;
44+
45+
// This branch returns ILocalizedString's instead of Strings so that the Command Palette can contain both the localized and the original value.
46+
const original = originalMessages?.[messageKey];
47+
const originalMessage: string | undefined = typeof original === 'string' ? original : original?.message;
48+
49+
if (!message) {
50+
if (!originalMessage) {
51+
console.warn(`[${extensionManifest.name}]: ${localize('missingNLSKey', "Couldn't find message for key {0}.", messageKey)}`);
6052
}
53+
return;
54+
}
55+
56+
if (
57+
// if we are translating the title or category of a command
58+
command && (key === 'title' || key === 'category') &&
59+
// and the original value is not the same as the translated value
60+
originalMessage && originalMessage !== message
61+
) {
62+
const localizedString: ILocalizedString = {
63+
value: message,
64+
original: originalMessage
65+
};
66+
obj[key] = localizedString;
6167
} else {
62-
console.warn(`[${extensionManifest.name}]: ${localize('missingNLSKey', "Couldn't find message for key {0}.", messageKey)}`);
68+
obj[key] = message;
6369
}
6470
}
6571
} else if (isObject(value)) {

src/vs/platform/extensionManagement/test/common/extensionNls.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,42 @@ suite('Localize Manifest', () => {
107107
assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Testauthentifizierung');
108108
assert.strictEqual((localizedManifest.contributes?.configuration as IConfiguration).title, 'Testkonfiguration');
109109
});
110+
111+
test('replaces template strings - is best effort #164630', function () {
112+
const manifestWithTypo: IExtensionManifest = {
113+
name: 'test',
114+
publisher: 'test',
115+
version: '1.0.0',
116+
engines: {
117+
vscode: '*'
118+
},
119+
contributes: {
120+
authentication: [
121+
{
122+
id: 'test.authentication',
123+
// This not existing in the bundle shouldn't cause an error.
124+
label: '%doesnotexist%',
125+
}
126+
],
127+
commands: [
128+
{
129+
command: 'test.command',
130+
title: '%test.command.title%',
131+
category: '%test.command.category%'
132+
},
133+
],
134+
}
135+
};
136+
137+
const localizedManifest = localizeManifest(
138+
deepClone(manifestWithTypo),
139+
{
140+
'test.command.title': 'Test Command',
141+
'test.command.category': 'Test Category'
142+
});
143+
144+
assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');
145+
assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');
146+
assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, '%doesnotexist%');
147+
});
110148
});

0 commit comments

Comments
 (0)