-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
Configure Display Language
command
- Loading branch information
Showing
9 changed files
with
278 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/core/src/browser/i18n/language-quick-pick-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2022 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { nls } from '../../common/nls'; | ||
import { AsyncLocalizationProvider, LanguageInfo } from '../../common/i18n/localization'; | ||
import { QuickInputService, QuickPickItem, QuickPickSeparator } from '../quick-input'; | ||
import { WindowService } from '../window/window-service'; | ||
|
||
export interface LanguageQuickPickItem extends QuickPickItem { | ||
languageId: string | ||
execute?(): Promise<void> | ||
} | ||
|
||
@injectable() | ||
export class LanguageQuickPickService { | ||
|
||
@inject(QuickInputService) protected readonly quickInputService: QuickInputService; | ||
@inject(AsyncLocalizationProvider) protected readonly localizationProvider: AsyncLocalizationProvider; | ||
@inject(WindowService) protected readonly windowService: WindowService; | ||
|
||
async pickDisplayLanguage(): Promise<string | undefined> { | ||
const quickInput = this.quickInputService.createQuickPick(); | ||
const installedItems = await this.getInstalledLanguages(); | ||
const quickInputItems: (QuickPickItem | QuickPickSeparator)[] = [ | ||
{ | ||
type: 'separator', | ||
label: nls.localize('theia/core/installedLanguages', 'Installed languages') | ||
}, | ||
...installedItems | ||
]; | ||
quickInput.items = quickInputItems; | ||
quickInput.busy = true; | ||
const selected = installedItems.find(item => nls.isSelectedLocale(item.languageId)); | ||
if (selected) { | ||
quickInput.activeItems = [selected]; | ||
} | ||
quickInput.placeholder = nls.localizeByDefault('Configure Display Language'); | ||
quickInput.show(); | ||
|
||
this.getAvailableLanguages().then(availableItems => { | ||
if (availableItems.length > 0) { | ||
quickInputItems.push({ | ||
type: 'separator', | ||
label: nls.localize('theia/core/availableLanguages', 'Available languages') | ||
}); | ||
for (const available of availableItems) { | ||
// Exclude already installed languages | ||
if (!installedItems.some(e => e.languageId === available.languageId)) { | ||
quickInputItems.push(available); | ||
} | ||
} | ||
quickInput.items = quickInputItems; | ||
} | ||
}).finally(() => { | ||
quickInput.busy = false; | ||
}); | ||
|
||
return new Promise(resolve => { | ||
quickInput.onDidAccept(async () => { | ||
const selectedItem = quickInput.selectedItems[0] as LanguageQuickPickItem; | ||
// Some language quick pick items want to install additional languages | ||
// We have to await that before returning the selected locale | ||
await selectedItem.execute?.(); | ||
resolve(selectedItem.languageId); | ||
}); | ||
quickInput.onDidHide(() => { | ||
resolve(undefined); | ||
}); | ||
}); | ||
} | ||
|
||
protected async getInstalledLanguages(): Promise<LanguageQuickPickItem[]> { | ||
const languageInfos = await this.localizationProvider.getAvailableLanguages(); | ||
const items: LanguageQuickPickItem[] = []; | ||
const en: LanguageInfo = { | ||
languageId: 'en', | ||
languageName: 'English', | ||
localizedLanguageName: 'English' | ||
}; | ||
languageInfos.push(en); | ||
for (const language of languageInfos.filter(e => !!e.languageId)) { | ||
items.push(this.createLanguageQuickPickItem(language)); | ||
} | ||
return items; | ||
} | ||
|
||
protected async getAvailableLanguages(): Promise<LanguageQuickPickItem[]> { | ||
return []; | ||
} | ||
|
||
protected createLanguageQuickPickItem(language: LanguageInfo): LanguageQuickPickItem { | ||
let label: string; | ||
let description: string | undefined; | ||
const languageName = language.localizedLanguageName || language.languageName; | ||
const id = language.languageId; | ||
const idLabel = id + (nls.isSelectedLocale(id) ? ` (${nls.localizeByDefault('Current')})` : ''); | ||
if (languageName) { | ||
label = languageName; | ||
description = idLabel; | ||
} else { | ||
label = idLabel; | ||
} | ||
return { | ||
label, | ||
description, | ||
languageId: id | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/vsx-registry/src/browser/vsx-language-quick-pick-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2022 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { LanguageQuickPickItem, LanguageQuickPickService } from '@theia/core/lib/browser/i18n/language-quick-pick-service'; | ||
import { RequestContext, RequestService } from '@theia/core/shared/@theia/request'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { LanguageInfo } from '@theia/core/lib/common/i18n/localization'; | ||
import { PluginPackage, PluginServer } from '@theia/plugin-ext'; | ||
import { OVSXClientProvider } from '../common/ovsx-client-provider'; | ||
import { VSXSearchEntry } from '@theia/ovsx-client'; | ||
import { VSXExtensionUri } from '../common/vsx-extension-uri'; | ||
|
||
@injectable() | ||
export class VSXLanguageQuickPickService extends LanguageQuickPickService { | ||
|
||
@inject(OVSXClientProvider) | ||
protected readonly clientProvider: OVSXClientProvider; | ||
|
||
@inject(RequestService) | ||
protected readonly requestService: RequestService; | ||
|
||
@inject(PluginServer) | ||
protected readonly pluginServer: PluginServer; | ||
|
||
protected override async getAvailableLanguages(): Promise<LanguageQuickPickItem[]> { | ||
const client = await this.clientProvider(); | ||
const searchResult = await client.search({ | ||
category: 'Language Packs', | ||
sortBy: 'downloadCount', | ||
sortOrder: 'desc', | ||
size: 20 | ||
}); | ||
if (searchResult.error) { | ||
throw new Error('Error while loading available languages: ' + searchResult.error); | ||
} | ||
|
||
const extensionLanguages = await Promise.all( | ||
searchResult.extensions.map(async extension => ({ | ||
extension, | ||
languages: await this.loadExtensionLanguages(extension) | ||
})) | ||
); | ||
|
||
const languages = new Map<string, { language: LanguageInfo, extensionUri: string }>(); | ||
|
||
for (const extension of extensionLanguages) { | ||
for (const localizationContribution of extension.languages) { | ||
if (!languages.has(localizationContribution.languageId)) { | ||
languages.set(localizationContribution.languageId, { | ||
language: localizationContribution, | ||
extensionUri: VSXExtensionUri.toUri(extension.extension.name, extension.extension.namespace).toString() | ||
}); | ||
} | ||
} | ||
} | ||
const items: LanguageQuickPickItem[] = []; | ||
|
||
for (const { language, extensionUri } of Array.from(languages.values())) { | ||
const item: LanguageQuickPickItem = { | ||
...this.createLanguageQuickPickItem(language), | ||
execute: async () => { | ||
await this.pluginServer.deploy(extensionUri); | ||
} | ||
}; | ||
items.push(item); | ||
} | ||
return items; | ||
} | ||
|
||
protected async loadExtensionLanguages(extension: VSXSearchEntry): Promise<LanguageInfo[]> { | ||
// When searching for extensions on ovsx, we don't receive the `manifest` property. | ||
// This property is only set when querying a specific extension. | ||
// To improve performance, we assume that a manifest exists at `/package.json`. | ||
const downloadUrl = extension.files.download; | ||
const parentUrl = downloadUrl.substring(0, downloadUrl.lastIndexOf('/')); | ||
const manifestUrl = parentUrl + '/package.json'; | ||
const manifestRequest = await this.requestService.request({ url: manifestUrl }); | ||
const manifestContent = RequestContext.asJson<PluginPackage>(manifestRequest); | ||
const localizations = manifestContent.contributes?.localizations; | ||
if (localizations) { | ||
return localizations.map(e => ({ | ||
languageId: e.languageId, | ||
languageName: e.languageName, | ||
localizedLanguageName: e.localizedLanguageName, | ||
languagePack: true | ||
})); | ||
} | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters