var cx_libtranslate = (() => { var __getOwnPropNames = Object.getOwnPropertyNames; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; // source/translation.js var require_translation = __commonJS({ "source/translation.js"(exports) { var translation = class { /** * @var {string} * This is translated content. */ #content; /** * @var {bool} * This is true, when content is translated from dict, and false * when could not being found. */ #translated; /** * This create new translation. Translation store content of the * translation, make avairable to format translated phrase and also * store that translation was found in the phrasebook. * * @param {string} content - Content of the translation. * @param {bool} translated - True when translation could be found. */ constructor(content, translated = true) { if (typeof content !== "string") { throw new TypeError("Translated content must be string."); } if (typeof translated !== "boolean") { throw new TypeError("Result of translation must be boolean."); } this.#content = content; this.#translated = translated; Object.freeze(this); } /** * This convert transiation to string. * * @returns {string} - Content of the translation. */ toString() { return this.#content; } /** * @returns {string} - Content of the translation. */ get text() { return this.#content; } /** * @returns {bool} - True when translation was found, false when not. */ get valid() { return this.translated; } /** * This would format ready translation, with numbers, dats, and * other content, which could not being statically places into * translation. To use it, place name of content object key into * "#{}" in translation. * * @example ``` * Translation: "I have more than #{how_many} apples!" * Object: { how_many: 10 } * Result: "I have more than 10 apples!" * ``` * * @param {string} content * @returns {string} */ format(content) { if (typeof content !== "object") { throw new TypeError("Content to format from must be object."); } if (!this.#translated) { return this.#content; } return this.#parse_format(content); } /** * This infill prepared translation with data from content * object. * * @see format * * @param {object} content - Content to load data from. * @returns {string} - Formater translation. */ #parse_format(content) { let parts = this.#content.split("#{"); let result = parts[0]; for (let count = 1; count < parts.length; ++count) { const part = parts[count]; const splited = part.split("}"); if (splited.length === 1) { return result + splited[0]; } const name = splited.splice(0, 1)[0].trim(); const rest = splited.join("}"); if (!(name in content)) { result += rest; continue; } result += content[name] + rest; } return result; } }; exports.translation = translation; } }); // source/phrasebook.js var require_phrasebook = __commonJS({ "source/phrasebook.js"(exports) { var translation = require_translation().translation; var phrasebook = class _phrasebook { /** * @var {Map} * This store phrases in flat notation. */ #phrases; /** * @var {?object} * This store object for nested object notation. */ #objects; /** * This create new phrasebook from phrases map, and optional object * for phrases in object notation. * * @param {Map} phrases - This contain phrases in flat notation. * @param {?object} objects - This contain phrases in object notation. */ constructor(phrases, objects = null) { if (!(phrases instanceof Map)) { throw new TypeError("Phrases must an map."); } if (objects !== null && typeof objects !== "object") { throw new TypeError("Objects must be null or object."); } this.#phrases = phrases; this.#objects = objects; } /** * This translate given phrase. When phrase is in the nested object * notation, then try to find phrase in objects. When not, try to find * phrase in the flat phrases. When could not find phrase, then return * not translated phrase. Content always is returned as translation * object, which could be also formated wich numbers, dates and * much more. * * @param {string} phrase - Phrase to translate. * @returns {translation} - Translated phrase. */ translate(phrase) { if (typeof phrase !== "string") { throw new TypeError("Phrase to translate must be an string."); } if (this.#is_nested(phrase)) { return this.#translate_nested(phrase); } return this.#translate_flat(phrase); } /** * This translate given phrase. When phrase is in the nested object * notation, then try to find phrase in objects. When not, try to find * phrase in the flat phrases. When could not find phrase, then return * not translated phrase. Content always is returned as translation * object, which could be also formated wich numbers, dates and * much more. * * @param {string} phrase - Phrase to translate. * @returns {translation} - Translated phrase. */ get(phrase) { return this.translate(phrase); } /** * Check that phrase is nested or not. * * @param {string} phrase - Phrase to check that is nested * @returns {bool} - True when nested, false when not */ #is_nested(phrase) { return phrase.indexOf(".") !== -1; } /** * This translate object notated phrase. * * @param {string} phrase - Phrase to translate. * @returns {translation} - Translated phrase. */ #translate_nested(phrase) { if (this.#objects === null) { return this.#translate_flat(phrase); } const parts = phrase.trim().split("."); let current = this.#objects; for (const count in parts) { const part = parts[count]; if (!(part in current)) { return new translation(phrase, false); } current = current[part]; } if (typeof current !== "string") { return new translation(phrase, false); } return new translation(current, true); } /** * This translate flat phrase. * * @param {string} phrase - Phrase to translate. * @returns {translation} - Translated phrase. */ #translate_flat(phrase) { const prepared = _phrasebook.prepare(phrase); const found = this.#phrases.has(prepared); const translated = found ? this.#phrases.get(prepared) : phrase; return new translation(translated, found); } /** * This prepars phrase, that mean replece all spaces with "_", trim * and also replace all big letters with lowwer. * * @param {string} content - Phrase to preapre. * @return {string} - Prepared phrase. */ static prepare(content) { if (typeof content !== "string") { throw new TypeError("Content to prepare must be an string."); } return content.trim().replaceAll(" ", "_").replaceAll(".", "_").toLowerCase(); } }; exports.phrasebook = phrasebook; } }); // source/loader.js var require_loader = __commonJS({ "source/loader.js"(exports) { var phrasebook = require_phrasebook().phrasebook; var loader = class { /** * @var {string} * This is location of the phrasebook on the server. */ #path; /** * @var {bool} * This is true, when must load local file, or false when fetch. */ #local; /** * This create new loader of the phrasebook. * * @param {string} path - Location of the phrasebook to fetch. * @param {bool} local - False when must fetch from remote. */ constructor(path, local = false) { if (typeof path !== "string") { throw new TypeError("Path of the file must be string."); } if (typeof local !== "boolean") { throw new TypeError("Local must be bool variable."); } this.#path = path; this.#local = local; } /** * This load file from path given in the constructor, parse and return it. * * @returns {phrasebook} - New phrasebook with content from JSON file. */ async #load_remote() { const request = await fetch(this.#path); const response = await request.json(); return this.#parse(response); } /** * This load file from path given in the constructor, parse and return it. * * @returns {phrasebook} - New phrasebook with content from JSON file. */ async #load_local() { let fs = null; if (fs === null) { throw new Error("Could not use ndoe:fs in browser."); } const content = await fs.readFile(this.#path, { encoding: "utf8" }); const response = JSON.parse(content); return this.#parse(response); } /** * This load file from path given in the constructor, parse and return it. * * @returns {phrasebook} - New phrasebook with content from JSON file. */ load() { if (this.#local) { return this.#load_local(); } return this.#load_remote(); } /** * This parse phrasebook. When phrasebook contain "phrases" or "objects" * keys, and also "objects" is not string, then parse it as nested file, * in the other way parse it as flat. * * @param {object} content - Fetched object with translations. * @returns {phrasebook} - Loaded phrasebook. */ #parse(content) { const has_objects = "objects" in content && typeof content["objects"] === "object"; const has_phrases = "phrases" in content && typeof content["phrases"] === "object"; const is_nested = has_objects || has_phrases; if (is_nested) { const phrases = has_phrases ? content["phrases"] : {}; const objects = has_objects ? content["objects"] : {}; return new phrasebook( this.#parse_phrases(phrases), objects ); } return new phrasebook(this.#parse_phrases(content)); } /** * This parse flat phrases object to map. * * @param {object} content - Flat phrases object to pase. * @returns {Map} - Phrases parsed as Map. */ #parse_phrases(content) { const phrases = /* @__PURE__ */ new Map(); Object.keys(content).forEach((phrase) => { const name = phrasebook.prepare(phrase); const translation = content[phrase]; phrases.set(name, translation); }); return phrases; } }; exports.loader = loader; } }); // source/languages.js var require_languages = __commonJS({ "source/languages.js"(exports) { var loader = require_loader().loader; var phrasebook = require_phrasebook().phrasebook; var languages = class _languages { /** * @var {string} * This represents path to directory where phrasebooks had been stored. */ #path; /** * @var {Map} * This store languages and its files on server. */ #libs; /** * @var {bool} * This store that directory is in the local file system, or remote * server. When true, resources would be loaded by node:fs. When * false, resources would be fetched. */ #local; /** * This create new languages library. Next, languages could be added to * the library by command, or by loading index file. * * @throws {TypeError} - When parameters is not in correct format. * * @param {string} path - Path to phrasebooks on the server or filesystem. * @param {bool} local - True when phrasebooks dirs would be loaded by * node:fs module. False when would be fetch. */ constructor(path, local = false) { if (typeof path !== "string") { throw new TypeError("Path to the phrasebooks must be string."); } if (typeof local !== "boolean") { throw new TypeError("Local must be bool variable."); } this.#local = local; this.#path = path; this.#libs = /* @__PURE__ */ new Map(); } /** * This add new language to the library by name. Name must be in form * like POSIX locale, like en_US, or pl_PL. That mean first two letter * mest be ISO 639-1 and second two letters mst be in ISO 3166-1 alpha-2 * 2 letter country code format. * * @see https://www.loc.gov/standards/iso639-2/php/code_list.php * @see https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 * @see https://en.wikipedia.org/wiki/Locale_(computer_software) * * @throws {TypeError} - When tpes of the parameters is not correct. * * @param {string} name - Name of the language, like "en_US". * @param {string} file - Name of the file in the directory. * @return {languages} - Instnace of this class to chain. */ add(name, file) { if (typeof name !== "string") { throw new TypeError("Name of the language must be sting."); } if (typeof file !== "string") { throw new TypeError("File on in the directory must be string."); } if (this.#libs.has(name)) { console.error('Language "' + name + '" already loaded.'); console.error("It could not being loaded twice."); return this; } if (!this.#valid_locale(name)) { console.error('Language name "' + name + '" invalid formated.'); console.error("It could not being loaded."); return this; } this.#libs.set(name, file); return this; } /** * This load all phrasebook given in the index file. Index must be * JSON file, which contain one object. That object properties must be * languages names in the notation like in add function. Valus of that * properties musts being strings which contains names of the phrasebook * files in the path directory. * * @example ``` { "pl_PL": "polish.json", "en_US": "english.json" } ``` * * @see add * * @param {string} index - Index file in the phrasebook directory. * @return {languages} - New languages instance with loaded index. */ async load(index) { if (typeof index !== "string") { throw new TypeError("Name of index file is not string."); } const response = await this.#load_index(index); const result = new _languages(this.#path, this.#local); Object.keys(response).forEach((name) => { if (typeof name !== "string") { console.error("Name of the language must be string."); console.error("Check languages index."); console.error("Skipping it."); return; } if (typeof response[name] !== "string") { console.error("Name of phrasebook file must be string."); console.error("Check languages index."); console.error("Skipping it."); return; } result.add(name, response[name]); }); return result; } /** * This load index object. That check, and when content must be loaded * from local filesystem, it use node:fs, or when it must be fetched from * remote, then use fetch API. * * @param {string} index - Name of the index file in library. * @returns {object} - Loaded index file content. */ async #load_index(index) { const path = this.#full_path(index); if (this.#local) { let fs = null; if (fs === null) { throw new Error("Could not use ndoe:fs in browser."); } return JSON.parse( await fs.readFile(path, { encoding: "utf-8" }) ); } const request = await fetch(path); return await request.json(); } /** * This check that language exists in languages library. * * @param {string} name - Name of the language to check. * @return {bool} - True when language exists, false when not */ has(name) { return this.#libs.has(name); } /** * This return all avairable languages. * * @return {Array} - List of all avairable languages. */ get avairable() { const alls = new Array(); this.#libs.keys().forEach((name) => { alls.push(name); }); return alls; } /** * This load phrasebook with give name. * * @throws {TypeError} - Param type is not correct. * @throws {RangeError} - Language not exists in libs. * * @param {string} name - Name of the language to load. * @returns {phrasebook} - Phrasebook loaded from the file. */ select(name) { if (typeof name !== "string") { throw new TypeError("Name of the language must be string."); } if (!this.has(name)) { return new phrasebook(/* @__PURE__ */ new Map()); } const file = this.#libs.get(name); const path = this.#full_path(file); return new loader(path, this.#local).load(); } /** * This return full path to the file. * * @param {string} name - Name of the file to get its path * @return {string} - Full path of the file */ #full_path(name) { let glue = "/"; if (this.#path[this.#path.length - 1] === glue) { glue = ""; } return this.#path + glue + name; } /** * This check that format is valid POSIX like locale. * * @param {string} name - Name to check format of. * @return {bool} - True when format is valid, false when not. */ #valid_locale(name) { const splited = name.split("_"); if (splited.length !== 2) { return false; } const first = splited[0]; const second = splited[1]; if (first.toLowerCase() !== first || first.length !== 2) { return false; } if (second.toUpperCase() !== second || second.length !== 2) { return false; } return true; } }; exports.languages = languages; } }); // source/core.js var require_core = __commonJS({ "source/core.js"(exports, module) { if (typeof module !== "undefined" && module.exports) { module.exports.phrasebook = require_phrasebook().phrasebook; module.exports.loader = require_loader().loader; module.exports.languages = require_languages().languages; module.exports.loader = require_loader().loader; } else { window.cx_libtranslate = { phrasebook: require_phrasebook().phrasebook, loader: require_loader().loader, languages: require_languages().languages, translation: require_translation().translation }; } } }); return require_core(); })(); //# sourceMappingURL=cx-libtranslate.full.js.map