UNPKG

amourlinguini

Version:

A JSON-based translation file manager.

156 lines 7.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Linguini = void 0; var path_1 = __importDefault(require("path")); var error_models_1 = require("./models/error-models"); var utils_1 = require("./utils/"); var Linguini = /** @class */ (function () { /** * Creates a new Linguini object to manage language files. * * @param folderPath - The folder path containing the language files. * @param fileName - The base name of the language files to use. Note this should not include any file extensions or language codes. Ex: `lang`. * @param options - Options to use for this Linguini setup. * * @returns A new Linguini object. */ function Linguini(folderPath, fileName, options) { if (options === void 0) { options = {}; } var _a; this.options = { replacementLevels: 10, customCommonFile: undefined, }; this.comData = {}; this.langDatas = {}; this.options = Object.assign(this.options, options); // Validate options if (this.options.customCommonFile && !utils_1.FileUtils.exists(this.options.customCommonFile)) { throw new error_models_1.LinguiniError("Custom common file does not exist: ".concat(this.options.customCommonFile)); } // Locate common file var comFilePath = (_a = this.options.customCommonFile) !== null && _a !== void 0 ? _a : path_1.default.join(folderPath, "".concat(fileName, ".common.json")); var comVars = {}; if (utils_1.FileUtils.exists(comFilePath)) { // Read common file var comFileContents = utils_1.FileUtils.readFileSync(comFilePath); var comFile = JSON.parse(comFileContents); // Extract common variables comVars = utils_1.DataUtils.flattenToVariables(comFile, 'COM:'); for (var i = 0; i < this.options.replacementLevels; i++) { comVars = utils_1.DataUtils.replaceVariablesInObj(comVars, comVars); } this.comData = comVars; } var fileNames = utils_1.FileUtils.readFileNamesSync(folderPath); var langCodes = utils_1.RegexUtils.getLangCodes(fileName, fileNames); for (var _i = 0, langCodes_1 = langCodes; _i < langCodes_1.length; _i++) { var langCode = langCodes_1[_i]; // Locate lang file var langFileName = "".concat(fileName, ".").concat(langCode, ".json"); var langFilePath = path_1.default.join(folderPath, langFileName); // Read lang file var langFileContents = utils_1.FileUtils.readFileSync(langFilePath); var langFile = JSON.parse(langFileContents); // Extract ref variables var refVars = utils_1.DataUtils.flattenToVariables(langFile.refs, 'REF:'); refVars = utils_1.DataUtils.replaceVariablesInObj(refVars, comVars); for (var i = 0; i < this.options.replacementLevels; i++) { refVars = utils_1.DataUtils.replaceVariablesInObj(refVars, refVars); } // Replace variables in lang data var langData = utils_1.DataUtils.replaceVariablesInObj(langFile.data, comVars); langData = utils_1.DataUtils.replaceVariablesInObj(langFile.data, refVars); // Store lang data this.langDatas[langCode] = { data: utils_1.DataUtils.flatten(langData), refs: refVars, }; } } /** * Returns an item from a language file, mapped to a type. * * @param location - The location of the item in the language file, using dot-notation, and relative to the "data" section in the JSON. Ex: `myCategory.myItem`. * @param langCode - The language file code to extract from. Ex: `en`. * @param typeMapper - A function which maps the retrieved item data to a type. The could be a built-in function from Linguini's `TypeMappers` import, or a custom function. * @param variables - Any variables (Ex: `{{MY_VARIABLE}}`) to replace in the retrieved data. * * @returns The retrieved language file item. */ Linguini.prototype.get = function (location, langCode, typeMapper, variables) { var raw = this.getRaw(location, langCode, variables); return typeMapper(raw); }; /** * Returns an item from a language file, as raw JSON. * * @param location - The location of the item in the language file, using dot-notation, and relative to the "data" section in the JSON. Ex: `myCategory.myItem`. * @param langCode - The language file code to extract from. Ex: `en`. * @param variables - Any variables (Ex: `{{MY_VARIABLE}}`) to replace in the retrieved data. * * @returns The retrieved language file item. */ Linguini.prototype.getRaw = function (location, langCode, variables) { var langData = this.langDatas[langCode]; if (langData === undefined) { throw new error_models_1.LinguiniError("Invalid language code: ".concat(langCode)); } var jsonValue = langData.data[location]; if (jsonValue === undefined) { throw new error_models_1.LinguiniError("Invalid location: ".concat(location)); } jsonValue = JSON.parse(JSON.stringify(jsonValue)); if (variables) { jsonValue = utils_1.DataUtils.replaceVariablesInObj(jsonValue, variables); } return jsonValue; }; /** * Returns a reference string from a language file. * * @param location - The location of the reference in the language file, using dot-notation, and relative to the "refs" section in the JSON. Ex: `myCategory.myItem`. * @param langCode - The language file code to extract from. Ex: `en`. * @param variables - Any variables (Ex: `{{MY_VARIABLE}}`) to replace in the retrieved data. * * @returns The retrieved language file reference string. */ Linguini.prototype.getRef = function (location, langCode, variables) { var langData = this.langDatas[langCode]; if (langData === undefined) { throw new error_models_1.LinguiniError("Invalid language code: ".concat(langCode)); } var ref = langData.refs["REF:".concat(location)]; if (ref === undefined) { throw new error_models_1.LinguiniError("Invalid location: ".concat(location)); } if (variables) { ref = utils_1.DataUtils.replaceVariablesInObj(ref, variables); } return ref; }; /** * Returns a common reference string from the common language file (*.common.json). * * @param location - The location of the reference in the common language file, using dot-notation. Ex: `myCategory.myItem`. * @param variables - Any variables (Ex: `{{MY_VARIABLE}}`) to replace in the retrieved data. * * @returns The retrieved common reference string. */ Linguini.prototype.getCom = function (location, variables) { var com = this.comData["COM:".concat(location)]; if (com === undefined) { throw new error_models_1.LinguiniError("Invalid location: ".concat(location)); } if (variables) { com = utils_1.DataUtils.replaceVariablesInObj(com, variables); } return com; }; return Linguini; }()); exports.Linguini = Linguini; //# sourceMappingURL=linguini.js.map