Skip to content

Commit

Permalink
feat(ui/element/i18n): localization mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
MM25Zamanian authored and alimd committed Dec 28, 2022
1 parent 5f8f57a commit 999e1b1
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ui/element/src/mixins/i18n.ts
Original file line number Diff line number Diff line change
@@ -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<ClassType extends Constructor<LitElement>>(
superClass: ClassType,
): Constructor<I18nResourceChangeMixinInterface> & 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<I18nResourceChangeMixinInterface> & ClassType;
}
export function I18nLocaleChangeMixin<ClassType extends Constructor<LitElement>>(
superClass: ClassType,
): Constructor<I18nLocaleChangeMixinInterface> & 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<I18nLocaleChangeMixinInterface> & ClassType;
}

0 comments on commit 999e1b1

Please sign in to comment.