fix(reader): page a book by its own language, not the UI language (#8184)

* test(reader): page an English book LTR when the UI language is Arabic

The UI language is only meant to set the page direction of a book that
names no language. An English book read in the Arabic UI was paged
right-to-left, so the right arrow turned back a page.

Verified: the new test in foliate-viewer-writing-mode.test.tsx fails on
main with "expected true to be false".

Co-Authored-By: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>

* fix(reader): use the UI language only for books with no language

The fallback from #6197 now applies only when the book names no
language, so a book that does keeps its own direction.

Verified: pnpm test --run (17211 passed, 16 skipped); the new test
fails without this change ("expected true to be false") and passes
with it; pnpm lint and pnpm format:check pass. Desktop app (macOS),
Arabic UI: the right arrow turns to the next page in an English book,
and the left arrow still does in an Arabic book.

Co-Authored-By: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>

* test(reader): page a book with no language by the UI language

The fix keeps the UI-language fallback for a book that names no
language. This pins that case next to the English-book one, so the
narrowing can't drop the fallback altogether.

Verified: pnpm test --run src/__tests__/app/reader/foliate-viewer-*.test.tsx
(8 files, 35 tests pass). With FoliateViewer.tsx at origin/main, the
English-book case fails ("expected true to be false") and the
no-language case passes; with the fix both pass. pnpm lint and
pnpm format:check pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Huda <htsulait@andrew.cmu.edu>
Co-authored-by: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>
This commit is contained in:
Huang Xin 2026-12-01 23:11:00 +08:00 • committed by GitHub
parent dac978b0ac
commit c3de25535d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 3 deletions

View file

@ -1,5 +1,5 @@
import { render, waitFor } from '@testing-library/react';
import { expect, it, vi } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { ComponentProps } from 'react';
import FoliateViewer from '@/app/reader/components/FoliateViewer';
import { useFoliateEvents } from '@/app/reader/hooks/useFoliateEvents';
@ -167,3 +167,42 @@ it('lets only the section being read set the book writing mode', async () => {
load(frontMatter, 0);
expect(h.settings).toMatchObject({ vertical: false, rtl: false });
});
// The UI language is only the last resort for a book with no direction. An
// English book read in the Arabic UI was paged right-to-left, so the right
// arrow turned back a page.
describe('page direction in an Arabic UI', () => {
// Opens the book and loads its first section, as the renderer would.
const loadFirstSection = async (bookKey: string, metadata: Record<string, unknown>) => {
localStorage.setItem('i18nextLng', 'ar');
h.settings = { writingMode: 'auto', vertical: false, rtl: false };
const props = {
bookKey,
bookDoc: { metadata },
config: {},
gridInsets: { top: 0, right: 0, bottom: 0, left: 0 },
contentInsets: { top: 0, right: 0, bottom: 0, left: 0 },
} as unknown as ComponentProps<typeof FoliateViewer>;
vi.mocked(useFoliateEvents).mockClear();
render(<FoliateViewer {...props} />);
await waitFor(() => expect(vi.mocked(useFoliateEvents).mock.calls.some(([v]) => v)).toBe(true));
const onLoad = vi.mocked(useFoliateEvents).mock.calls.find(([view]) => view)![1]!.onLoad!;
const doc = Object.assign(document.implementation.createHTMLDocument('chapter'), {
isEventListenersAdded: true,
});
h.contents = [];
onLoad(new CustomEvent('load', { detail: { doc, index: 0 } }));
};
afterEach(() => localStorage.removeItem('i18nextLng'));
it('pages a book by its own language, not the UI language', async () => {
await loadFirstSection('english-book', { language: 'en' });
expect(h.settings.rtl).toBe(false);
});
it('pages a book that names no language by the UI language', async () => {
await loadFirstSection('nameless-book', {});
expect(h.settings.rtl).toBe(true);
});
});

View file

@ -684,8 +684,10 @@ const FoliateViewer: React.FC<{
writingDir?.vertical || viewSettings.writingMode.includes('vertical') || false;
// Fixed-layout books carry no writing mode; their direction may come
// from the document itself (PDF ViewerPreferences /Direction /R2L). The
// UI language is the last resort, for a book that says nothing at all.
const documentRtl = writingDir?.rtl || getDirFromUILanguage() === 'rtl' || false;
// UI language is the last resort, for a book that says nothing at all:
// a book that names its language (an English book) is not paged RTL.
const uiRtl = !bookDoc.metadata?.language?.length && getDirFromUILanguage() === 'rtl';
const documentRtl = writingDir?.rtl || uiRtl;
const newRtl = getPageProgressionRTL(viewSettings.writingMode, bookDoc.dir, documentRtl);
// Continuous scrolling keeps the neighbouring sections mounted, and they
// finish loading *after* the one being read. A horizontal front matter