Compare commits

...
3 Commits
Author SHA1 Message Date
Bram Kragten 088640642c Bumped version to 20261125.4 2026-12-02 13:44:49 +01:00
WendelinandClaude Opus 5.5 0819c80044 Fix plural forms being ignored in many languages (#56347)
* Keep the native Intl.PluralRules so plural forms work in every language

@formatjs/intl-pluralrules 7.1 and @formatjs/intl-numberformat 10.1 added a
should-polyfill check for formatting numeric strings with
trailingZeroDisplay: "stripToMinimum", which current browsers lack. Every
browser then replaced its native Intl.PluralRules with the polyfill, which
only carries English data, so languages like Russian and Polish lost their
"few" and "many" plural forms. Use our own checks without that feature, as
Home Assistant only passes numbers.

Fixes #56345

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018Kv7eFTWUj93p4Bx4NdNA9

* Trim the Intl polyfill check comments and test

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Remove the unused FormatJS should-polyfill paths from tsconfig

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
2026-12-02 13:44:35 +01:00
Lazizbek Ergashev bb0c3bcd80 Keep visibility when an or matches despite an error in another branch (#56336) 2026-12-02 13:44:34 +01:00
8 changed files with 115 additions and 20 deletions

No files matched your search

-4
View File
@@ -508,12 +508,8 @@ const createRspackConfig = ({
"@formatjs/intl-locale/should-polyfill.js",
"@formatjs/intl-locale/polyfill-force":
"@formatjs/intl-locale/polyfill-force.js",
"@formatjs/intl-numberformat/should-polyfill":
"@formatjs/intl-numberformat/should-polyfill.js",
"@formatjs/intl-numberformat/polyfill-force":
"@formatjs/intl-numberformat/polyfill-force.js",
"@formatjs/intl-pluralrules/should-polyfill":
"@formatjs/intl-pluralrules/should-polyfill.js",
"@formatjs/intl-pluralrules/polyfill-force":
"@formatjs/intl-pluralrules/polyfill-force.js",
"@formatjs/intl-relativetimeformat/should-polyfill":
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "home-assistant-frontend"
version = "20261125.3"
version = "20261125.4"
license = "Apache-2.0"
license-files = ["LICENSE*"]
description = "The Home Assistant frontend"
@@ -244,7 +244,7 @@ export class ConditionEvaluatorController implements ReactiveController {
return;
}
if (message.error !== undefined) {
this._serverResults[subtree.id] = false;
this._serverResults[subtree.id] = undefined;
this._subtreeErrors[subtree.id] =
typeof message.error === "string"
? message.error
@@ -265,7 +265,7 @@ export class ConditionEvaluatorController implements ReactiveController {
if (generation !== this._generation) {
return;
}
this._serverResults[subtree.id] = false;
this._serverResults[subtree.id] = undefined;
this._subtreeErrors[subtree.id] =
getWsErrorMessage(err) ?? hass.localize("ui.common.unknown_error");
this._recompute();
@@ -299,18 +299,19 @@ export class ConditionEvaluatorController implements ReactiveController {
}
};
// A failed subtree stays unknown, so it only hides the card when the
// result depends on it (a true `or` sibling still wins).
const value = this._split.evaluate(clientEvaluator, this._serverResults);
const error = firstDefined(this._subtreeErrors);
// Don't treat a server error as `false` that a client `not` would invert.
if (error !== undefined) {
if (value === undefined && error !== undefined) {
this._setResult("hidden", error);
return;
}
const value = this._split.evaluate(clientEvaluator, this._serverResults);
const result: ConditionEvaluation =
value === undefined ? "unknown" : value ? "visible" : "hidden";
this._setResult(result, firstDefined(this._subtreeTemplateErrors));
this._setResult(result, error ?? firstDefined(this._subtreeTemplateErrors));
}
private _setResult(
+4 -2
View File
@@ -3,12 +3,14 @@ import { shouldPolyfill as shouldPolyfillDisplayNames } from "@formatjs/intl-dis
import { shouldPolyfill as shouldPolyfillGetCanonicalLocales } from "@formatjs/intl-getcanonicallocales/should-polyfill";
import { shouldPolyfill as shouldPolyfillListFormat } from "@formatjs/intl-listformat/should-polyfill";
import { shouldPolyfill as shouldPolyfillLocale } from "@formatjs/intl-locale/should-polyfill";
import { shouldPolyfill as shouldPolyfillNumberFormat } from "@formatjs/intl-numberformat/should-polyfill";
import { shouldPolyfill as shouldPolyfillPluralRules } from "@formatjs/intl-pluralrules/should-polyfill";
import { shouldPolyfill as shouldPolyfillRelativeTimeFormat } from "@formatjs/intl-relativetimeformat/should-polyfill";
import { shouldPolyfill as shouldPolyfillDurationFormat } from "@formatjs/intl-durationformat/should-polyfill";
import { getLocalLanguage } from "../../util/common-translation";
import {
shouldPolyfillNumberFormat,
shouldPolyfillPluralRules,
} from "./intl-should-polyfill";
import {
polyfillLocaleData,
polyfillTimeZoneData,
@@ -0,0 +1,63 @@
// FormatJS 7.1 / 10.1 also probe for formatting numeric strings with
// trailingZeroDisplay: "stripToMinimum", which current browsers fail.
// That swapped in the English-only PluralRules polyfill everywhere; we only
// pass numbers, so leave that probe out.
const supportsLocale = (
supportedLocalesOf: (locales: string[]) => string[],
locale: string
) => supportedLocalesOf([locale]).length === 1;
export const shouldPolyfillPluralRules = (locale: string): boolean => {
try {
return !(
typeof Intl.PluralRules === "function" &&
// Older engines ignore the fraction digits when choosing the category
new Intl.PluralRules("en", { minimumFractionDigits: 2 }).select(1) !==
"one" &&
supportsLocale(Intl.PluralRules.supportedLocalesOf, locale)
);
} catch (_err) {
return true;
}
};
// Newer than the TypeScript lib this project targets
type NumberFormatMethods = Partial<
Record<"formatRange" | "formatRangeToParts" | "formatToParts", unknown>
>;
type RoundingPriorityOptions = Intl.NumberFormatOptions & {
roundingPriority?: "auto" | "morePrecision" | "lessPrecision";
};
export const shouldPolyfillNumberFormat = (locale: string): boolean => {
try {
const prototype: NumberFormatMethods = Intl.NumberFormat.prototype;
const roundingPriority: RoundingPriorityOptions = {
notation: "compact",
minimumSignificantDigits: 3,
maximumSignificantDigits: 3,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
roundingPriority: "morePrecision",
};
return !(
typeof Intl.NumberFormat === "function" &&
new Intl.NumberFormat("en", {
style: "unit",
unit: "bit",
unitDisplay: "long",
notation: "scientific",
}).format(10000) === "1E4 bits" &&
new Intl.NumberFormat("en", roundingPriority).format(100000000) ===
"100.00M" &&
typeof prototype.formatRange === "function" &&
typeof prototype.formatRangeToParts === "function" &&
typeof prototype.formatToParts === "function" &&
Intl.NumberFormat.supportedLocalesOf(["es"]).length === 1 &&
supportsLocale(Intl.NumberFormat.supportedLocalesOf, locale)
);
} catch (_err) {
return true;
}
};
@@ -173,7 +173,7 @@ describe("ConditionEvaluatorController", () => {
cond({
condition: "not",
conditions: [
{ condition: "user", users: ["other"] },
{ condition: "user", users: ["user1"] },
{ condition: "template", value_template: "{{ broken" },
],
}),
@@ -184,6 +184,27 @@ describe("ConditionEvaluatorController", () => {
expect(controller.error).toBe("bad");
});
it("shows when an or matches despite a subscription error in another branch", async () => {
const { controller } = await setup([
cond({
condition: "or",
conditions: [
{ condition: "user", users: ["user1"] },
{
condition: "or",
conditions: [
{ condition: "state", entity: "person.a", state: "home" },
{ condition: "state", entity: "sensor.missing", state: "on" },
],
},
],
}),
]);
expect(subs).toHaveLength(1);
subs[0].push({ error: "In 'or': unknown entity sensor.missing" });
expect(controller.result).toBe("visible");
});
it("surfaces template errors without overriding the result", async () => {
// Template render errors are flagged; they don't force hidden.
const { controller, last } = await setup([
@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import {
shouldPolyfillNumberFormat,
shouldPolyfillPluralRules,
} from "../../../src/resources/polyfills/intl-should-polyfill";
// The PluralRules polyfill only has English data, so replacing a working
// native Intl.PluralRules drops plural forms like "few" and "many"
describe("Intl polyfill checks", () => {
it("works", () => {
for (const locale of ["en", "de", "ru", "pl", "ar"]) {
expect(shouldPolyfillPluralRules(locale)).toBe(false);
expect(shouldPolyfillNumberFormat(locale)).toBe(false);
}
expect(shouldPolyfillPluralRules("xx")).toBe(true);
expect(shouldPolyfillNumberFormat("xx")).toBe(true);
});
});
-6
View File
@@ -118,15 +118,9 @@
"@formatjs/intl-locale/polyfill-force": [
"./node_modules/@formatjs/intl-locale/polyfill-force.js"
],
"@formatjs/intl-numberformat/should-polyfill": [
"./node_modules/@formatjs/intl-numberformat/should-polyfill.js"
],
"@formatjs/intl-numberformat/polyfill-force": [
"./node_modules/@formatjs/intl-numberformat/polyfill-force.js"
],
"@formatjs/intl-pluralrules/should-polyfill": [
"./node_modules/@formatjs/intl-pluralrules/should-polyfill.js"
],
"@formatjs/intl-pluralrules/polyfill-force": [
"./node_modules/@formatjs/intl-pluralrules/polyfill-force.js"
],