src/vs/workbench/api/common/extHostLocalizationService.ts
109 LOC · 39 covered · 70 uncovered · 7 ranges · 38 concepts · 1 introducers · 33 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
extHostExtensionService.ts ×64
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { LANGUAGE_DEFAULT } from '../../../base/common/platform.js';
import { format2 } from '../../../base/common/strings.js';
import { URI } from '../../../base/common/uri.js';
import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
import { ILogService } from '../../../platform/log/common/log.js';
import { ExtHostLocalizationShape, IStringDetails, MainContext, MainThreadLocalizationShape } from './extHost.protocol.js';
import { IExtHostInitDataService } from './extHostInitDataService.js';
import { IExtHostRpcService } from './extHostRpcService.js';
export class ExtHostLocalizationService implements ExtHostLocalizationShape {
readonly _serviceBrand: undefined;
private readonly _proxy: MainThreadLocalizationShape;
private readonly currentLanguage: string;
private readonly isDefaultLanguage: boolean;
private readonly bundleCache: Map<string, { contents: { [key: string]: string }; uri: URI }> = new Map();
constructor(
@IExtHostInitDataService initData: IExtHostInitDataService,
@IExtHostRpcService rpc: IExtHostRpcService,
@ILogService private readonly logService: ILogService
) {
this._proxy = rpc.getProxy(MainContext.MainThreadLocalization);
this.currentLanguage = initData.environment.appLanguage;
this.isDefaultLanguage = this.currentLanguage === LANGUAGE_DEFAULT;
}
getMessage(extensionId: string, details: IStringDetails): string {
const { message, args, comment } = details;
if (this.isDefaultLanguage) {
return format2(message, (args ?? {}));
}
let key = message;
if (comment && comment.length > 0) {
key += `/${Array.isArray(comment) ? comment.join('') : comment}`;
}
const str = this.bundleCache.get(extensionId)?.contents[key];
if (!str) {
this.logService.warn(`Using default string since no string found in i18n bundle that has the key: ${key}`);
}
return format2(str ?? message, (args ?? {}));
}
getBundle(extensionId: string): { [key: string]: string } | undefined {
return this.bundleCache.get(extensionId)?.contents;
}
getBundleUri(extensionId: string): URI | undefined {
return this.bundleCache.get(extensionId)?.uri;
}
async initializeLocalizedMessages(extension: IExtensionDescription): Promise<void> {
if (this.isDefaultLanguage
|| (!extension.l10n && !extension.isBuiltin)
) {
return;
}
if (this.bundleCache.has(extension.identifier.value)) {
return;
}
let contents: { [key: string]: string } | undefined;
const bundleUri = await this.getBundleLocation(extension);
if (!bundleUri) {
this.logService.error(`No bundle location found for extension ${extension.identifier.value}`);
return;
}
try {
const response = await this._proxy.$fetchBundleContents(bundleUri);
const result = JSON.parse(response);
// 'contents.bundle' is a well-known key in the language pack json file that contains the _code_ translations for the extension
contents = extension.isBuiltin ? result.contents?.bundle : result;
} catch (e) {
this.logService.error(`Failed to load translations for ${extension.identifier.value} from ${bundleUri}: ${e.message}`);
return;
}
if (contents) {
this.bundleCache.set(extension.identifier.value, {
contents,
uri: bundleUri
});
}
}
private async getBundleLocation(extension: IExtensionDescription): Promise<URI | undefined> {
if (extension.isBuiltin) {
const uri = await this._proxy.$fetchBuiltInBundleUri(extension.identifier.value, this.currentLanguage);
return URI.revive(uri);
}
return extension.l10n
? URI.joinPath(extension.extensionLocation, extension.l10n, `bundle.l10n.${this.currentLanguage}.json`)
: undefined;
}
export const IExtHostLocalizationService = createDecorator<IExtHostLocalizationService>('IExtHostLocalizationService');
export interface IExtHostLocalizationService extends ExtHostLocalizationService { }