| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189 |
- 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)) {
- DEBUG: throw new RangeError(
- 'Could not find "' + name + '".'
- );
- 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 set phrasebook as default. That mean, it would add own translate
- * function to global scope, and create _({string}) function in the global
- * scope. It is useable to minify code with translations. It is not
- * avairable in the NodeJS, only in the browser.
- *
- * @returns {phrasebook} - Object itself to chain loading.
- */
- set_as_default() {
- const translate = (content) => {
- return this.translate(content);
- };
- window._ = translate;
- window.translate = translate;
- }
- /**
- * 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 {
- /**
- * @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);
- this.#libs.clear();
- 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;
- }
- this.add(name, response[name]);
- });
- return this;
- }
- /**
- * 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;
- }
- /**
- * @returns {string} - Default
- */
- get default() {
- const avairable = this.avairable;
- if (avairable.length === 0) {
- throw new Error("Languages list is empty. Can not load default.");
- }
- return avairable[0];
- }
- /**
- * 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)) {
- DEBUG: throw new RangeError(
- 'Not found language "' + 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/selector.js
- var require_selector = __commonJS({
- "source/selector.js"(exports) {
- var languages = require_languages().languages;
- var selector = class {
- /**
- * @var {languages}
- * This store languages container.
- */
- #languages;
- /**
- * @var {?HTMLElement}
- * This store selector HTML container, or null when not created.
- */
- #container;
- /**
- * @var {HTMLElement}
- * This store languages HTML selector object.
- */
- #selector;
- /**
- * @var {Array}
- * This is array of callbacks which must being called after change.
- */
- #callbacks;
- /**
- * This create new selector object, from languages container.
- *
- * @param {languages} languages - Languages container to work on.
- */
- constructor(languages2) {
- this.#container = null;
- this.#languages = languages2;
- this.#callbacks = new Array();
- this.#selector = this.#create_selector();
- }
- /**
- * This check that selector is currently inserted anywhere.
- *
- * @returns {bool} - True when selector is inserted anywhere.
- */
- get is_inserted() {
- return this.#container !== null;
- }
- /**
- * This inserts selector into given HTML element, or directly into
- * document body, when any element is not specified. It returns
- * itselt, to call more functions inline.
- *
- * @param {?HTMLElement} where - Place to insert selector, or null.
- * @returns {selector} - This object to chain loading.
- */
- insert(where = null) {
- if (this.is_inserted) {
- return this;
- }
- if (where === null) {
- where = document.querySelector("body");
- }
- this.#container = this.#create_container();
- where.appendChild(this.#container);
- return this;
- }
- /**
- * This run all functions which was registered as onChange callback.
- */
- #on_change() {
- this.#callbacks.forEach((count) => {
- count(this.current);
- });
- }
- /**
- * This function remove selector from HTML element, if it is already
- * inserted anywhere.
- *
- * @returns {selector} - This object to chain loading.
- */
- remove() {
- if (!this.is_inserted) {
- return this;
- }
- this.#container.remove();
- this.#container = null;
- return this;
- }
- /**
- * This create new container with selector inside.
- *
- * @returns {HTMLElement} - New container with selector.
- */
- #create_container() {
- const container = document.createElement("div");
- container.classList.add(this.class_name);
- container.appendChild(this.#selector);
- return container;
- }
- /**
- * This add new callback to selector. All callbacks would be called
- * after language change. Callback would get one parameter, which is
- * name of the location.
- *
- * @param {CallableFunction} callback - Function to call on change.
- * @returns
- */
- add_listener(callback) {
- this.#callbacks.push(callback);
- return this;
- }
- /**
- * This return HTML class name of selector container.
- *
- * @returns {string} - HTML class name of selector container.
- */
- get class_name() {
- return "cx-libtranslate-language-selector";
- }
- /**
- * This create HTML option element from language name.
- *
- * @param {string} location - Name of single language.
- * @returns {HTMLElement} - New option element.
- */
- #create_option(location) {
- const name = location.split("_").pop();
- const option = document.createElement("option");
- option.innerText = name;
- option.value = location;
- return option;
- }
- /**
- * This return current selected language name.
- *
- * @returns {string} - Current selected language.
- */
- get current() {
- return this.#selector.value;
- }
- /**
- * This set current selected language for the selector.
- *
- * @param {string} name - Name of the language to set.
- * @returns {selector} - This to chain loading.
- */
- set_selection(name) {
- if (!this.#languages.has(name)) {
- DEBUG: throw new Error(
- 'Selector has not "' + name + '" language in the container.'
- );
- }
- this.#selector.value = name;
- return this;
- }
- /**
- * This reload languages list in the selector. It could be used
- * after change in the languages container.
- *
- * @returns {selector} - Itself to chain loading.
- */
- reload() {
- while (this.#selector.lastChild) {
- this.#selector.lastChild.remove();
- }
- this.#languages.avairable.forEach((count) => {
- this.#selector.appendChild(this.#create_option(count));
- });
- return this;
- }
- /**
- * This create new HTML selector object, witch all languages
- * from container inserted as options.
- *
- * @returns {HTMLElement} - New selector object.
- */
- #create_selector() {
- const selector2 = document.createElement("select");
- selector2.addEventListener("change", () => {
- this.#on_change();
- });
- this.#languages.avairable.forEach((count) => {
- selector2.appendChild(this.#create_option(count));
- });
- return selector2;
- }
- };
- exports.selector = selector;
- }
- });
- // source/autotranslate.js
- var require_autotranslate = __commonJS({
- "source/autotranslate.js"(exports) {
- var { phrasebook } = require_phrasebook();
- var autotranslate = class _autotranslate {
- /**
- * @var {?phrasebook}
- * This store phrasebook to get translates from, or store null, to get
- * translate content from global translate function.
- */
- #phrasebook;
- /**
- * @var {?MutationObserver}
- * This store observer object, when it is connecter and waiting for
- * changaes, or store null, when observer currently not working.
- */
- #observer;
- /**
- * This create new autotranslator. It require phrasebook, to loads
- * translations for phrases from. When null had been given, the it
- * use global translate function.
- *
- * @throws {Error} - When trying to use it in the NodeJS.
- *
- * @param {?phrasebook} phrasebook
- */
- constructor(phrasebook2 = null) {
- this.#observer = null;
- this.#phrasebook = phrasebook2;
- }
- /**
- * It return class name for elements, which would be translated by
- * autotranslator.
- *
- * @returns {string} - Class name for autotranslating elements.
- */
- static get_class_name() {
- return "translate";
- }
- /**
- * This return selector for choose elements which must be autotranslated.
- *
- * @returns {string} - Selector of the elements to translate.
- */
- get #class_selector() {
- return "." + _autotranslate.get_class_name();
- }
- /**
- * This return name of attribute which store phrase to translate.
- *
- * @returns {string} - Name of attribute which store phrase.
- */
- static get_attribute_name() {
- return "phrase";
- }
- /**
- * This check that autotranslator is connected and waiting to changes.
- *
- * @returns {bool} - True when observer is connected, fakse when not.
- */
- get is_connected() {
- return this.#observer !== null;
- }
- /**
- * This search elements which could be translated in the element given
- * in the parameter. When null given, then it search elements in the
- * all document.
- *
- * @param {?HTMLElement} where - Item to load items from or null.
- * @returns {Array} - Array of elements to translate.
- */
- #get_all_items(where = null) {
- if (where === null) {
- where = document;
- }
- return Array.from(
- where.querySelectorAll(this.#class_selector)
- );
- }
- /**
- * It translate given phrase, baseed on loaded phrasebook, or when not
- * loaded any, then use global translate function. When it also not
- * exists, then throws error in debug mode, or return not translated
- * phrase on production.
- *
- * @throws {Error} - When any option to translate not exists.
- *
- * @param {string} content - Phrase to translate.
- * @returns {string} - Translated content.
- */
- #translate(content) {
- if (this.#phrasebook !== null) {
- return this.#phrasebook.translate(content);
- }
- if (_ === void 0) {
- DEBUG: throw new Error("All translate options are unavairable.");
- return content;
- }
- return _(content);
- }
- /**
- * This add mutable observer to the body. It wait for DOM modifications,
- * and when any new node had been adder, or any phrase attribute had
- * been changed, then it trying to translate it.
- *
- * @returns {autotranslate} - This object to chain load.
- */
- connect() {
- if (this.is_connected) {
- return this;
- }
- const body = document.querySelector("body");
- const callback = (targets) => {
- this.#process(targets);
- };
- const options = {
- childList: true,
- attributes: true,
- characterData: false,
- subtree: true,
- attributeFilter: [_autotranslate.get_attribute_name()],
- attributeOldValue: false,
- characterDataOldValue: false
- };
- this.#observer = new MutationObserver(callback);
- this.#observer.observe(body, options);
- return this;
- }
- /**
- * This prcoess all given in the array mutable records.
- *
- * @param {Array} targets - Array with mutable records.
- */
- #process(targets) {
- targets.forEach((count) => {
- if (count.type === "attributes") {
- this.#update_single(count.target);
- return;
- }
- this.#get_all_items(count.target).forEach((count2) => {
- this.#update_single(count2);
- });
- });
- }
- /**
- * This disconnect observer, and remove it.
- *
- * @returns {autotranslate} - This object to chain loading.
- */
- disconnect() {
- if (!this.is_connected) {
- return this;
- }
- this.#observer.disconnect();
- this.#observer = null;
- return this;
- }
- /**
- * This update single element, based on phrase attribute. When element
- * is standard HTMLElement, then it place translated content into
- * innerText, but when element is input, like HTMLInputElement or
- * HTMLTextAreaElement, then it place result into placeholder. When
- * input is button, or submit, then it put content into value.
- *
- * @param {HTMLElement} target - Element to translate
- */
- #update_single(target) {
- const attrobute_name = _autotranslate.get_attribute_name();
- const phrase = target.getAttribute(attrobute_name);
- const result = this.#translate(phrase);
- if (target instanceof HTMLInputElement) {
- if (target.type === "button" || target.type === "submit") {
- target.value = result;
- return;
- }
- target.placeholder = result;
- return;
- }
- if (target instanceof HTMLTextAreaElement) {
- target.placeholder = result;
- return;
- }
- target.innerText = result;
- }
- /**
- * This update translation of all elements in the document. It is useable
- * when new autotranslator is created.
- *
- * @returns {autotranslate} - Instance of object to chain loading.
- */
- update() {
- this.#get_all_items().forEach((count) => {
- this.#update_single(count);
- });
- return this;
- }
- };
- exports.autotranslate = autotranslate;
- }
- });
- // source/preferences.js
- var require_preferences = __commonJS({
- "source/preferences.js"(exports) {
- var languages = require_languages().languages;
- var phrasebook = require_phrasebook().phrasebook;
- var selector = require_selector().selector;
- var autotranslate = require_autotranslate().autotranslate;
- var preferences = class {
- /**
- * @var {languages}
- * This store loaded languages object.
- */
- #languages;
- /**
- * @var {?selector}
- * This store selector for preferences.
- */
- #selector;
- /**
- * @var {?autotranslate}
- * This store autotranslator, or null.
- */
- #autotranslate;
- /**
- * This create new language preferences manager from loaded languages
- * object.
- *
- * @throws {Error} - When trying to use it in NodeJS.
- *
- * @param {languages} target - loaded languages object.
- */
- constructor(target) {
- this.#selector = null;
- this.#autotranslate = null;
- this.#languages = target;
- }
- /**
- * This return name of language key in localStorage.
- *
- * @returns {string} - Name of key in localStorage.
- */
- get #setting_name() {
- return "cx_libtranslate_lang";
- }
- /**
- * This return current saved language name, or null if any language
- * is not selected yet.
- *
- * @returns {?string} - Saved language or null.
- */
- get #state() {
- return localStorage.getItem(this.#setting_name);
- }
- /**
- * This return selector, which could being used in the ui.
- *
- * @returns {selector} - New UI selector of the languages.
- */
- get selector() {
- if (this.#selector !== null) {
- return this.#selector;
- }
- this.#selector = new selector(this.#languages).set_selection(this.current).add_listener((target) => {
- this.update(target);
- }).add_listener(async (target) => {
- if (this.#autotranslate === null) {
- return;
- }
- this.#reload_autotranslate();
- });
- return this.#selector;
- }
- async #reload_autotranslate() {
- const connected = this.#autotranslate.is_connected;
- if (connected) {
- this.#autotranslate.disconnect();
- }
- this.#autotranslate = null;
- const created = await this.get_autotranslate();
- if (connected) {
- created.connect();
- }
- }
- /**
- * This load phrasebook for selected language, create autotranslate
- * for it, and returns it. Autotranslate is cached in this object.
- *
- * @returns {autotranslate} - Autotranslate for phrasebook.
- */
- async get_autotranslate() {
- if (this.#autotranslate !== null) {
- return this.#autotranslate;
- }
- const phrasebook2 = await this.load_choosen_phrasebook();
- this.#autotranslate = new autotranslate(phrasebook2);
- this.#autotranslate.update();
- return this.#autotranslate;
- }
- /**
- * This save new selected language name to localStorage, or remove it
- * from there if null given.
- *
- * @param {?string} content - Name of selected language or null.
- */
- set #state(content) {
- if (content === null) {
- localStorage.removeItem(this.#setting_name);
- return;
- }
- localStorage.setItem(this.#setting_name, content);
- }
- /**
- * This return current language from localStorage. When any language is
- * not loaded yet, then return default language. It also check that value
- * value from localStorage, and if it not avairable in languages storage,
- * then also return default language.
- *
- * @return {string} - Name of the current language.
- */
- get current() {
- const saved = this.#state;
- if (saved === null) {
- return this.#languages.default;
- }
- if (this.#languages.has(saved)) {
- return saved;
- }
- return this.#languages.default;
- }
- /**
- * This load phrasebook for current selected language.
- *
- * @returns {phrasebook} - Phrasebook for current language.
- */
- async load_choosen_phrasebook() {
- return await this.#languages.select(this.current);
- }
- /**
- * This return loaded languages container.
- *
- * @returns {languages} - Languages container.
- */
- get languages() {
- return this.#languages;
- }
- /**
- * This set new language for user. It also check that language exists
- * in the language container. When not exists, throws error.
- *
- * @throws {Error} - When language not exists in container.
- *
- * @param {string} name - New language to select.
- * @returns {preferences} - This object to chain.
- */
- update(name) {
- DEBUG: if (!this.#languages.has(name)) {
- let error = 'Can not set language "' + name + '" ';
- error += "because not exists in languages container.";
- throw new Error(error);
- }
- this.#state = name;
- return this;
- }
- };
- exports.preferences = preferences;
- }
- });
- // 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;
- module.exports.preferences = require_preferences().preferences;
- module.exports.selector = require_selector().selector;
- module.exports.autotranslate = require_autotranslate().autotranslate;
- } else {
- window.cx_libtranslate = {
- phrasebook: require_phrasebook().phrasebook,
- loader: require_loader().loader,
- languages: require_languages().languages,
- translation: require_translation().translation,
- preferences: require_preferences().preferences,
- selector: require_selector().selector,
- autotranslate: require_autotranslate().autotranslate
- };
- }
- }
- });
- return require_core();
- })();
- //# sourceMappingURL=cx-libtranslate.debug.js.map
|