Defend against Chrome rewriting the DOM, and add the language setting
Groundwork for un-shelving translations. Chrome's translator rewrites the rendered DOM directly, wrapping text nodes in <font> elements React has never heard of, and the next update can then call removeChild against a parent whose children have moved (facebook/react#11538). This is the structural defence against that, plus the setting the served language will read from. The language setting is `uiLanguage`, and it is deliberately not the `locale` field that already exists. That one is a formatting choice -- what calendar, clock and numerals to use -- and folding the two together would silently rewrite everybody's date format the first time they picked a language. German dates with an English interface is a real preference, and so is the reverse. It defaults to English when absent, which covers both a new account and every settings file written before this, and Accept-Language is not consulted: a served locale should be something the reader chose rather than something guessed and then written down as though they had. Only languages with strings shipped are offered, which today means English alone -- a picker entry without a catalogue behind it would leave the page claiming a language it is not in, which stops a reader translating a page they cannot read. `<html lang>` is set where applyTheme is set: at store module load, from the localStorage cache, before createRoot() has rendered anything. Not in an effect -- a lang that is briefly wrong is enough to raise the translate prompt on a page that needed none. There is no server-rendered alternative to reach for here: ihasmail serves a static shell and holds no account state, and the settings file lives in the reader's own JMAP Files, so reading it before the page existed would mean authenticating to Stalwart on every page load. The static lang="en" in index.html covers the first bytes; the store only ever corrects a reader who chose otherwise. Both halves are tested. translate="no" and class="notranslate" go on the narrow boundaries only: rendered email bodies, raw message source, attachment text, the generated and hand-edited Sieve, the brand and the login name. Not on <body> -- someone whose language ihasmail does not speak yet should still be able to translate the parts that are ours. Email bodies turn out to live in a shadow root, so React never reconciles them and they were never a crash risk; the marker there is about not rewriting what a sender actually wrote. Twenty-four fragile interpolation points were found with the TypeScript parser rather than grep, and fifteen refactored. Pluralisation and "count + label" pairs are collapsed into a single expression so the text is a lone child React updates with textContent, rather than a text node with conditional siblings to insert around. One of them -- InviteCard's {method === "REPLY" && organizer ? "" : ""} -- rendered an empty string either way and is simply gone. The boundary is scoped to the main content, so the header, folder tree and any open composer sit outside it and survive independently. It recovers by remounting the subtree, which costs nothing because everything inside re-derives from the stores, and it logs at info rather than error: a reader translating a page is expected and recovered from, and filing it as an error would put an entry in every console-reading reporter for behaviour that worked. It re-raises anything that is not a DOM mutation error, so a real bug still surfaces as one, and it gives up after three attempts rather than looping invisibly. Worth recording: the crash could not be reproduced on React 19.2.8. Wrapping 207-249 React-managed text nodes in <font>, exactly as the translator does, then driving in-place conditional toggles and navigations, left the app intact with the boundary never firing. The original issue is from React 16 and the reconciler has changed a great deal since. So this lands as defence whose premise is weaker than assumed rather than as a fix for something observed here, and the boundary is insurance rather than a load-bearing part. The notranslate markers and the collapsed interpolations stand on their own merits either way.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { DEFAULT_UI_LANGUAGE, UI_LANGUAGES, resolveUiLanguage } from "@/lib/languages";
|
||||
import { DEFAULT_SETTINGS, acceptRemote } from "@/store/settings";
|
||||
|
||||
/**
|
||||
* The interface language decides what `<html lang>` claims, and a wrong claim
|
||||
* is exactly what makes Chrome offer to translate a page that needs no
|
||||
* translating — which is the offer that ends in a rewritten DOM and a crashed
|
||||
* component tree. So the resolution is deliberately narrow.
|
||||
*/
|
||||
describe("resolveUiLanguage", () => {
|
||||
it("is English when nothing has been chosen", () => {
|
||||
// The absent case covers both a new account and every settings file
|
||||
// written before this setting existed.
|
||||
expect(resolveUiLanguage(undefined)).toBe("en");
|
||||
expect(resolveUiLanguage(null)).toBe("en");
|
||||
expect(resolveUiLanguage("")).toBe("en");
|
||||
expect(DEFAULT_SETTINGS.uiLanguage).toBe(DEFAULT_UI_LANGUAGE);
|
||||
});
|
||||
|
||||
it("refuses a language whose strings are not shipped", () => {
|
||||
// The account travels between machines and can outlive a catalogue. A
|
||||
// page that says lang="de" while rendering English is worse than one that
|
||||
// admits to English: it stops the reader translating it themselves.
|
||||
expect(resolveUiLanguage("de")).toBe("en");
|
||||
expect(resolveUiLanguage("xx-XX")).toBe("en");
|
||||
});
|
||||
|
||||
it("honours one that is", () => {
|
||||
for (const l of UI_LANGUAGES) expect(resolveUiLanguage(l.tag)).toBe(l.tag);
|
||||
});
|
||||
|
||||
it("only offers languages that resolve to themselves", () => {
|
||||
// Guards the ordering mistake: adding a picker entry before its catalogue.
|
||||
for (const l of UI_LANGUAGES) {
|
||||
expect(resolveUiLanguage(l.tag)).toBe(l.tag);
|
||||
expect(l.name.trim()).not.toBe("");
|
||||
}
|
||||
});
|
||||
|
||||
it("follows the account rather than the device", () => {
|
||||
// Language is a preference about the person, not the screen: it is not in
|
||||
// DEVICE_KEYS, so it rides in the settings file like the rest.
|
||||
expect(acceptRemote({ uiLanguage: "en" })).toEqual({ uiLanguage: "en" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* The interface languages that actually have strings shipped.
|
||||
*
|
||||
* Deliberately not `lib/locales.ts`. That list is every tag CLDR can format a
|
||||
* date in — about 620 of them — and it answers a different question: what
|
||||
* calendar, clock and numerals to use. This one answers "what language is the
|
||||
* app written in", and the only honest entries are the ones somebody has
|
||||
* translated. Offering a language with no strings behind it would set
|
||||
* `<html lang>` to a language the page is not in, which is worse than not
|
||||
* offering it: it stops Chrome offering to translate a page the reader cannot
|
||||
* read.
|
||||
*
|
||||
* Wanting German dates with an English interface is a real preference, and so
|
||||
* is the reverse, which is why `uiLanguage` and `locale` are separate settings
|
||||
* rather than one.
|
||||
*
|
||||
* Adding a language means adding its catalogue and then adding it here, in
|
||||
* that order. RTL languages — Arabic, Hebrew, Persian — need bidi and layout
|
||||
* work well beyond strings, so they are not simply a matter of another entry.
|
||||
*/
|
||||
export interface UiLanguage {
|
||||
/** BCP 47, and what `<html lang>` is set to. */
|
||||
tag: string;
|
||||
/** The language's name in that language, which is how a picker should read. */
|
||||
name: string;
|
||||
}
|
||||
|
||||
export const UI_LANGUAGES: readonly UiLanguage[] = [
|
||||
{ tag: "en", name: "English" },
|
||||
];
|
||||
|
||||
export const DEFAULT_UI_LANGUAGE = "en";
|
||||
|
||||
/**
|
||||
* The language to actually render in.
|
||||
*
|
||||
* A stored preference is only honoured if its strings are still shipped: a
|
||||
* catalogue can be withdrawn, and an account carrying `de` from another
|
||||
* machine must not leave this one claiming to be German while showing English.
|
||||
*/
|
||||
export function resolveUiLanguage(stored: string | undefined | null): string {
|
||||
if (!stored) return DEFAULT_UI_LANGUAGE;
|
||||
return UI_LANGUAGES.some((l) => l.tag === stored) ? stored : DEFAULT_UI_LANGUAGE;
|
||||
}
|
||||
Reference in New Issue
Block a user