From 1d47303127b62c635b7fa7c29b287fb81c51ad56 Mon Sep 17 00:00:00 2001 From: Steve Repsher Date: Fri, 5 Aug 2022 09:39:19 -0400 Subject: [PATCH] Separate supervisor localize key exceptions and disallow string (#13317) --- hassio/src/backups/hassio-backups.ts | 4 ++-- hassio/src/supervisor-base-element.ts | 5 +++-- src/common/translations/localize.ts | 18 +++++++----------- src/data/supervisor/supervisor.ts | 11 +++++++++-- src/translations/en.json | 1 + 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/hassio/src/backups/hassio-backups.ts b/hassio/src/backups/hassio-backups.ts index 99d2869f8b..f2b0466623 100644 --- a/hassio/src/backups/hassio-backups.ts +++ b/hassio/src/backups/hassio-backups.ts @@ -176,7 +176,7 @@ export class HassioBackups extends LitElement { : supervisorTabs(this.hass)} .hass=${this.hass} .localizeFunc=${this.supervisor.localize} - .searchLabel=${this.supervisor.localize("search")} + .searchLabel=${this.supervisor.localize("backup.search")} .noDataText=${this.supervisor.localize("backup.no_backups")} .narrow=${this.narrow} .route=${this.route} @@ -240,7 +240,7 @@ export class HassioBackups extends LitElement { : html` ( + localize: await computeLocalize( this.constructor.prototype, language, { diff --git a/src/common/translations/localize.ts b/src/common/translations/localize.ts index 06a16904f1..13c5a2e49b 100644 --- a/src/common/translations/localize.ts +++ b/src/common/translations/localize.ts @@ -9,18 +9,18 @@ import { getLocalLanguage } from "../../util/common-translation"; // Exclude some patterns from key type checking for now // These are intended to be removed as errors are fixed // Fixing component category will require tighter definition of types from backend and/or web socket -type LocalizeKeyExceptions = +export type LocalizeKeys = + | FlattenObjectKeys> | `${string}` | `panel.${string}` | `state.${string}` | `state_attributes.${string}` | `state_badge.${string}` | `ui.${string}` - | `${keyof TranslationDict["supervisor"]}.${string}` | `component.${string}`; // Tweaked from https://www.raygesualdo.com/posts/flattening-object-keys-with-typescript-types -type FlattenObjectKeys< +export type FlattenObjectKeys< T extends Record, Key extends keyof T = keyof T > = Key extends string @@ -29,10 +29,8 @@ type FlattenObjectKeys< : `${Key}` : never; -export type LocalizeFunc< - Dict extends Record = TranslationDict -> = ( - key: FlattenObjectKeys | LocalizeKeyExceptions, +export type LocalizeFunc = ( + key: Keys, ...args: any[] ) => string; @@ -94,14 +92,12 @@ export const polyfillsLoaded = * } */ -export const computeLocalize = async < - Dict extends Record = TranslationDict ->( +export const computeLocalize = async ( cache: any, language: string, resources: Resources, formats?: FormatsType -): Promise> => { +): Promise> => { if (polyfillsLoaded) { await polyfillsLoaded; } diff --git a/src/data/supervisor/supervisor.ts b/src/data/supervisor/supervisor.ts index 62812b2c59..2610c76fda 100644 --- a/src/data/supervisor/supervisor.ts +++ b/src/data/supervisor/supervisor.ts @@ -1,6 +1,9 @@ import { Connection, getCollection } from "home-assistant-js-websocket"; import { Store } from "home-assistant-js-websocket/dist/store"; -import { LocalizeFunc } from "../../common/translations/localize"; +import { + FlattenObjectKeys, + LocalizeFunc, +} from "../../common/translations/localize"; import { HomeAssistant, TranslationDict } from "../../types"; import { HassioAddonsInfo } from "../hassio/addon"; import { HassioHassOSInfo, HassioHostInfo } from "../hassio/host"; @@ -57,6 +60,10 @@ export interface SupervisorEvent { [key: string]: any; } +export type SupervisorKeys = + | FlattenObjectKeys + | `${keyof TranslationDict["supervisor"]}.${string}`; + export interface Supervisor { host: HassioHostInfo; supervisor: HassioSupervisorInfo; @@ -67,7 +74,7 @@ export interface Supervisor { os: HassioHassOSInfo; addon: HassioAddonsInfo; store: SupervisorStore; - localize: LocalizeFunc; + localize: LocalizeFunc; } export const supervisorApiWsRequest = ( diff --git a/src/translations/en.json b/src/translations/en.json index 13084e5d84..7c7cb97831 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -4955,6 +4955,7 @@ } }, "backup": { + "search": "[%key:ui::panel::config::backup::picker::search%]", "no_backups": "You don't have any backups yet.", "create_blocked_not_running": "Creating a backup is not possible right now because the system is in {state} state.", "delete_selected": "Delete selected backups",