1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.LocalizationRegistry = exports.LocalizationContribution = void 0;
|
28 | const fs = require("fs-extra");
|
29 | const inversify_1 = require("inversify");
|
30 | const common_1 = require("../../common");
|
31 | const localization_provider_1 = require("./localization-provider");
|
32 | exports.LocalizationContribution = Symbol('LocalizationContribution');
|
33 | let 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);
|
106 | LocalizationRegistry = __decorate([
|
107 | (0, inversify_1.injectable)()
|
108 | ], LocalizationRegistry);
|
109 | exports.LocalizationRegistry = LocalizationRegistry;
|
110 |
|
\ | No newline at end of file |