@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
104 lines • 4.17 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2021 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import * as fs from '../../../shared/fs-extra/index.cjs';
import { inject, injectable, named } from 'inversify';
import { ContributionProvider, isObject } from '../../common/index.js';
import { LazyLocalization, LocalizationProvider } from './localization-provider.js';
export const LocalizationContribution = Symbol('LocalizationContribution');
let LocalizationRegistry = class LocalizationRegistry {
async initialize() {
await Promise.all(this.contributions.getContributions().map(contribution => contribution.registerLocalizations(this)));
}
registerLocalization(localization) {
if (!LazyLocalization.is(localization)) {
localization = LazyLocalization.fromLocalization(localization);
}
this.localizationProvider.addLocalizations(localization);
}
registerLocalizationFromRequire(locale, required) {
const translations = this.flattenTranslations(required);
this.registerLocalization(this.createLocalization(locale, () => Promise.resolve(translations)));
}
registerLocalizationFromFile(localizationPath, locale) {
if (!locale) {
locale = this.identifyLocale(localizationPath);
}
if (!locale) {
throw new Error('Could not determine locale from path.');
}
this.registerLocalization(this.createLocalization(locale, async () => {
const translationJson = await fs.readJson(localizationPath);
return this.flattenTranslations(translationJson);
}));
}
createLocalization(locale, translations) {
let localization;
if (typeof locale === 'string') {
localization = {
languageId: locale,
getTranslations: translations
};
}
else {
localization = {
...locale,
getTranslations: translations
};
}
return localization;
}
flattenTranslations(localization) {
if (isObject(localization)) {
const record = {};
for (const [key, value] of Object.entries(localization)) {
if (typeof value === 'string') {
record[key] = value;
}
else if (isObject(value)) {
const flattened = this.flattenTranslations(value);
for (const [flatKey, flatValue] of Object.entries(flattened)) {
record[`${key}/${flatKey}`] = flatValue;
}
}
}
return record;
}
else {
return {};
}
}
identifyLocale(localizationPath) {
const regex = /nls\.(\w+)\.json$/i;
const match = regex.exec(localizationPath);
if (match) {
return match[1];
}
return undefined;
}
};
__decorate([
inject(LocalizationProvider)
], LocalizationRegistry.prototype, "localizationProvider", void 0);
__decorate([
inject(ContributionProvider),
named(LocalizationContribution)
], LocalizationRegistry.prototype, "contributions", void 0);
LocalizationRegistry = __decorate([
injectable()
], LocalizationRegistry);
export { LocalizationRegistry };
//# sourceMappingURL=localization-contribution.js.map