fix(i18n): numbers, units, dates and order in the UI language, and after a switch (E30 S3)
Every locale formatter takes the language as an argument. Under the React
Compiler a formatter that read the i18n singleton was memoized on its other
arguments, so a card kept "2 hr 4 min" after a switch to Hungarian; plexLang()
had the same flaw and left Plex queries in the old language. Components now
take the language from useTranslation, and the signature makes forgetting it
a type error.
- ratings, file sizes, stream stats: the language's decimal mark ("8,3")
- view and subscriber counts compact in the language ("1,2 E")
- seconds as the language's unit ("6 mp") in the sliders and the stats
- a person's birth and death dates as dates, not ISO strings
- A-Z lists by the shown name in the language's alphabet (compareText),
the tag catalog re-sorted by its translated names
- lint local/locale-format (was date-format) also refuses Intl.NumberFormat
outside lib/format.ts, localeCompare, and a component reading the language
off the i18n singleton
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
// format.ts boots i18n on import (localStorage); the formatters here take the language as an argument.
|
||||
vi.mock("../i18n", () => ({ default: { t: (k: string) => k } }));
|
||||
import {
|
||||
buildStatRows,
|
||||
createFragMeter,
|
||||
@@ -17,25 +20,28 @@ const DASH = "—";
|
||||
|
||||
describe("plexStats formatters", () => {
|
||||
it("fmtBitrate scales kbps/Mbps and dashes zero/undefined", () => {
|
||||
expect(fmtBitrate(undefined)).toBe(DASH);
|
||||
expect(fmtBitrate(0)).toBe(DASH);
|
||||
expect(fmtBitrate(820_000)).toBe("820 kbps");
|
||||
expect(fmtBitrate(4_200_000)).toBe("4.2 Mbps");
|
||||
expect(fmtBitrate(1_000_000)).toBe("1.0 Mbps");
|
||||
expect(fmtBitrate(undefined, "en")).toBe(DASH);
|
||||
expect(fmtBitrate(0, "en")).toBe(DASH);
|
||||
expect(fmtBitrate(820_000, "en")).toBe("820 kbps");
|
||||
expect(fmtBitrate(4_200_000, "en")).toBe("4.2 Mbps");
|
||||
expect(fmtBitrate(1_000_000, "en")).toBe("1.0 Mbps");
|
||||
expect(fmtBitrate(4_200_000, "hu")).toBe("4,2 Mbps"); // E30 S3: the UI language's decimal mark
|
||||
});
|
||||
|
||||
it("fmtBufferSecs shows one decimal, dashes negatives/non-finite", () => {
|
||||
expect(fmtBufferSecs(12.34)).toBe("12.3 s");
|
||||
expect(fmtBufferSecs(0)).toBe("0.0 s");
|
||||
expect(fmtBufferSecs(-1)).toBe(DASH);
|
||||
expect(fmtBufferSecs(Infinity)).toBe(DASH);
|
||||
expect(fmtBufferSecs(12.34, "en")).toBe("12.3 sec");
|
||||
expect(fmtBufferSecs(0, "en")).toBe("0.0 sec");
|
||||
expect(fmtBufferSecs(12.34, "hu")).toBe("12,3 mp");
|
||||
expect(fmtBufferSecs(-1, "en")).toBe(DASH);
|
||||
expect(fmtBufferSecs(Infinity, "en")).toBe(DASH);
|
||||
});
|
||||
|
||||
it("fmtFrames shows ratio + drop %, dashes when nothing decoded", () => {
|
||||
expect(fmtFrames(0, 0)).toBe(DASH);
|
||||
expect(fmtFrames(0, 1000)).toBe("0 / 1000 (0.00%)");
|
||||
expect(fmtFrames(5, 1000)).toBe("5 / 1000 (0.50%)");
|
||||
expect(fmtFrames(30, 1000)).toBe("30 / 1000 (3.0%)");
|
||||
expect(fmtFrames(0, 0, "en")).toBe(DASH);
|
||||
expect(fmtFrames(0, 1000, "en")).toBe("0 / 1,000 (0.00%)");
|
||||
expect(fmtFrames(5, 1000, "en")).toBe("5 / 1,000 (0.50%)");
|
||||
expect(fmtFrames(30, 1000, "en")).toBe("30 / 1,000 (3.0%)");
|
||||
expect(fmtFrames(30, 1000, "hu")).toBe("30 / 1000 (3,0%)");
|
||||
});
|
||||
|
||||
it("fmtRes dashes an undecoded 0×0 frame", () => {
|
||||
@@ -68,6 +74,7 @@ describe("plexStats formatters", () => {
|
||||
|
||||
describe("buildStatRows", () => {
|
||||
const base: PlexStatsInput = {
|
||||
lang: "en",
|
||||
playable: "remux",
|
||||
transport: "hls",
|
||||
videoWidth: 1280,
|
||||
@@ -199,6 +206,7 @@ describe("measured bitrate", () => {
|
||||
|
||||
describe("buildStatRows — what is actually playing", () => {
|
||||
const hls = (over: Partial<PlexStatsInput> = {}): PlexStatsInput => ({
|
||||
lang: "en",
|
||||
playable: "remux",
|
||||
transport: "hls",
|
||||
videoWidth: 1920,
|
||||
|
||||
Reference in New Issue
Block a user