From 6437439b0802db61767d06cd7b7f54dc4d45a2df Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Tue, 9 Aug 2022 19:17:49 +0200 Subject: [PATCH] chore: commit save point --- application/example/i18n/formatter.mjs | 10 ++++++ application/example/i18n/providers/fetch.mjs | 5 +++ application/example/i18n/translations.mjs | 20 +++++++++++ application/source/i18n/formatter.mjs | 28 ++------------- application/source/i18n/locale.mjs | 10 ------ application/source/i18n/namespace.mjs | 2 -- application/source/i18n/provider.mjs | 10 ------ application/source/i18n/providers/fetch.mjs | 19 +---------- .../source/i18n/providers/namespace.mjs | 2 -- application/source/i18n/translations.mjs | 34 +------------------ 10 files changed, 39 insertions(+), 101 deletions(-) create mode 100644 application/example/i18n/formatter.mjs create mode 100644 application/example/i18n/providers/fetch.mjs create mode 100644 application/example/i18n/translations.mjs diff --git a/application/example/i18n/formatter.mjs b/application/example/i18n/formatter.mjs new file mode 100644 index 000000000..1dff047ff --- /dev/null +++ b/application/example/i18n/formatter.mjs @@ -0,0 +1,10 @@ +import {Formatter} from '@schukai/monster/source/i18n/formatter.mjs'; +import {Translations} from '@schukai/monster/source/i18n/translations.mjs'; + +const translations = new Translations('en') + .assignTranslations({ + thekey: "${animal} has eaten the ${food}!" + }); + +new Formatter({}, translations).format("thekey:animal=dog::food=cake") +// ↦ dog has eaten the cake! diff --git a/application/example/i18n/providers/fetch.mjs b/application/example/i18n/providers/fetch.mjs new file mode 100644 index 000000000..530fe729e --- /dev/null +++ b/application/example/i18n/providers/fetch.mjs @@ -0,0 +1,5 @@ +import {Fetch} from '@schukai/monster/source/i18n/providers/fetch.mjs'; + +// fetch from API +const translation = new Fetch('https://example.com/${language}.json').getTranslation('en-GB'); +// ↦ https://example.com/en.json diff --git a/application/example/i18n/translations.mjs b/application/example/i18n/translations.mjs new file mode 100644 index 000000000..58a00d39e --- /dev/null +++ b/application/example/i18n/translations.mjs @@ -0,0 +1,20 @@ +import {Translations} from '@schukai/monster/source/i18n/translations.mjs'; +import {parseLocale} from '@schukai/monster/source/i18n/locale.mjs'; + +const translation = new Translations(parseLocale('en-GB')); + +translation.assignTranslations({ + text1: "click", + text2: { + 'one': 'click once', + 'other': 'click n times' + } +}); + +console.log(translation.getText('text1')); +// ↦ click + +console.log(translation.getPluralRuleText('text2', 1)); +// -> click once +console.log(translation.getPluralRuleText('text2', 2)); +// -> click n times diff --git a/application/source/i18n/formatter.mjs b/application/source/i18n/formatter.mjs index a80a4b489..22f1c4f58 100644 --- a/application/source/i18n/formatter.mjs +++ b/application/source/i18n/formatter.mjs @@ -1,5 +1,3 @@ - - /** * Copyright schukai GmbH and contributors 2022. All Rights Reserved. * Node module: @schukai/monster @@ -7,8 +5,6 @@ * 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"; @@ -27,26 +23,7 @@ const internalTranslationSymbol = Symbol('internalTranslation') /** * The Formatter extends the Text.Formatter with the possibility to replace the key by a translation. * - * ``` - * <script type="module"> - * import {Formatter} from '@schukai/monster/source/i18n/formatter.mjs'; - * new Formatter() - * </script> - * ``` - * - * @example - * - * import {Formatter} from '@schukai/monster/source/i18n/formatter.mjs'; - * import {Translations} from '@schukai/monster/source/i18n/translations.mjs'; - * - * const translations = new Translations('en') - * .assignTranslations({ - * thekey: "${animal} has eaten the ${food}!" - * }); - * - * new Formatter({}, translations).format("thekey:animal=dog::food=cake") - * // ↦ dog has eaten the cake! - * + * @externalExample ../../example/i18n/formatter.mjs * @license AGPLv3 * @since 1.26.0 * @copyright schukai GmbH @@ -114,8 +91,7 @@ class Formatter extends TextFormatter { throw new Error("the closing marker is missing") } } - - + const parts = validateString(text).split('::') const translationKey = parts.shift().trim(); // key value delimiter const parameter = parts.join('::').trim(); diff --git a/application/source/i18n/locale.mjs b/application/source/i18n/locale.mjs index e12871ba3..60fe97972 100644 --- a/application/source/i18n/locale.mjs +++ b/application/source/i18n/locale.mjs @@ -1,5 +1,3 @@ - - /** * Copyright schukai GmbH and contributors 2022. All Rights Reserved. * Node module: @schukai/monster @@ -7,7 +5,6 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ - import {Base} from "../types/base.mjs"; import {validateString} from "../types/validate.mjs"; import {clone} from "../util/clone.mjs"; @@ -29,13 +26,6 @@ const localeStringSymbol = Symbol('localeString'); /** * The Locale class is a base class for the language classes. * - * ``` - * <script type="module"> - * import {Locale} from '@schukai/monster/source/i18n/locale.mjs'; - * new Locale() - * </script> - * ``` - * * RFC * * ``` diff --git a/application/source/i18n/namespace.mjs b/application/source/i18n/namespace.mjs index 51cd0ecf5..7ae29800e 100644 --- a/application/source/i18n/namespace.mjs +++ b/application/source/i18n/namespace.mjs @@ -1,5 +1,3 @@ - - /** * In this namespace you will find classes and methods for handling locale and localized texts. * diff --git a/application/source/i18n/provider.mjs b/application/source/i18n/provider.mjs index a0507045a..12337549f 100644 --- a/application/source/i18n/provider.mjs +++ b/application/source/i18n/provider.mjs @@ -1,5 +1,3 @@ - - /** * Copyright schukai GmbH and contributors 2022. All Rights Reserved. * Node module: @schukai/monster @@ -7,7 +5,6 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ - import {BaseWithOptions} from "../types/basewithoptions.mjs"; import {Locale} from "./locale.mjs" import {Translations} from "./translations.mjs" @@ -17,13 +14,6 @@ export {Provider} /** * A provider makes a translation object available. * - * ``` - * <script type="module"> - * import {Provider} from '@schukai/monster/source/i18n/provider.mjs'; - * new Provider() - * </script> - * ``` - * * @license AGPLv3 * @since 1.13.0 * @copyright schukai GmbH diff --git a/application/source/i18n/providers/fetch.mjs b/application/source/i18n/providers/fetch.mjs index 4d5542c17..1f462173f 100644 --- a/application/source/i18n/providers/fetch.mjs +++ b/application/source/i18n/providers/fetch.mjs @@ -1,5 +1,3 @@ - - /** * Copyright schukai GmbH and contributors 2022. All Rights Reserved. * Node module: @schukai/monster @@ -7,7 +5,6 @@ * 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 {Formatter} from "../../text/formatter.mjs"; @@ -23,21 +20,7 @@ export {Fetch} /** * The fetch provider retrieves a JSON file from the given URL and returns a translation object. * - * ``` - * <script type="module"> - * import {Fetch} from '@schukai/monster/source/i18n/providers/fetch.mjs'; - * new Fetch() - * </script> - * ``` - * - * @example <caption>das ist ein test</caption> - * - * import {Fetch} from '@schukai/monster/source/i18n/providers/fetch.mjs'; - * - * // fetch from API - * const translation = new Fetch('https://example.com/${language}.json').getTranslation('en-GB'); - * // ↦ https://example.com/en.json - * + * @externalExample ../../example/i18n/providers/fetch.mjs * @license AGPLv3 * @since 1.13.0 * @copyright schukai GmbH diff --git a/application/source/i18n/providers/namespace.mjs b/application/source/i18n/providers/namespace.mjs index 83f35f4eb..535b13f8b 100644 --- a/application/source/i18n/providers/namespace.mjs +++ b/application/source/i18n/providers/namespace.mjs @@ -1,5 +1,3 @@ - - /** * In this namespace you will find classes and methods for handling locale and localized texts. * diff --git a/application/source/i18n/translations.mjs b/application/source/i18n/translations.mjs index e2bde3a06..82d9008d0 100644 --- a/application/source/i18n/translations.mjs +++ b/application/source/i18n/translations.mjs @@ -1,5 +1,3 @@ - - /** * Copyright schukai GmbH and contributors 2022. All Rights Reserved. * Node module: @schukai/monster @@ -7,7 +5,6 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ - import {Base} from "../types/base.mjs"; import {isObject, isString} from "../types/is.mjs"; import {validateInstance, validateInteger, validateObject, validateString} from "../types/validate.mjs"; @@ -18,36 +15,7 @@ export {Translations} /** * With this class you can manage translations and access the keys. * - * ``` - * <script type="module"> - * import {Translations} from '@schukai/monster/source/i18n/translations.mjs'; - * new Translations() - * </script> - * ``` - * - * @example - * - * import {Translations} from '@schukai/monster/source/i18n/translations.mjs'; - * import {parseLocale} from '@schukai/monster/source/i18n/locale.mjs'; - * - * const translation = new Translations(parseLocale('en-GB')); - * - * translation.assignTranslations({ - * text1: "click", - * text2: { - * 'one': 'click once', - * 'other': 'click n times' - * } - * }); - * - * console.log(translation.getText('text1')); - * // ↦ click - * - * console.log(translation.getPluralRuleText('text2',1)); - * // -> click once - * console.log(translation.getPluralRuleText('text2',2)); - * // -> click n times - * + * @externalExample ../../example/i18n/translations.mjs * @license AGPLv3 * @since 1.13.0 * @copyright schukai GmbH -- GitLab