From 999e1b11f0af55704fdd5859398908aeedbb833c Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Tue, 27 Dec 2022 23:40:22 +0330 Subject: [PATCH] feat(ui/element/i18n): localization mixin --- ui/element/src/mixins/i18n.ts | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ui/element/src/mixins/i18n.ts diff --git a/ui/element/src/mixins/i18n.ts b/ui/element/src/mixins/i18n.ts new file mode 100644 index 000000000..d3ee6ae06 --- /dev/null +++ b/ui/element/src/mixins/i18n.ts @@ -0,0 +1,53 @@ +import {l10n} from '@alwatr/i18n'; +import {LitElement} from 'lit'; + +import type {Constructor} from '../type.js'; +import type {ListenerInterface} from '@alwatr/signal'; + +export declare class I18nResourceChangeMixinInterface extends LitElement { + private __i18nResourceChangeMixinListener?: ListenerInterface<'l10n-resource-change'>; +} +export declare class I18nLocaleChangeMixinInterface extends LitElement { + private __i18nLocaleChangeMixinListener?: ListenerInterface<'locale-change'>; +} + +export function I18nResourceChangeMixin>( + superClass: ClassType, +): Constructor & ClassType { + class I18nResourceChangeMixinClass extends superClass { + private __i18nResourceChangeMixinListener = l10n.resourceChangeSignal.addListener(() => this.requestUpdate()); + + override disconnectedCallback(): void { + super.disconnectedCallback(); + + this.__i18nResourceChangeMixinListener.remove(); + } + } + + return I18nResourceChangeMixinClass as unknown as Constructor & ClassType; +} +export function I18nLocaleChangeMixin>( + superClass: ClassType, +): Constructor & ClassType { + class I18nLocaleChangeMixinClass extends superClass { + private __i18nLocaleChangeMixinListener = l10n.localeChangeSignal.addListener((locale) => { + this.dir = locale.direction; + }); + + override connectedCallback(): void { + super.connectedCallback(); + + if (l10n.localeChangeSignal.value != null) { + this.dir = l10n.localeChangeSignal.value.direction; + } + } + + override disconnectedCallback(): void { + super.disconnectedCallback(); + + this.__i18nLocaleChangeMixinListener.remove(); + } + } + + return I18nLocaleChangeMixinClass as unknown as Constructor & ClassType; +}