-
Notifications
You must be signed in to change notification settings - Fork 29.5k
/
localizationsActions.ts
119 lines (103 loc) · 4.41 KB
/
localizationsActions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize, localize2 } from 'vs/nls';
import { IQuickInputService, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Action2, MenuId } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ILanguagePackItem, ILanguagePackService } from 'vs/platform/languagePacks/common/languagePacks';
import { ILocaleService } from 'vs/workbench/services/localization/common/locale';
import { IExtensionsWorkbenchService } from 'vs/workbench/contrib/extensions/common/extensions';
export class ConfigureDisplayLanguageAction extends Action2 {
public static readonly ID = 'workbench.action.configureLocale';
constructor() {
super({
id: ConfigureDisplayLanguageAction.ID,
title: localize2('configureLocale', "Configure Display Language"),
menu: {
id: MenuId.CommandPalette
},
metadata: {
description: localize2('configureLocaleDescription', "Changes the locale of VS Code based on installed language packs. Common languages include French, Chinese, Spanish, Japanese, German, Korean, and more.")
}
});
}
public async run(accessor: ServicesAccessor): Promise<void> {
const languagePackService: ILanguagePackService = accessor.get(ILanguagePackService);
const quickInputService: IQuickInputService = accessor.get(IQuickInputService);
const localeService: ILocaleService = accessor.get(ILocaleService);
const extensionWorkbenchService: IExtensionsWorkbenchService = accessor.get(IExtensionsWorkbenchService);
const installedLanguages = await languagePackService.getInstalledLanguages();
const qp = quickInputService.createQuickPick<ILanguagePackItem>();
qp.matchOnDescription = true;
qp.placeholder = localize('chooseLocale', "Select Display Language");
if (installedLanguages?.length) {
const items: Array<ILanguagePackItem | IQuickPickSeparator> = [{ type: 'separator', label: localize('installed', "Installed") }];
qp.items = items.concat(this.withMoreInfoButton(installedLanguages));
}
const disposables = new DisposableStore();
const source = new CancellationTokenSource();
disposables.add(qp.onDispose(() => {
source.cancel();
disposables.dispose();
}));
const installedSet = new Set<string>(installedLanguages?.map(language => language.id!) ?? []);
languagePackService.getAvailableLanguages().then(availableLanguages => {
const newLanguages = availableLanguages.filter(l => l.id && !installedSet.has(l.id));
if (newLanguages.length) {
qp.items = [
...qp.items,
{ type: 'separator', label: localize('available', "Available") },
...this.withMoreInfoButton(newLanguages)
];
}
qp.busy = false;
});
disposables.add(qp.onDidAccept(async () => {
const selectedLanguage = qp.activeItems[0] as ILanguagePackItem | undefined;
if (selectedLanguage) {
qp.hide();
await localeService.setLocale(selectedLanguage);
}
}));
disposables.add(qp.onDidTriggerItemButton(async e => {
qp.hide();
if (e.item.extensionId) {
await extensionWorkbenchService.open(e.item.extensionId);
}
}));
qp.show();
qp.busy = true;
}
private withMoreInfoButton(items: ILanguagePackItem[]): ILanguagePackItem[] {
for (const item of items) {
if (item.extensionId) {
item.buttons = [{
tooltip: localize('moreInfo', "More Info"),
iconClass: 'codicon-info'
}];
}
}
return items;
}
}
export class ClearDisplayLanguageAction extends Action2 {
public static readonly ID = 'workbench.action.clearLocalePreference';
public static readonly LABEL = localize2('clearDisplayLanguage', "Clear Display Language Preference");
constructor() {
super({
id: ClearDisplayLanguageAction.ID,
title: ClearDisplayLanguageAction.LABEL,
menu: {
id: MenuId.CommandPalette
}
});
}
public async run(accessor: ServicesAccessor): Promise<void> {
const localeService: ILocaleService = accessor.get(ILocaleService);
await localeService.clearLocalePreference();
}
}