@softkit/i18n
Version:
This library is a simple wrapper based on [nestjs-i18n](https://nestjs-i18n.com/)
108 lines • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.I18nAbstractFileLoader = void 0;
const tslib_1 = require("tslib");
const i18n_loader_1 = require("./i18n.loader");
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const promises_1 = require("node:fs/promises");
const utils_1 = require("../utils");
const i18n_error_1 = require("../i18n.error");
const isI18nData = (data) => {
return typeof data === 'object' && data !== null;
};
const isStringOrI18nTranslation = (value) => {
return (typeof value === 'string' || (typeof value === 'object' && value !== null));
};
class I18nAbstractFileLoader extends i18n_loader_1.I18nLoader {
constructor(options) {
super(options);
this.options = this.sanitizeOptions(options);
}
async languages() {
return this.parseLanguages();
}
async load() {
return this.parseTranslations();
}
async parseTranslations() {
const i18nPath = node_path_1.default.normalize(this.options.path + node_path_1.default.sep);
const translations = {};
const filePattern = this.options.filePattern ?? '*.json';
if (!(await (0, utils_1.exists)(i18nPath))) {
throw new i18n_error_1.I18nError(`i18n path (${i18nPath}) cannot be found`);
}
if (!/\*\.[A-z]+/.test(filePattern)) {
throw new i18n_error_1.I18nError(`filePattern should be formatted like: *.json, *.txt, *.custom etc`);
}
const languages = await this.parseLanguages();
const pattern = new RegExp('.' + filePattern.replace('.', '.'));
const files = await [
...languages.map((l) => node_path_1.default.join(i18nPath, l)),
i18nPath,
].reduce(async (f, p) => {
(await f).push(...(await (0, utils_1.getFiles)(p, pattern, this.options.includeSubfolders ?? false)));
return f;
}, Promise.resolve([]));
for (const file of files) {
let global = false;
const pathParts = node_path_1.default
.dirname(node_path_1.default.relative(i18nPath, file))
.split(node_path_1.default.sep);
const key = pathParts[0];
if (key === '.') {
global = true;
}
const source = await (0, promises_1.readFile)(file, 'utf8');
const data = this.formatData(source, file);
const prefix = [...pathParts.slice(1), node_path_1.default.basename(file).split('.')[0]];
if (isI18nData(data)) {
for (const property of Object.keys(data)) {
for (const lang of global ? languages : [key]) {
translations[lang] = translations[lang] ?? {};
const translationsProperty = data[property];
if (global) {
if (isStringOrI18nTranslation(translationsProperty)) {
translations[lang][property] = translationsProperty;
}
}
else {
this.assignPrefixedTranslation(translations[lang], prefix, property, translationsProperty);
}
}
}
}
}
return translations;
}
assignPrefixedTranslation(translations, prefix, property, value) {
if (typeof translations === 'string') {
throw new TypeError('Cannot assign a prefixed translation to a string');
}
if (prefix.length > 0) {
translations[prefix[0]] = translations[prefix[0]] ?? {};
this.assignPrefixedTranslation(translations[prefix[0]], prefix.slice(1), property, value);
}
else if (isStringOrI18nTranslation(value)) {
translations[property] = value;
}
}
async parseLanguages() {
const i18nPath = node_path_1.default.normalize(this.options.path + node_path_1.default.sep);
return (await (0, utils_1.getDirectories)(i18nPath)).map((dir) => node_path_1.default.relative(i18nPath, dir));
}
sanitizeOptions(options) {
const defaultOptions = this.getDefaultOptions();
const normalizedPath = node_path_1.default.normalize(options.path + node_path_1.default.sep);
const filePattern = options.filePattern || defaultOptions.filePattern || '*.json';
return {
...defaultOptions,
...options,
path: normalizedPath,
filePattern: filePattern.startsWith('*.')
? filePattern
: '*.' + filePattern,
};
}
}
exports.I18nAbstractFileLoader = I18nAbstractFileLoader;
//# sourceMappingURL=i18n.abstract-file.loader.js.map