UNPKG

5.01 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2021 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 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.LocalizationRegistry = exports.LocalizationContribution = void 0;
28const fs = require("fs-extra");
29const inversify_1 = require("inversify");
30const common_1 = require("../../common");
31const localization_provider_1 = require("./localization-provider");
32exports.LocalizationContribution = Symbol('LocalizationContribution');
33let LocalizationRegistry = class LocalizationRegistry {
34 async initialize() {
35 await Promise.all(this.contributions.getContributions().map(contribution => contribution.registerLocalizations(this)));
36 }
37 registerLocalization(localization) {
38 this.localizationProvider.addLocalizations(localization);
39 }
40 registerLocalizationFromRequire(locale, required) {
41 const translations = this.flattenTranslations(required);
42 this.registerLocalization(this.createLocalization(locale, translations));
43 }
44 async registerLocalizationFromFile(localizationPath, locale) {
45 if (!locale) {
46 locale = this.identifyLocale(localizationPath);
47 }
48 if (!locale) {
49 throw new Error('Could not determine locale from path.');
50 }
51 const translationJson = await fs.readJson(localizationPath);
52 const translations = this.flattenTranslations(translationJson);
53 this.registerLocalization(this.createLocalization(locale, translations));
54 }
55 createLocalization(locale, translations) {
56 let localization;
57 if (typeof locale === 'string') {
58 localization = {
59 languageId: locale,
60 translations
61 };
62 }
63 else {
64 localization = Object.assign(Object.assign({}, locale), { translations });
65 }
66 return localization;
67 }
68 flattenTranslations(localization) {
69 if ((0, common_1.isObject)(localization)) {
70 const record = {};
71 for (const [key, value] of Object.entries(localization)) {
72 if (typeof value === 'string') {
73 record[key] = value;
74 }
75 else if ((0, common_1.isObject)(value)) {
76 const flattened = this.flattenTranslations(value);
77 for (const [flatKey, flatValue] of Object.entries(flattened)) {
78 record[`${key}/${flatKey}`] = flatValue;
79 }
80 }
81 }
82 return record;
83 }
84 else {
85 return {};
86 }
87 }
88 identifyLocale(localizationPath) {
89 const regex = /nls\.(\w+)\.json$/i;
90 const match = regex.exec(localizationPath);
91 if (match) {
92 return match[1];
93 }
94 return undefined;
95 }
96};
97__decorate([
98 (0, inversify_1.inject)(localization_provider_1.LocalizationProvider),
99 __metadata("design:type", localization_provider_1.LocalizationProvider)
100], LocalizationRegistry.prototype, "localizationProvider", void 0);
101__decorate([
102 (0, inversify_1.inject)(common_1.ContributionProvider),
103 (0, inversify_1.named)(exports.LocalizationContribution),
104 __metadata("design:type", Object)
105], LocalizationRegistry.prototype, "contributions", void 0);
106LocalizationRegistry = __decorate([
107 (0, inversify_1.injectable)()
108], LocalizationRegistry);
109exports.LocalizationRegistry = LocalizationRegistry;
110//# sourceMappingURL=localization-contribution.js.map
\No newline at end of file