diff --git a/application/example/i18n/formatter.mjs b/application/example/i18n/formatter.mjs new file mode 100644 index 0000000000000000000000000000000000000000..1dff047ffd672d9f28a6b2fba6943b2894e13d69 --- /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 0000000000000000000000000000000000000000..530fe729e3f928e2d5d7422faa61e12af1ed818a --- /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 0000000000000000000000000000000000000000..58a00d39eb9b9f8097f74b107338b248a4690a4c --- /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 a80a4b48912dd65d8e25e20e6ef8aa60930f0827..22f1c4f587accfce1f15452024698fcdda7efffc 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 e12871ba3091980409583ec9c5749b5426ca9afc..60fe979725a4989875fc3a70d40ceabb6b34938c 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 51cd0ecf58f4067c14a69175bf9e6e07bd135bde..7ae29800e47121002ef3eac2a7a1d4a2e222f6a1 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 a0507045a6ded08b2fcbb52e8df6789ba8b306f1..12337549fb3cf73fb42f2a541b10d9d17aae41a7 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 4d5542c17d1c4f1c3e91a76896cd49127fda5b84..1f462173f0ef4fdebfe70393d3bf4970de5f76f5 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 83f35f4eb0e85311dbf37359fffe1dab8661151e..535b13f8b1027987c29a88a8c28fb8f5097bb68a 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 e2bde3a0687ce4eece1f11799703f53eac9a5697..82d9008d09e20d3aaa7513e4bead3e96f71b61d4 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