UNPKG

5.79 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.LanguageQuickPickService = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const nls_1 = require("../../common/nls");
22const localization_1 = require("../../common/i18n/localization");
23const quick_input_1 = require("../quick-input");
24const window_service_1 = require("../window/window-service");
25let LanguageQuickPickService = class LanguageQuickPickService {
26 async pickDisplayLanguage() {
27 const quickInput = this.quickInputService.createQuickPick();
28 const installedItems = await this.getInstalledLanguages();
29 const quickInputItems = [
30 {
31 type: 'separator',
32 label: nls_1.nls.localizeByDefault('Installed')
33 },
34 ...installedItems
35 ];
36 quickInput.items = quickInputItems;
37 quickInput.busy = true;
38 const selected = installedItems.find(item => nls_1.nls.isSelectedLocale(item.languageId));
39 if (selected) {
40 quickInput.activeItems = [selected];
41 }
42 quickInput.placeholder = nls_1.nls.localizeByDefault('Configure Display Language');
43 quickInput.show();
44 this.getAvailableLanguages().then(availableItems => {
45 if (availableItems.length > 0) {
46 quickInputItems.push({
47 type: 'separator',
48 label: nls_1.nls.localizeByDefault('Available')
49 });
50 const installed = new Set(installedItems.map(e => e.languageId));
51 for (const available of availableItems) {
52 // Exclude already installed languages
53 if (!installed.has(available.languageId)) {
54 quickInputItems.push(available);
55 }
56 }
57 quickInput.items = quickInputItems;
58 }
59 }).finally(() => {
60 quickInput.busy = false;
61 });
62 return new Promise(resolve => {
63 quickInput.onDidAccept(async () => {
64 var _a;
65 const selectedItem = quickInput.selectedItems[0];
66 if (selectedItem) {
67 // Some language quick pick items want to install additional languages
68 // We have to await that before returning the selected locale
69 await ((_a = selectedItem.execute) === null || _a === void 0 ? void 0 : _a.call(selectedItem));
70 resolve(selectedItem);
71 }
72 else {
73 resolve(undefined);
74 }
75 quickInput.hide();
76 });
77 quickInput.onDidHide(() => {
78 resolve(undefined);
79 });
80 });
81 }
82 async getInstalledLanguages() {
83 const languageInfos = await this.localizationProvider.getAvailableLanguages();
84 const items = [];
85 const en = {
86 languageId: 'en',
87 languageName: 'English',
88 localizedLanguageName: 'English'
89 };
90 languageInfos.push(en);
91 for (const language of languageInfos.filter(e => !!e.languageId)) {
92 items.push(this.createLanguageQuickPickItem(language));
93 }
94 return items;
95 }
96 async getAvailableLanguages() {
97 return [];
98 }
99 createLanguageQuickPickItem(language) {
100 let label;
101 let description;
102 const languageName = language.localizedLanguageName || language.languageName;
103 const id = language.languageId;
104 const idLabel = id + (nls_1.nls.isSelectedLocale(id) ? ` (${nls_1.nls.localizeByDefault('Current')})` : '');
105 if (languageName) {
106 label = languageName;
107 description = idLabel;
108 }
109 else {
110 label = idLabel;
111 }
112 return {
113 label,
114 description,
115 languageId: id,
116 languageName: language.languageName,
117 localizedLanguageName: language.localizedLanguageName
118 };
119 }
120};
121(0, tslib_1.__decorate)([
122 (0, inversify_1.inject)(quick_input_1.QuickInputService),
123 (0, tslib_1.__metadata)("design:type", Object)
124], LanguageQuickPickService.prototype, "quickInputService", void 0);
125(0, tslib_1.__decorate)([
126 (0, inversify_1.inject)(localization_1.AsyncLocalizationProvider),
127 (0, tslib_1.__metadata)("design:type", Object)
128], LanguageQuickPickService.prototype, "localizationProvider", void 0);
129(0, tslib_1.__decorate)([
130 (0, inversify_1.inject)(window_service_1.WindowService),
131 (0, tslib_1.__metadata)("design:type", Object)
132], LanguageQuickPickService.prototype, "windowService", void 0);
133LanguageQuickPickService = (0, tslib_1.__decorate)([
134 (0, inversify_1.injectable)()
135], LanguageQuickPickService);
136exports.LanguageQuickPickService = LanguageQuickPickService;
137//# sourceMappingURL=language-quick-pick-service.js.map
\No newline at end of file