mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-25 13:57:21 +00:00
Support requesting translations for multiple integrations in one request (#12704)
* Support requesting translations for multiple integrations in one request - Requires https://github.com/home-assistant/core/pull/71979 * onboarding as well * integrations -> integration * fix cache * short return if they are all loaded * reduce * reduce * reduce
This commit is contained in:
parent
7d1c77a38f
commit
af6b0d3266
@ -52,7 +52,7 @@ export const getHassTranslations = async (
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
language: string,
|
language: string,
|
||||||
category: TranslationCategory,
|
category: TranslationCategory,
|
||||||
integration?: string,
|
integration?: string | string[],
|
||||||
config_flow?: boolean
|
config_flow?: boolean
|
||||||
): Promise<Record<string, unknown>> => {
|
): Promise<Record<string, unknown>> => {
|
||||||
const result = await hass.callWS<{ resources: Record<string, unknown> }>({
|
const result = await hass.callWS<{ resources: Record<string, unknown> }>({
|
||||||
|
@ -49,12 +49,14 @@ class OnboardingIntegrations extends LitElement {
|
|||||||
this.hass.loadBackendTranslation("title", undefined, true);
|
this.hass.loadBackendTranslation("title", undefined, true);
|
||||||
this._unsubEvents = subscribeConfigFlowInProgress(this.hass, (flows) => {
|
this._unsubEvents = subscribeConfigFlowInProgress(this.hass, (flows) => {
|
||||||
this._discovered = flows;
|
this._discovered = flows;
|
||||||
|
const integrations: Set<string> = new Set();
|
||||||
for (const flow of flows) {
|
for (const flow of flows) {
|
||||||
// To render title placeholders
|
// To render title placeholders
|
||||||
if (flow.context.title_placeholders) {
|
if (flow.context.title_placeholders) {
|
||||||
this.hass.loadBackendTranslation("config", flow.handler);
|
integrations.add(flow.handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.hass.loadBackendTranslation("config", Array.from(integrations));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,17 +156,18 @@ class HaConfigIntegrations extends SubscribeMixin(LitElement) {
|
|||||||
this._deviceRegistryEntries = entries;
|
this._deviceRegistryEntries = entries;
|
||||||
}),
|
}),
|
||||||
subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => {
|
subscribeConfigFlowInProgress(this.hass, async (flowsInProgress) => {
|
||||||
const translationsPromisses: Promise<LocalizeFunc>[] = [];
|
const integrations: Set<string> = new Set();
|
||||||
flowsInProgress.forEach((flow) => {
|
flowsInProgress.forEach((flow) => {
|
||||||
// To render title placeholders
|
// To render title placeholders
|
||||||
if (flow.context.title_placeholders) {
|
if (flow.context.title_placeholders) {
|
||||||
translationsPromisses.push(
|
integrations.add(flow.handler);
|
||||||
this.hass.loadBackendTranslation("config", flow.handler)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
this._fetchManifest(flow.handler);
|
this._fetchManifest(flow.handler);
|
||||||
});
|
});
|
||||||
await Promise.all(translationsPromisses);
|
await this.hass.loadBackendTranslation(
|
||||||
|
"config",
|
||||||
|
Array.from(integrations)
|
||||||
|
);
|
||||||
await nextRender();
|
await nextRender();
|
||||||
this._configEntriesInProgress = flowsInProgress.map((flow) => ({
|
this._configEntriesInProgress = flowsInProgress.map((flow) => ({
|
||||||
...flow,
|
...flow,
|
||||||
|
@ -242,12 +242,22 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let integrationsToLoad: string[] = [];
|
||||||
|
|
||||||
// Check if already loaded
|
// Check if already loaded
|
||||||
if (!force) {
|
if (!force) {
|
||||||
if (integration) {
|
if (integration && Array.isArray(integration)) {
|
||||||
|
integrationsToLoad = integration.filter(
|
||||||
|
(i) => !alreadyLoaded.integrations.includes(i)
|
||||||
|
);
|
||||||
|
if (!integrationsToLoad.length) {
|
||||||
|
return this.hass!.localize;
|
||||||
|
}
|
||||||
|
} else if (integration) {
|
||||||
if (alreadyLoaded.integrations.includes(integration)) {
|
if (alreadyLoaded.integrations.includes(integration)) {
|
||||||
return this.hass!.localize;
|
return this.hass!.localize;
|
||||||
}
|
}
|
||||||
|
integrationsToLoad = [integration];
|
||||||
} else if (
|
} else if (
|
||||||
configFlow ? alreadyLoaded.configFlow : alreadyLoaded.setup
|
configFlow ? alreadyLoaded.configFlow : alreadyLoaded.setup
|
||||||
) {
|
) {
|
||||||
@ -256,10 +266,8 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add to cache
|
// Add to cache
|
||||||
if (integration) {
|
if (integrationsToLoad.length) {
|
||||||
if (!alreadyLoaded.integrations.includes(integration)) {
|
alreadyLoaded.integrations.push(...integrationsToLoad);
|
||||||
alreadyLoaded.integrations.push(integration);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
alreadyLoaded.setup = true;
|
alreadyLoaded.setup = true;
|
||||||
if (configFlow) {
|
if (configFlow) {
|
||||||
@ -271,7 +279,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
|
|||||||
this.hass!,
|
this.hass!,
|
||||||
language,
|
language,
|
||||||
category,
|
category,
|
||||||
integration,
|
integrationsToLoad.length ? integrationsToLoad : undefined,
|
||||||
configFlow
|
configFlow
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user