From aa58d737283ee8abb8d0f006b5aa8e8a0828edcb Mon Sep 17 00:00:00 2001 From: Claudio Benedetti Date: Fri, 26 Jan 2024 16:51:54 +0100 Subject: [PATCH] `defaultLocale` is protected + Component locale supports undefined --- src/base/__tests__/bk-base.test.ts | 4 +++ .../__tests__/localized-components.tets.ts | 4 +-- src/base/bk-base/bk-base.ts | 2 +- src/base/localized-components.ts | 27 ++++++++++++++----- test/e2e/localized.spec.ts | 4 +-- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/base/__tests__/bk-base.test.ts b/src/base/__tests__/bk-base.test.ts index b09c5f4..287a707 100644 --- a/src/base/__tests__/bk-base.test.ts +++ b/src/base/__tests__/bk-base.test.ts @@ -57,6 +57,7 @@ describe('bk-base tests', () => { describe('localized-component tests', () => { it('should not localize', () => { const base = new BkBase() + // @ts-expect-error access private field expect(base.defaultLocale).toBeUndefined() expect(base.customLocale).toBeUndefined() expect(base.locale).toBeUndefined() @@ -73,6 +74,7 @@ describe('bk-base tests', () => { it('should localize with default', () => { const base = new BkBase() + // @ts-expect-error access private field base.defaultLocale = {title: 'Title', subtitle: 'Subtitle'} base.customLocale = { title: {en: 'Title-custom', it: 'Titolo-custom'}, @@ -83,6 +85,7 @@ describe('bk-base tests', () => { it('should initialize localize', () => { const base = new BkBase() + // @ts-expect-error access private field base.defaultLocale = {title: 'Title', subtitle: 'Subtitle'} base.locale = {title: 'Title2'} expect(base.locale).toStrictEqual({title: 'Title2'}) @@ -107,6 +110,7 @@ describe('bk-base tests', () => { Object.defineProperty(window, 'navigator', {writable: true, value: {language: 'it'}}) const base = new BkBase() + // @ts-expect-error access private field base.defaultLocale = {title: 'Title', subtitle: 'Subtitle'} base.customLocale = { title: {en: 'Title-custom'}, diff --git a/src/base/__tests__/localized-components.tets.ts b/src/base/__tests__/localized-components.tets.ts index deea3a7..2141b2f 100644 --- a/src/base/__tests__/localized-components.tets.ts +++ b/src/base/__tests__/localized-components.tets.ts @@ -27,7 +27,7 @@ describe('localized-components tests - solveLocale', () => { badge: 'badge-en', } } - const it_ = { + const ita = { title: 'title-it', subtitle: { subtitle: 'subtitle-it', @@ -44,7 +44,7 @@ describe('localized-components tests - solveLocale', () => { it.each([ [locale, 'en', en], - [locale, 'it', it_], + [locale, 'it', ita], [locale, 'es', es], [locale, 'fr', en], [locale, undefined, en], diff --git a/src/base/bk-base/bk-base.ts b/src/base/bk-base/bk-base.ts index afd0cf6..c5f045b 100644 --- a/src/base/bk-base/bk-base.ts +++ b/src/base/bk-base/bk-base.ts @@ -80,7 +80,6 @@ export class BkBase extends LitElement implements Loc this._eventBus = e } - defaultLocale?: L | undefined @property({attribute: false}) set customLocale(l: Localized) { this._locale = mergeLabels(solveLocale(l), this.defaultLocale) @@ -120,6 +119,7 @@ export class BkBase extends LitElement implements Loc this._subscription = s } + protected defaultLocale?: L | undefined private _locale?: L set locale (l: L | undefined) { this._locale = l diff --git a/src/base/localized-components.ts b/src/base/localized-components.ts index 6ef6f06..5b886f3 100644 --- a/src/base/localized-components.ts +++ b/src/base/localized-components.ts @@ -1,7 +1,7 @@ import {type LocalizedText, DEFAULT_LANGUAGE, getLocalizedText} from '../utils/i18n' export type Labels = { - [key: string]: string | Labels + [key: string]: string | undefined | Labels } /** @@ -9,19 +9,34 @@ export type Labels = { * * For instance: * - * `Localized<{name: string; address: {street: string, nr: number}}>` + * ``` + * Localized<{ + * name: string + * nick?: string + * avatar: {file: string, size: number} + * permissions: string[] + * }> + * ``` * * is equivalent to * - * `{name: LocalizedText; address: {street: LocalizedText, nr: number}}` + * ``` + * { + * name: LocalizedText + * nick?: LocalizedText + * avatar: {file: LocalizedText, size: number} + * permissions: string[] + * } + * ``` */ -export type Localized = { - [K in keyof L]: L[K] extends string +export type Localized | undefined> = { + [K in keyof L]: L[K] extends (string | undefined) ? LocalizedText - : (L[K] extends Labels ? Localized : L[K]) + : (L[K] extends (Record | undefined) ? Localized : L[K]) } export interface LocalizedComponent { + customLocale?: Localized locale?: L } diff --git a/test/e2e/localized.spec.ts b/test/e2e/localized.spec.ts index 8ba5e7e..8952c83 100644 --- a/test/e2e/localized.spec.ts +++ b/test/e2e/localized.spec.ts @@ -60,7 +60,7 @@ class LocalComponent extends BkComponent, ComponentLabels> { @customElement('defaulted-localized-component') class WithDefaultLocalComponent extends LocalComponent { - defaultLocale: ComponentLabels = { + protected defaultLocale: ComponentLabels = { name: 'default-name', body: { text: 'default-text', @@ -110,7 +110,7 @@ describe('localization tests', () => { text: 'custom-text', badge: 'custom-badge' } - } as ComponentLabels + } } .buttonlabel=${'Button'} >`