Skip to content

Commit

Permalink
Export Localized<> utility type
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Benedetti committed Jan 26, 2024
1 parent b26f605 commit 7be38ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/base/bk-base/bk-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {Subscription,ReplaySubject} from 'rxjs'
import type {Observable} from 'rxjs'

import type {EventBus} from '../../events'
import {Labels, Localized, LocalizedComponent, mergeLabels, solveLocale} from '../localized-components'
import {Localized} from '../../utils/i18n'
import {Labels, LocalizedComponent, mergeLabels, solveLocale} from '../localized-components'

export type Listener = (eventBus: EventBus, kickoff: Observable<0>) => Subscription
export type Bootstrapper = (eventBus: EventBus) => void
Expand Down
33 changes: 1 addition & 32 deletions src/base/localized-components.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
import {type LocalizedText, DEFAULT_LANGUAGE, getLocalizedText} from '../utils/i18n'
import {type LocalizedText, DEFAULT_LANGUAGE, getLocalizedText, Localized} from '../utils/i18n'

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

/**
* Turns string feilds of objects into `LocalizedText`
*
* For instance:
*
* ```
* Localized<{
* name: string
* nick?: string
* avatar: {file: string, size: number}
* permissions: string[]
* }>
* ```
*
* is equivalent to
*
* ```
* {
* name: LocalizedText
* nick?: LocalizedText
* avatar: {file: LocalizedText, size: number}
* permissions: string[]
* }
* ```
*/
export type Localized<L extends Record<string, unknown> | undefined> = {
[K in keyof L]: L[K] extends (string | undefined)
? LocalizedText
: (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
31 changes: 31 additions & 0 deletions src/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
export type LocalizedText = string | Record<string, string>

/**
* Turns string feilds of objects into `LocalizedText`
*
* For instance:
*
* ```
* Localized<{
* name: string
* nick?: string
* avatar: {file: string, size: number}
* permissions: string[]
* }>
* ```
*
* is equivalent to
*
* ```
* {
* name: LocalizedText
* nick?: LocalizedText
* avatar: {file: LocalizedText, size: number}
* permissions: string[]
* }
* ```
*/
export type Localized<L extends Record<string, unknown> | undefined> = {
[K in keyof L]: L[K] extends (string | undefined)
? LocalizedText
: (L[K] extends (Record<string, unknown> | undefined) ? Localized<L[K]> : L[K])
}

export const DEFAULT_LANGUAGE = 'en'

export function getLocalizedText (
Expand Down

0 comments on commit 7be38ed

Please sign in to comment.