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:
@@ -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 [];
|
||||
|
||||
|
||||
@@ -93,6 +93,18 @@
|
||||
let registry = null;
|
||||
const myOrigin = location.origin;
|
||||
|
||||
function applyLocale(i18n, locale) {
|
||||
if (!i18n) return;
|
||||
|
||||
const requestedLocale = locale || 'en';
|
||||
if (i18n.hasLocale?.(requestedLocale)) {
|
||||
i18n.setLocale?.(requestedLocale);
|
||||
return;
|
||||
}
|
||||
|
||||
i18n.setLocale?.('en');
|
||||
}
|
||||
|
||||
function log(...args) {
|
||||
console.log('[EmbedPDF-frame]', ...args);
|
||||
parent.postMessage({
|
||||
@@ -284,6 +296,10 @@
|
||||
viewer = EmbedPDF.init({
|
||||
type: 'container',
|
||||
target: target,
|
||||
i18n: {
|
||||
defaultLocale: msg.locale || 'en',
|
||||
fallbackLocale: 'en',
|
||||
},
|
||||
disabledCategories: [
|
||||
'document-open',
|
||||
'document-close',
|
||||
@@ -303,6 +319,7 @@
|
||||
|
||||
try {
|
||||
registry = await viewer.registry;
|
||||
applyLocale(registry.getPlugin('i18n')?.provides?.(), msg.locale);
|
||||
const dm = registry.getPlugin('document-manager')?.provides();
|
||||
if (dm) {
|
||||
dm.onDocumentOpened?.((ev) => {
|
||||
@@ -399,6 +416,10 @@
|
||||
viewer?.setTheme?.(msg.theme);
|
||||
}
|
||||
|
||||
if (msg.type === 'setLocale') {
|
||||
applyLocale(registry?.getPlugin('i18n')?.provides?.(), msg.locale);
|
||||
}
|
||||
|
||||
if (msg.type === 'scrollToPage') {
|
||||
const scrollPlugin = registry?.getPlugin('scroll')?.provides();
|
||||
scrollPlugin?.scrollToPage({ pageNumber: msg.pageNumber, behavior: 'smooth' });
|
||||
|
||||
@@ -65,7 +65,7 @@
|
||||
"annotationFailed": "Failed to save annotations"
|
||||
},
|
||||
"docViewer": {
|
||||
"changesSavedInfo": "Changes are written to file here",
|
||||
"changesSavedInfo": "Edits in the Document Viewer are automatically saved to the PDF.",
|
||||
"dismiss": "Dismiss"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user