From 4e4ab39465165ee57ae9ff3e6d4ec28dc4827709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Wed, 2 Aug 2023 12:28:04 +0200 Subject: [PATCH] rename Translation types to set --- .../src/libs/localization-api/index.ts | 2 +- .../localization-api/localize.controller.ts | 46 +++++++++++-------- .../src/libs/localization-api/manager.ts | 16 ++++--- .../registry/translation.registry.ts | 4 +- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/localization-api/index.ts b/src/Umbraco.Web.UI.Client/src/libs/localization-api/index.ts index ea134f4fa2..139e9726f8 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/localization-api/index.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/localization-api/index.ts @@ -1,3 +1,3 @@ export * from './registry/translation.registry.js'; export * from './localize.controller.js'; -export { registerTranslation, type DefaultTranslation, type Translation } from './manager.js'; +export { registerTranslation } from './manager.js'; diff --git a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localize.controller.ts b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localize.controller.ts index 169220d40c..c99ddc1d58 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localize.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localize.controller.ts @@ -12,17 +12,18 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import { - DefaultTranslation, + DefaultTranslationSet, FunctionParams, - Translation, + TranslationSet, connectedElements, documentDirection, documentLanguage, fallback, translations, } from './manager.js'; -import { UmbController, UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api'; +import { UmbController, UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; +const LocalizeControllerAlias = Symbol(); /** * The UmbLocalizeController enables localization for your element. * @@ -41,31 +42,36 @@ import { UmbController, UmbControllerHostElement } from '@umbraco-cms/backoffice * } * ``` */ -export class UmbLocalizeController implements UmbController { - host; - controllerAlias = 'localize'; +export class UmbLocalizeController + implements UmbController +{ + #host; + #hostEl; + controllerAlias = LocalizeControllerAlias; - constructor(host: UmbControllerHostElement) { - this.host = host; - this.host.addController(this); + constructor(host: UmbControllerHost) { + this.#host = host; + this.#hostEl = host.getHostElement() as HTMLElement; + this.#host.addController(this); } hostConnected(): void { - if (connectedElements.has(this.host)) { + if (connectedElements.has(this.#hostEl)) { return; } - connectedElements.add(this.host); + connectedElements.add(this.#hostEl); } hostDisconnected(): void { - connectedElements.delete(this.host); + connectedElements.delete(this.#hostEl); } destroy(): void { // We do not need to call delete here, as hostDisconnected is called when controller is removed. //connectedElements.delete(this.host); - this.host.removeController(this); + this.#host.removeController(this); + this.#hostEl = undefined as any; } /** @@ -73,7 +79,7 @@ export class UmbLocalizeControllertranslations.get(`${language}-${region}`); - const secondary = translations.get(language); + const primary = translations.get(`${language}-${region}`); + const secondary = translations.get(language); return { locale, language, region, primary, secondary }; } /** Outputs a translated term. */ - term(key: K, ...args: FunctionParams): string { + term(key: K, ...args: FunctionParams): string { const { primary, secondary } = this.getTranslationData(this.lang()); let term: any; @@ -104,8 +110,8 @@ export class UmbLocalizeController = T extends (...args: infer U) => string ? U : []; -export interface Translation { +export interface TranslationSet { $code: string; // e.g. en, en-GB $dir: 'ltr' | 'rtl'; } -export interface DefaultTranslation extends Translation { - [key: string]: any; +export interface DefaultTranslationSet extends TranslationSet { + [key: string]: UmbTranslationEntry; } export const connectedElements = new Set(); const documentElementObserver = new MutationObserver(update); -export const translations: Map = new Map(); +export const translations: Map = new Map(); export let documentDirection = document.documentElement.dir || 'ltr'; export let documentLanguage = document.documentElement.lang || navigator.language; -export let fallback: Translation; +export let fallback: TranslationSet; // Watch for changes on documentElementObserver.observe(document.documentElement, { @@ -38,7 +39,7 @@ documentElementObserver.observe(document.documentElement, { }); /** Registers one or more translations */ -export function registerTranslation(...translation: Translation[]) { +export function registerTranslation(...translation: TranslationSet[]) { translation.map((t) => { const code = t.$code.toLowerCase(); @@ -65,6 +66,7 @@ export function update() { [...connectedElements.keys()].map((el) => { if (typeof (el as LitElement).requestUpdate === 'function') { + // TODO: We might want to implement a specific Umbraco method for informing about this. and then make the default UmbLitElement call requestUpdate..? Cause then others can implement their own solution? (el as LitElement).requestUpdate(); } }); diff --git a/src/Umbraco.Web.UI.Client/src/libs/localization-api/registry/translation.registry.ts b/src/Umbraco.Web.UI.Client/src/libs/localization-api/registry/translation.registry.ts index 10b39ac3d2..96ee85528c 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/localization-api/registry/translation.registry.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/localization-api/registry/translation.registry.ts @@ -1,4 +1,4 @@ -import { Translation, registerTranslation } from '../manager.js'; +import { TranslationSet, registerTranslation } from '../manager.js'; import { hasDefaultExport, loadExtension } from '@umbraco-cms/backoffice/extension-api'; import { UmbBackofficeExtensionRegistry, @@ -41,7 +41,7 @@ export class UmbTranslationRegistry { } // Notify subscribers that the inner dictionary has changed. - const translation: Translation = { + const translation: TranslationSet = { $code: userCulture, $dir: extension.meta.direction ?? 'ltr', ...innerDictionary,