fix(web): language-aware CJK font stacks for chat prose (#2143)

Set message-scoped lang from the reply language and add :lang(ja/ko/zh-Hant)
font stacks so Japanese/Korean/Traditional Chinese replies no longer fall
through to SimSun/Songti SC. Leave the shared SC-first Tailwind stacks alone.
This commit is contained in:
GLM DeepTutor Worker
2026-11-09 13:40:39 +08:00
parent 60fb3bdfdd
commit 5e6ccfa6eb
4 changed files with 225 additions and 1 deletions
+112
View File
@@ -443,6 +443,118 @@
@apply font-serif text-[16px];
}
/* ─── Prose: language-aware CJK stacks ───
The shared serif/sans stacks in tailwind.config.js intentionally lead with
Simplified Chinese faces so `zh` replies never hit an arbitrary browser
default. That same ordering serves every other CJK language badly: a
Japanese reply on Windows renders in SimSun (Simplified glyph forms, kana
from a Chinese face) because SimSun answers before any Japanese font is
reached. Assistant messages set `lang` from the reply language (see
`lib/content-lang.ts` + `AssistantResponse`), and these rules list the
region-correct faces FIRST while keeping the Simplified Chinese ones as
trailing fallbacks for machines without the regional fonts. `zh` and `en`
match no rule here and keep the default stacks — do not reorder the shared
stacks to fix this, Chinese text would pick up Japanese glyph forms. */
.prose:lang(ja),
.font-serif:lang(ja) {
font-family:
var(--font-serif),
'Hiragino Mincho ProN',
'Yu Mincho',
'Noto Serif JP',
'MS Mincho',
'Songti SC',
'STSong',
'Noto Serif SC',
'Source Han Serif SC',
'SimSun',
Georgia,
serif;
}
.prose:lang(ja) :is(h1, h2, h3, h4, h5, h6),
.font-sans:lang(ja) {
font-family:
var(--font-sans),
'Hiragino Sans',
'Yu Gothic',
'Noto Sans JP',
'Meiryo',
'PingFang SC',
'Hiragino Sans GB',
'Microsoft YaHei',
'Noto Sans SC',
system-ui,
sans-serif;
}
.prose:lang(ko),
.font-serif:lang(ko) {
font-family:
var(--font-serif),
'Nanum Myeongjo',
'Batang',
'Noto Serif KR',
'Songti SC',
'STSong',
'Noto Serif SC',
'Source Han Serif SC',
'SimSun',
Georgia,
serif;
}
.prose:lang(ko) :is(h1, h2, h3, h4, h5, h6),
.font-sans:lang(ko) {
font-family:
var(--font-sans),
'Nanum Gothic',
'Malgun Gothic',
'Apple SD Gothic Neo',
'Noto Sans KR',
'PingFang SC',
'Hiragino Sans GB',
'Microsoft YaHei',
'Noto Sans SC',
system-ui,
sans-serif;
}
.prose:lang(zh-Hant),
.prose:lang(zh-TW),
.font-serif:lang(zh-Hant),
.font-serif:lang(zh-TW) {
font-family:
var(--font-serif),
'Songti TC',
'Noto Serif TC',
'PMingLiU',
'Songti SC',
'STSong',
'Noto Serif SC',
'Source Han Serif SC',
'SimSun',
Georgia,
serif;
}
.prose:lang(zh-Hant) :is(h1, h2, h3, h4, h5, h6),
.prose:lang(zh-TW) :is(h1, h2, h3, h4, h5, h6),
.font-sans:lang(zh-Hant),
.font-sans:lang(zh-TW) {
font-family:
var(--font-sans),
'PingFang TC',
'Microsoft JhengHei',
'Noto Sans TC',
'PingFang SC',
'Hiragino Sans GB',
'Microsoft YaHei',
'Noto Sans SC',
system-ui,
sans-serif;
}
.prose pre {
@apply rounded-lg p-4 overflow-x-auto my-4;
background: #292524;
+5 -1
View File
@@ -5,6 +5,7 @@ import { Fragment, memo, useMemo } from "react";
import MarkdownRenderer from "@/components/common/MarkdownRenderer";
import ModelThinkingCard from "@/components/common/ModelThinkingCard";
import { useReading } from "@/context/ReadingContext";
import { contentLang } from "@/lib/content-lang";
import type { StreamEvent } from "@/features/chat/model/protocol";
import {
hasVisibleMarkdownContent,
@@ -112,13 +113,16 @@ function AssistantResponseImpl({
// structured landmark. aria-live="polite" + aria-atomic="false" announces
// streamed-in content as the user pauses, without re-reading the whole
// bubble each token. Together this is the minimal pattern that turns a
// silent stream into an audible one.
// silent stream into an audible one. ``lang`` scopes the reply's language
// to the message text so the ``:lang()`` CJK font stacks in globals.css
// apply to chat prose without touching surrounding chrome.
return (
<div
role="article"
aria-live="polite"
aria-atomic="false"
className={className}
lang={contentLang(language)}
>
{segments.map((segment, index) => {
if (segment.kind === "think") {
+31
View File
@@ -0,0 +1,31 @@
/**
* Map a reply-language code onto the HTML ``lang`` value used for assistant
* message content. Only CJK reply languages get an explicit tag: the shared
* font stacks in ``tailwind.config.js`` deliberately lead with Simplified
* Chinese faces, so a ``ja``/``ko``/``zh-tw`` reply rendered under them picks
* up Simplified glyph forms (SimSun even answers for kana). Pinning the
* script/region lets the ``:lang()`` stacks in ``globals.css`` put the
* region-correct faces first while ``zh``/``en`` keep the default stacks.
* Everything else returns ``undefined`` so the app shell's own ``lang``
* keeps applying.
*/
export function contentLang(
language: string | undefined,
): string | undefined {
const code = language?.trim().toLowerCase().replaceAll("_", "-");
if (!code) return undefined;
if (code === "ja" || code === "jp" || code.startsWith("ja-")) return "ja";
if (code === "ko" || code === "kr" || code.startsWith("ko-")) return "ko";
if (
code === "zh-tw" ||
code === "zh-hant" ||
code === "zh-hk" ||
code === "zh-mo" ||
code.startsWith("zh-hant-") ||
code.startsWith("zh-tw-")
) {
return "zh-Hant";
}
if (code === "zh" || code.startsWith("zh-")) return "zh-Hans";
return undefined;
}
+77
View File
@@ -0,0 +1,77 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { contentLang } from "../lib/content-lang";
const read = (...parts: string[]) =>
fs.readFileSync(path.join(process.cwd(), ...parts), "utf8");
test("contentLang maps CJK reply languages onto HTML lang tags", () => {
assert.equal(contentLang(undefined), undefined);
assert.equal(contentLang(""), undefined);
assert.equal(contentLang(" "), undefined);
assert.equal(contentLang("ja"), "ja");
assert.equal(contentLang("JA"), "ja");
assert.equal(contentLang("jp"), "ja");
assert.equal(contentLang("ja-JP"), "ja");
assert.equal(contentLang("ko"), "ko");
assert.equal(contentLang("ko_KR"), "ko");
assert.equal(contentLang("kr"), "ko");
assert.equal(contentLang("zh-tw"), "zh-Hant");
assert.equal(contentLang("zh_TW"), "zh-Hant");
assert.equal(contentLang("zh-Hant"), "zh-Hant");
assert.equal(contentLang("zh-hk"), "zh-Hant");
assert.equal(contentLang("zh"), "zh-Hans");
assert.equal(contentLang("zh-cn"), "zh-Hans");
assert.equal(contentLang("zh-Hans"), "zh-Hans");
assert.equal(contentLang("en"), undefined);
assert.equal(contentLang("fr"), undefined);
assert.equal(contentLang("pt"), undefined);
});
function stackOrder(css: string, selectorBlock: string, earlier: string, later: string) {
const idx = css.indexOf(selectorBlock);
assert.ok(idx >= 0, `missing selector block ${selectorBlock}`);
const bodyStart = css.indexOf("{", idx);
const bodyEnd = css.indexOf("}", bodyStart);
const body = css.slice(bodyStart, bodyEnd);
const a = body.indexOf(earlier);
const b = body.indexOf(later);
assert.ok(a >= 0, `${earlier} missing in ${selectorBlock}`);
assert.ok(b >= 0, `${later} missing in ${selectorBlock}`);
assert.ok(a < b, `${earlier} must precede ${later} in ${selectorBlock}`);
}
test("globals.css :lang CJK stacks put regional faces before SimSun/Songti SC", () => {
const css = read("app/globals.css");
stackOrder(css, ".prose:lang(ja)", "Yu Mincho", "SimSun");
stackOrder(css, ".prose:lang(ja)", "Noto Serif JP", "SimSun");
stackOrder(css, ".prose:lang(ja)", "Hiragino Mincho ProN", "Songti SC");
stackOrder(css, ".prose:lang(ko)", "Noto Serif KR", "SimSun");
stackOrder(css, ".prose:lang(ko)", "Batang", "Songti SC");
stackOrder(css, ".prose:lang(zh-Hant)", "Songti TC", "SimSun");
stackOrder(css, ".prose:lang(zh-Hant)", "Noto Serif TC", "Songti SC");
const tw = read("tailwind.config.js");
const serifIdx = tw.indexOf("serif:");
const serifSlice = tw.slice(serifIdx, serifIdx + 400);
assert.ok(serifSlice.includes("Songti SC"));
assert.ok(!serifSlice.includes("Yu Mincho"));
assert.ok(!serifSlice.includes("Noto Serif JP"));
});
test("AssistantResponse sets lang from contentLang(language)", () => {
const src = read("components/common/AssistantResponse.tsx");
assert.match(src, /import \{ contentLang \} from "@\/lib\/content-lang"/);
assert.match(src, /lang=\{contentLang\(language\)\}/);
});