liferay-npm-bundler-preset-reactjs
Version:
liferay npm bundler preset react
109 lines (108 loc) • 4.11 kB
JavaScript
"use strict";
/**
* SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const properties_1 = __importDefault(require("properties"));
const file_path_1 = __importDefault(require("../file-path"));
const util_1 = require("./util");
/**
* Reflects localization configuration of JS Toolkit projects.
*/
class Localization {
constructor(project) {
this._supported = false;
this._localeStringsMap = {};
this._project = project;
let absPath = util_1.getFeaturesFilePath(this._project, 'create-jar.features.localization', 'features/localization/Language.properties');
if (absPath !== undefined) {
// Be lenient: remove trailing .properties if present
if (absPath.endsWith('.properties')) {
absPath = absPath.substring(0, absPath.length - 11);
}
this._languageFileBaseName = new file_path_1.default(absPath);
this._supported = true;
}
}
/**
* Get the array of available locales for the project.
* @return an array or `undefined` if L10N is not supported
*/
get availableLocales() {
if (!this.supported) {
return undefined;
}
if (this._availableLocales === undefined) {
this._availableLocales = Object.keys(this.localizationFileMap).filter((locale) => locale !== Localization.DEFAULT_LOCALE);
}
return this._availableLocales;
}
/**
* Get the map of localized strings for a given locale.
* @return a map or `undefined` if L10N is not supported
*/
getLabels(locale = Localization.DEFAULT_LOCALE) {
if (!this.supported) {
return undefined;
}
if (this._localeStringsMap[locale] === undefined) {
const file = this.localizationFileMap[locale];
if (file) {
this._localeStringsMap[locale] = properties_1.default.parse(fs_1.default.readFileSync(file.asNative).toString());
}
else {
this._localeStringsMap[locale] = {};
}
}
return this._localeStringsMap[locale];
}
/**
* Get the language file base name (absolute path plus name without
* `.properties` extension).
* @return a base file name or `undefined` if L10N is not supported
*/
get languageFileBaseName() {
if (!this.supported) {
return undefined;
}
return this._languageFileBaseName;
}
/**
* Get the map of localization FilePaths indexed by locale abbreviation.
* @return a map or `undefined` if L10N is not supported
*/
get localizationFileMap() {
if (!this.supported) {
return undefined;
}
if (this._localizationFileMap === undefined) {
const localizationDirPath = path_1.default.dirname(this.languageFileBaseName.asNative);
const fileNames = fs_1.default.readdirSync(localizationDirPath);
this._localizationFileMap = fileNames.reduce((map, fileName) => ((map[this._getFileNameLocale(fileName)] = new file_path_1.default(path_1.default.join(localizationDirPath, fileName))),
map), {});
}
return this._localizationFileMap;
}
get supported() {
return this._supported;
}
/**
* Get the locale of a .properties file based on its name
*/
_getFileNameLocale(fileName) {
const start = fileName.indexOf('_');
if (start === -1) {
return Localization.DEFAULT_LOCALE;
}
const end = fileName.lastIndexOf('.properties');
return fileName.substring(start + 1, end);
}
}
exports.default = Localization;
Localization.DEFAULT_LOCALE = 'default';