diff --git a/application/source/i18n/formatter.mjs b/application/source/i18n/formatter.mjs index 18f4c31dd50961ad0096201a491c8c0da148295c..a30bb4470829e7826f6ff3c9d977fb1fdb6b93e1 100644 --- a/application/source/i18n/formatter.mjs +++ b/application/source/i18n/formatter.mjs @@ -5,14 +5,14 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ -import { internalSymbol } from "../constants.mjs"; -import { extend } from "../data/extend.mjs"; +import {instanceSymbol, internalSymbol} from "../constants.mjs"; +import {extend} from "../data/extend.mjs"; -import { Formatter as TextFormatter } from "../text/formatter.mjs"; -import { validateInstance, validateString } from "../types/validate.mjs"; -import { Translations } from "./translations.mjs"; +import {Formatter as TextFormatter} from "../text/formatter.mjs"; +import {validateInstance, validateString} from "../types/validate.mjs"; +import {Translations} from "./translations.mjs"; -export { Formatter }; +export {Formatter}; /** * @private @@ -41,6 +41,15 @@ class Formatter extends TextFormatter { this[internalTranslationSymbol] = validateInstance(translation, Translations); } + /** + * This method is called by the `instanceof` operator. + * @returns {symbol} + * @since 3.27.0 + */ + static get [instanceSymbol]() { + return Symbol.for("@schukai/monster/i18n/formatter@@instance"); + } + /** * @property {object} marker * @property {array} marker.open=["i18n{","${"] diff --git a/application/source/i18n/locale.mjs b/application/source/i18n/locale.mjs index af58d4bbcb946589631f78d3e4678e2b5c90dc8f..6eefd159798870e5a8972062a165902b3dd6c1ed 100644 --- a/application/source/i18n/locale.mjs +++ b/application/source/i18n/locale.mjs @@ -5,6 +5,7 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ +import {instanceSymbol} from "../constants.mjs"; import { Base } from "../types/base.mjs"; import { validateString } from "../types/validate.mjs"; import { clone } from "../util/clone.mjs"; @@ -100,6 +101,15 @@ class Locale extends Base { this[localeStringSymbol] = s.join("-"); } + /** + * This method is called by the `instanceof` operator. + * @returns {symbol} + * @since 3.27.0 + */ + static get [instanceSymbol]() { + return Symbol.for("@schukai/monster/i18n/locale@@instance"); + } + /** * @return {string} */ diff --git a/application/source/i18n/provider.mjs b/application/source/i18n/provider.mjs index c28b654b88b8e740f48d1a9a3810a7470628079e..16577cc99925c1f29e1e7264031b4f66e871a404 100644 --- a/application/source/i18n/provider.mjs +++ b/application/source/i18n/provider.mjs @@ -5,6 +5,7 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ +import {instanceSymbol} from "../constants.mjs"; import {hasObjectLink, getLinkedObjects,addToObjectLink} from "../dom/attributes.mjs"; import {getLocaleOfDocument} from "../dom/locale.mjs"; import {BaseWithOptions} from "../types/basewithoptions.mjs"; @@ -32,6 +33,16 @@ const translationsLinkSymbol = Symbol.for("@schukai/monster/i18n/translations@@l * @see {@link https://datatracker.ietf.org/doc/html/rfc3066} */ class Provider extends BaseWithOptions { + + /** + * This method is called by the `instanceof` operator. + * @returns {symbol} + * @since 3.27.0 + */ + static get [instanceSymbol]() { + return Symbol.for("@schukai/monster/i18n/provider@@instance"); + } + /** * @param {Locale|string} locale * @return {Promise} diff --git a/application/source/i18n/translations.mjs b/application/source/i18n/translations.mjs index 4e47bc7b31170961c530f518af41dbe42d76791a..8d6836938266c993ab706ee0802f54787955bfa8 100644 --- a/application/source/i18n/translations.mjs +++ b/application/source/i18n/translations.mjs @@ -5,7 +5,8 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ -import { getLinkedObjects,hasObjectLink} from "../dom/attributes.mjs"; +import {instanceSymbol} from "../constants.mjs"; +import {getLinkedObjects, hasObjectLink} from "../dom/attributes.mjs"; import {ATTRIBUTE_OBJECTLINK} from "../dom/constants.mjs"; import {getDocument} from "../dom/util.mjs"; import {Base} from "../types/base.mjs"; @@ -44,6 +45,15 @@ class Translations extends Base { this.storage = new Map(); } + /** + * This method is called by the `instanceof` operator. + * @returns {symbol} + * @since 3.27.0 + */ + static get [instanceSymbol]() { + return Symbol.for("@schukai/monster/i18n/translations@@instance"); + } + /** * Fetches a text using the specified key. * If no suitable key is found, `defaultText` is taken. @@ -107,9 +117,11 @@ class Translations extends Base { return validateString(r[keyword]); } - if (r.hasOwnProperty(DEFAULT_KEY)) { - return validateString(r[DEFAULT_KEY]); - } + // @deprecated since 2023-03-14 + // DEFAULT_KEY is undefined + // if (r.hasOwnProperty(DEFAULT_KEY)) { + // return validateString(r[DEFAULT_KEY]); + // } return validateString(defaultText); } @@ -171,7 +183,7 @@ class Translations extends Base { */ assignTranslations(translations) { validateObject(translations); - + if (translations instanceof Translations) { translations.storage.forEach((v, k) => { this.setText(k, v); @@ -202,7 +214,7 @@ function getDocumentTranslations(element) { const d = getDocument() if (!(element instanceof HTMLElement)) { - element = d.querySelector('['+ATTRIBUTE_OBJECTLINK+'~="' + translationsLinkSymbol.toString() + '"]'); + element = d.querySelector('[' + ATTRIBUTE_OBJECTLINK + '~="' + translationsLinkSymbol.toString() + '"]'); if (element === null) { throw new Error("Cannot find element with translations. Add a translations object to the document."); } @@ -215,9 +227,9 @@ function getDocumentTranslations(element) { if (!hasObjectLink(element, translationsLinkSymbol)) { throw new Error("This element has no translations."); } - + let obj = getLinkedObjects(element, translationsLinkSymbol); - + for (const t of obj) { if (t instanceof Translations) { return t;