forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlocalizations.ts
176 lines (157 loc) · 7.91 KB
/
localizations.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createHash } from 'crypto';
import { distinct, equals } from 'vs/base/common/arrays';
import { Queue } from 'vs/base/common/async';
import { Disposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { join } from 'vs/base/common/path';
import { Promises } from 'vs/base/node/pfs';
import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
import { IExtensionIdentifier, IExtensionManagementService, ILocalExtension } from 'vs/platform/extensionManagement/common/extensionManagement';
import { areSameExtensions } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { ILocalizationsService, isValidLocalization } from 'vs/platform/localizations/common/localizations';
import { ILogService } from 'vs/platform/log/common/log';
interface ILanguagePack {
hash: string;
extensions: {
extensionIdentifier: IExtensionIdentifier;
version: string;
}[];
translations: { [id: string]: string };
}
export class LocalizationsService extends Disposable implements ILocalizationsService {
declare readonly _serviceBrand: undefined;
private readonly cache: LanguagePacksCache;
constructor(
@IExtensionManagementService private readonly extensionManagementService: IExtensionManagementService,
@INativeEnvironmentService environmentService: INativeEnvironmentService,
@ILogService private readonly logService: ILogService
) {
super();
this.cache = this._register(new LanguagePacksCache(environmentService, logService));
this.extensionManagementService.registerParticipant({
postInstall: async (extension: ILocalExtension): Promise<void> => {
return this.postInstallExtension(extension);
},
postUninstall: async (extension: ILocalExtension): Promise<void> => {
return this.postUninstallExtension(extension);
}
});
}
async getLanguageIds(): Promise<string[]> {
const languagePacks = await this.cache.getLanguagePacks();
// Contributed languages are those installed via extension packs, so does not include English
const languages = ['en', ...Object.keys(languagePacks)];
return distinct(languages);
}
private async postInstallExtension(extension: ILocalExtension): Promise<void> {
if (extension && extension.manifest && extension.manifest.contributes && extension.manifest.contributes.localizations && extension.manifest.contributes.localizations.length) {
this.logService.info('Adding language packs from the extension', extension.identifier.id);
await this.update();
}
}
private async postUninstallExtension(extension: ILocalExtension): Promise<void> {
const languagePacks = await this.cache.getLanguagePacks();
if (Object.keys(languagePacks).some(language => languagePacks[language] && languagePacks[language].extensions.some(e => areSameExtensions(e.extensionIdentifier, extension.identifier)))) {
this.logService.info('Removing language packs from the extension', extension.identifier.id);
await this.update();
}
}
async update(): Promise<boolean> {
const [current, installed] = await Promise.all([this.cache.getLanguagePacks(), this.extensionManagementService.getInstalled()]);
const updated = await this.cache.update(installed);
return !equals(Object.keys(current), Object.keys(updated));
}
}
class LanguagePacksCache extends Disposable {
private languagePacks: { [language: string]: ILanguagePack } = {};
private languagePacksFilePath: string;
private languagePacksFileLimiter: Queue<any>;
private initializedCache: boolean | undefined;
constructor(
@INativeEnvironmentService environmentService: INativeEnvironmentService,
@ILogService private readonly logService: ILogService
) {
super();
this.languagePacksFilePath = join(environmentService.userDataPath, 'languagepacks.json');
this.languagePacksFileLimiter = new Queue();
}
getLanguagePacks(): Promise<{ [language: string]: ILanguagePack }> {
// if queue is not empty, fetch from disk
if (this.languagePacksFileLimiter.size || !this.initializedCache) {
return this.withLanguagePacks()
.then(() => this.languagePacks);
}
return Promise.resolve(this.languagePacks);
}
update(extensions: ILocalExtension[]): Promise<{ [language: string]: ILanguagePack }> {
return this.withLanguagePacks(languagePacks => {
Object.keys(languagePacks).forEach(language => delete languagePacks[language]);
this.createLanguagePacksFromExtensions(languagePacks, ...extensions);
}).then(() => this.languagePacks);
}
private createLanguagePacksFromExtensions(languagePacks: { [language: string]: ILanguagePack }, ...extensions: ILocalExtension[]): void {
for (const extension of extensions) {
if (extension && extension.manifest && extension.manifest.contributes && extension.manifest.contributes.localizations && extension.manifest.contributes.localizations.length) {
this.createLanguagePacksFromExtension(languagePacks, extension);
}
}
Object.keys(languagePacks).forEach(languageId => this.updateHash(languagePacks[languageId]));
}
private createLanguagePacksFromExtension(languagePacks: { [language: string]: ILanguagePack }, extension: ILocalExtension): void {
const extensionIdentifier = extension.identifier;
const localizations = extension.manifest.contributes && extension.manifest.contributes.localizations ? extension.manifest.contributes.localizations : [];
for (const localizationContribution of localizations) {
if (extension.location.scheme === Schemas.file && isValidLocalization(localizationContribution)) {
let languagePack = languagePacks[localizationContribution.languageId];
if (!languagePack) {
languagePack = { hash: '', extensions: [], translations: {} };
languagePacks[localizationContribution.languageId] = languagePack;
}
let extensionInLanguagePack = languagePack.extensions.filter(e => areSameExtensions(e.extensionIdentifier, extensionIdentifier))[0];
if (extensionInLanguagePack) {
extensionInLanguagePack.version = extension.manifest.version;
} else {
languagePack.extensions.push({ extensionIdentifier, version: extension.manifest.version });
}
for (const translation of localizationContribution.translations) {
languagePack.translations[translation.id] = join(extension.location.fsPath, translation.path);
}
}
}
}
private updateHash(languagePack: ILanguagePack): void {
if (languagePack) {
const md5 = createHash('md5');
for (const extension of languagePack.extensions) {
md5.update(extension.extensionIdentifier.uuid || extension.extensionIdentifier.id).update(extension.version);
}
languagePack.hash = md5.digest('hex');
}
}
private withLanguagePacks<T>(fn: (languagePacks: { [language: string]: ILanguagePack }) => T | null = () => null): Promise<T> {
return this.languagePacksFileLimiter.queue(() => {
let result: T | null = null;
return Promises.readFile(this.languagePacksFilePath, 'utf8')
.then(undefined, err => err.code === 'ENOENT' ? Promise.resolve('{}') : Promise.reject(err))
.then<{ [language: string]: ILanguagePack }>(raw => { try { return JSON.parse(raw); } catch (e) { return {}; } })
.then(languagePacks => { result = fn(languagePacks); return languagePacks; })
.then(languagePacks => {
for (const language of Object.keys(languagePacks)) {
if (!languagePacks[language]) {
delete languagePacks[language];
}
}
this.languagePacks = languagePacks;
this.initializedCache = true;
const raw = JSON.stringify(this.languagePacks);
this.logService.debug('Writing language packs', raw);
return Promises.writeFile(this.languagePacksFilePath, raw);
})
.then(() => result, error => this.logService.error(error));
});
}
}