Skip to content

Commit

Permalink
defaultLocale is protected + Component locale supports undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Benedetti committed Jan 26, 2024
1 parent efcd773 commit aa58d73
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/base/__tests__/bk-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'},
Expand All @@ -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'})
Expand All @@ -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'},
Expand Down
4 changes: 2 additions & 2 deletions src/base/__tests__/localized-components.tets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('localized-components tests - solveLocale', () => {
badge: 'badge-en',
}
}
const it_ = {
const ita = {
title: 'title-it',
subtitle: {
subtitle: 'subtitle-it',
Expand All @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion src/base/bk-base/bk-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export class BkBase<L extends Labels = Labels> extends LitElement implements Loc
this._eventBus = e
}

defaultLocale?: L | undefined
@property({attribute: false})
set customLocale(l: Localized<L>) {
this._locale = mergeLabels(solveLocale(l), this.defaultLocale)
Expand Down Expand Up @@ -120,6 +119,7 @@ export class BkBase<L extends Labels = Labels> extends LitElement implements Loc
this._subscription = s
}

protected defaultLocale?: L | undefined
private _locale?: L
set locale (l: L | undefined) {
this._locale = l
Expand Down
27 changes: 21 additions & 6 deletions src/base/localized-components.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import {type LocalizedText, DEFAULT_LANGUAGE, getLocalizedText} from '../utils/i18n'

export type Labels = {
[key: string]: string | Labels
[key: string]: string | undefined | Labels
}

/**
* Turns string feilds of objects into `LocalizedText`
*
* 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<L extends Labels> = {
[K in keyof L]: L[K] extends string
export type Localized<L extends Record<string, unknown> | undefined> = {
[K in keyof L]: L[K] extends (string | undefined)
? LocalizedText
: (L[K] extends Labels ? Localized<L[K]> : L[K])
: (L[K] extends (Record<string, unknown> | undefined) ? Localized<L[K]> : L[K])
}

export interface LocalizedComponent<L extends Labels = Labels> {
customLocale?: Localized<L>
locale?: L
}

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/localized.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LocalComponent extends BkComponent<Record<string, any>, ComponentLabels> {

@customElement('defaulted-localized-component')
class WithDefaultLocalComponent extends LocalComponent {
defaultLocale: ComponentLabels = {
protected defaultLocale: ComponentLabels = {
name: 'default-name',
body: {
text: 'default-text',
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('localization tests', () => {
text: 'custom-text',
badge: 'custom-badge'
}
} as ComponentLabels
}
}
.buttonlabel=${'Button'}
></localized-component>`
Expand Down

0 comments on commit aa58d73

Please sign in to comment.