Separate supervisor localize key exceptions and disallow string (#13317)

This commit is contained in:
Steve Repsher 2022-08-05 09:39:19 -04:00 committed by GitHub
parent 150bc00c31
commit 1d47303127
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 17 deletions

View File

@ -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`
<ha-icon-button
.label=${this.supervisor.localize(
"snapshot.delete_selected"
"backup.delete_selected"
)}
.path=${mdiDelete}
id="delete-btn"

View File

@ -22,10 +22,11 @@ import {
Supervisor,
SupervisorObject,
supervisorCollection,
SupervisorKeys,
} from "../../src/data/supervisor/supervisor";
import { ProvideHassLitMixin } from "../../src/mixins/provide-hass-lit-mixin";
import { urlSyncMixin } from "../../src/state/url-sync-mixin";
import { HomeAssistant, Route, TranslationDict } from "../../src/types";
import { HomeAssistant, Route } from "../../src/types";
import { getTranslation } from "../../src/util/common-translation";
declare global {
@ -124,7 +125,7 @@ export class SupervisorBaseElement extends urlSyncMixin(
this.supervisor = {
...this.supervisor,
localize: await computeLocalize<TranslationDict["supervisor"]>(
localize: await computeLocalize<SupervisorKeys>(
this.constructor.prototype,
language,
{

View File

@ -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<Omit<TranslationDict, "supervisor">>
| `${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<string, any>,
Key extends keyof T = keyof T
> = Key extends string
@ -29,10 +29,8 @@ type FlattenObjectKeys<
: `${Key}`
: never;
export type LocalizeFunc<
Dict extends Record<string, unknown> = TranslationDict
> = (
key: FlattenObjectKeys<Dict> | LocalizeKeyExceptions,
export type LocalizeFunc<Keys extends string = LocalizeKeys> = (
key: Keys,
...args: any[]
) => string;
@ -94,14 +92,12 @@ export const polyfillsLoaded =
* }
*/
export const computeLocalize = async <
Dict extends Record<string, unknown> = TranslationDict
>(
export const computeLocalize = async <Keys extends string = LocalizeKeys>(
cache: any,
language: string,
resources: Resources,
formats?: FormatsType
): Promise<LocalizeFunc<Dict>> => {
): Promise<LocalizeFunc<Keys>> => {
if (polyfillsLoaded) {
await polyfillsLoaded;
}

View File

@ -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<TranslationDict["supervisor"]>
| `${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<TranslationDict["supervisor"]>;
localize: LocalizeFunc<SupervisorKeys>;
}
export const supervisorApiWsRequest = <T>(

View File

@ -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",