fix(pdf-reader): localize embedpdf viewer to active app language (#466)

* fix(pdf-reader): localize embedpdf viewer to active app language

* fix(pdf-reader): use runtime locale fallback for embedpdf translations

* fix(pdf-reader): clarify document viewer save info copy
This commit is contained in:
Zach
2026-04-22 17:27:12 -04:00
committed by Zach Yale
parent 0eb4c8e3df
commit da7fa4bc14
4 changed files with 64 additions and 4 deletions
@@ -188,6 +188,16 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
this.startChromeAutoHide();
document.addEventListener('fullscreenchange', this.onFullscreenChange);
this.t.langChanges$
.pipe(takeUntil(this.destroy$))
.subscribe(lang => {
if (this.viewerMode === 'book') {
this.embedPdfBook.setLocale(lang);
} else if (this.embedPdfIframe?.contentWindow) {
this.embedPdfIframe.contentWindow.postMessage({ type: 'setLocale', locale: lang }, location.origin);
}
});
// Listen for mousemove outside Angular zone to avoid constant change detection
this.ngZone.runOutsideAngular(() => {
const mouseMoveHandler = () => {
@@ -427,7 +437,8 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
await this.embedPdfBook.init(
targetEl,
pdfUrl,
this.isDarkTheme ? 'dark' : 'light'
this.isDarkTheme ? 'dark' : 'light',
this.t.getActiveLang()
);
this.bookViewerInitialized = true;
@@ -861,7 +872,8 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
iframe.contentWindow!.postMessage({
type: 'init',
wasmUrl: '/assets/pdfium/pdfium.wasm',
theme: this.isDarkTheme ? 'dark' : 'light'
theme: this.isDarkTheme ? 'dark' : 'light',
locale: this.t.getActiveLang()
}, location.origin);
} catch (err) {
@@ -16,6 +16,7 @@ import type {
SpreadMode,
RotateCapability,
PanCapability,
I18nCapability,
} from '@embedpdf/snippet';
export interface PdfOutlineItem {
@@ -38,6 +39,7 @@ export class EmbedPdfBookService {
private spread: SpreadCapability | null = null;
private rotate: RotateCapability | null = null;
private pan: PanCapability | null = null;
private i18n: I18nCapability | null = null;
private currentDocumentId: string | null = null;
@@ -61,7 +63,7 @@ export class EmbedPdfBookService {
return this.scroll?.getTotalPages() ?? 0;
}
async init(target: HTMLElement, pdfUrl: string, theme: 'dark' | 'light'): Promise<void> {
async init(target: HTMLElement, pdfUrl: string, theme: 'dark' | 'light', localeCode: string): Promise<void> {
// Recreate event streams so the service is reusable after destroy()
this.pageChange$ = new Subject<PageChangeEvent>();
this.annotationEvent$ = new Subject<AnnotationEvent>();
@@ -75,6 +77,7 @@ export class EmbedPdfBookService {
const EmbedPDF = (await import('@embedpdf/snippet')).default;
const wasmUrl = new URL('/assets/pdfium/pdfium.wasm', location.origin).href;
const requestedLocale = localeCode || 'en';
this.container = EmbedPDF.init({
type: 'container',
@@ -83,6 +86,10 @@ export class EmbedPdfBookService {
wasmUrl,
worker: true,
log: false,
i18n: {
defaultLocale: requestedLocale,
fallbackLocale: 'en',
},
theme: {preference: theme},
disabledCategories: [
'redaction',
@@ -144,6 +151,10 @@ export class EmbedPdfBookService {
const panPlugin = this.registry.getPlugin('pan');
this.pan = panPlugin?.provides?.() as PanCapability ?? null;
const i18nPlugin = this.registry.getPlugin('i18n');
this.i18n = i18nPlugin?.provides?.() as I18nCapability ?? null;
this.applyLocale(requestedLocale);
// wire events
if (this.scroll) {
this.pageChangeUnsub = this.scroll.onPageChange((ev: PageChangeEvent) => {
@@ -196,6 +207,10 @@ export class EmbedPdfBookService {
this.container?.setTheme(theme);
}
setLocale(localeCode: string): void {
this.applyLocale(localeCode || 'en');
}
scrollToPage(pageNumber: number, behavior: 'instant' | 'smooth' = 'smooth'): void {
this.scroll?.scrollToPage({pageNumber, behavior});
}
@@ -371,6 +386,7 @@ export class EmbedPdfBookService {
this.spread = null;
this.rotate = null;
this.pan = null;
this.i18n = null;
this.currentDocumentId = null;
this.restoreWorkerShims();
@@ -378,6 +394,17 @@ export class EmbedPdfBookService {
this.restoreDevicePixelRatio();
}
private applyLocale(localeCode: string): void {
if (!this.i18n) return;
if (this.i18n.hasLocale(localeCode)) {
this.i18n.setLocale(localeCode);
return;
}
this.i18n.setLocale('en');
}
private convertBookmarks(items: unknown[]): PdfOutlineItem[] {
if (!Array.isArray(items)) return [];