Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/extensionManagement/test/common/extensionNls.test.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 assert from 'assert';
7
import { deepClone } from '../../../../base/common/objects.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
9
import { ILocalizedString } from '../../../action/common/action.js';
10
import { IConfigurationNode } from '../../../configuration/common/configurationRegistry.js';
11
import { localizeManifest } from '../../common/extensionNls.js';
12
import { IExtensionManifest } from '../../../extensions/common/extensions.js';
13
import { NullLogger } from '../../../log/common/log.js';
14
15
const manifest: IExtensionManifest = {
16
name: 'test',
17
publisher: 'test',
18
version: '1.0.0',
19
engines: {
20
vscode: '*'
21
},
22
contributes: {
23
commands: [
24
{
25
command: 'test.command',
26
title: '%test.command.title%',
27
category: '%test.command.category%'
28
},
29
],
30
authentication: [
31
{
32
id: 'test.authentication',
33
label: '%test.authentication.label%',
34
}
35
],
36
configuration: {
37
// to ensure we test another "title" property
38
title: '%test.configuration.title%',
39
properties: {
40
'test.configuration': {
41
type: 'string',
42
description: 'not important',
43
}
44
}
45
}
46
}
47
};
48
49
suite('Localize Manifest', () => {
50
const store = ensureNoDisposablesAreLeakedInTestSuite();
51
test('replaces template strings', function () {
52
const localizedManifest = localizeManifest(
53
store.add(new NullLogger()),
54
deepClone(manifest),
55
{
56
'test.command.title': 'Test Command',
57
'test.command.category': 'Test Category',
58
'test.authentication.label': 'Test Authentication',
59
'test.configuration.title': 'Test Configuration',
60
}
61
);
62
63
assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');
64
assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');
65
assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Test Authentication');
66
assert.strictEqual((localizedManifest.contributes?.configuration as IConfigurationNode).title, 'Test Configuration');
67
});
68
69
test('replaces template strings with fallback if not found in translations', function () {
70
const localizedManifest = localizeManifest(
71
store.add(new NullLogger()),
72
deepClone(manifest),
73
{},
74
{
75
'test.command.title': 'Test Command',
76
'test.command.category': 'Test Category',
77
'test.authentication.label': 'Test Authentication',
78
'test.configuration.title': 'Test Configuration',
79
}
80
);
81
82
assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');
83
assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');
84
assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Test Authentication');
85
assert.strictEqual((localizedManifest.contributes?.configuration as IConfigurationNode).title, 'Test Configuration');
86
});
87
88
test('replaces template strings - command title & categories become ILocalizedString', function () {
89
const localizedManifest = localizeManifest(
90
store.add(new NullLogger()),
91
deepClone(manifest),
92
{
93
'test.command.title': 'Befehl test',
94
'test.command.category': 'Testkategorie',
95
'test.authentication.label': 'Testauthentifizierung',
96
'test.configuration.title': 'Testkonfiguration',
97
},
98
{
99
'test.command.title': 'Test Command',
100
'test.command.category': 'Test Category',
101
'test.authentication.label': 'Test Authentication',
102
'test.configuration.title': 'Test Configuration',
103
}
104
);
105
106
const title = localizedManifest.contributes?.commands?.[0].title as ILocalizedString;
107
const category = localizedManifest.contributes?.commands?.[0].category as ILocalizedString;
108
assert.strictEqual(title.value, 'Befehl test');
109
assert.strictEqual(title.original, 'Test Command');
110
assert.strictEqual(category.value, 'Testkategorie');
111
assert.strictEqual(category.original, 'Test Category');
112
113
// Everything else stays as a string.
114
assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Testauthentifizierung');
115
assert.strictEqual((localizedManifest.contributes?.configuration as IConfigurationNode).title, 'Testkonfiguration');
116
});
117
118
test('replaces template strings - is best effort #164630', function () {
119
const manifestWithTypo: IExtensionManifest = {
120
name: 'test',
121
publisher: 'test',
122
version: '1.0.0',
123
engines: {
124
vscode: '*'
125
},
126
contributes: {
127
authentication: [
128
{
129
id: 'test.authentication',
130
// This not existing in the bundle shouldn't cause an error.
131
label: '%doesnotexist%',
132
}
133
],
134
commands: [
135
{
136
command: 'test.command',
137
title: '%test.command.title%',
138
category: '%test.command.category%'
139
},
140
],
141
}
142
};
143
144
const localizedManifest = localizeManifest(
145
store.add(new NullLogger()),
146
deepClone(manifestWithTypo),
147
{
148
'test.command.title': 'Test Command',
149
'test.command.category': 'Test Category'
150
});
151
152
assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');
153
assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');
154
assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, '%doesnotexist%');
155
});
156
});
157
158