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();
|
this.startChromeAutoHide();
|
||||||
document.addEventListener('fullscreenchange', this.onFullscreenChange);
|
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
|
// Listen for mousemove outside Angular zone to avoid constant change detection
|
||||||
this.ngZone.runOutsideAngular(() => {
|
this.ngZone.runOutsideAngular(() => {
|
||||||
const mouseMoveHandler = () => {
|
const mouseMoveHandler = () => {
|
||||||
@@ -427,7 +437,8 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
|||||||
await this.embedPdfBook.init(
|
await this.embedPdfBook.init(
|
||||||
targetEl,
|
targetEl,
|
||||||
pdfUrl,
|
pdfUrl,
|
||||||
this.isDarkTheme ? 'dark' : 'light'
|
this.isDarkTheme ? 'dark' : 'light',
|
||||||
|
this.t.getActiveLang()
|
||||||
);
|
);
|
||||||
this.bookViewerInitialized = true;
|
this.bookViewerInitialized = true;
|
||||||
|
|
||||||
@@ -861,7 +872,8 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
|||||||
iframe.contentWindow!.postMessage({
|
iframe.contentWindow!.postMessage({
|
||||||
type: 'init',
|
type: 'init',
|
||||||
wasmUrl: '/assets/pdfium/pdfium.wasm',
|
wasmUrl: '/assets/pdfium/pdfium.wasm',
|
||||||
theme: this.isDarkTheme ? 'dark' : 'light'
|
theme: this.isDarkTheme ? 'dark' : 'light',
|
||||||
|
locale: this.t.getActiveLang()
|
||||||
}, location.origin);
|
}, location.origin);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import type {
|
|||||||
SpreadMode,
|
SpreadMode,
|
||||||
RotateCapability,
|
RotateCapability,
|
||||||
PanCapability,
|
PanCapability,
|
||||||
|
I18nCapability,
|
||||||
} from '@embedpdf/snippet';
|
} from '@embedpdf/snippet';
|
||||||
|
|
||||||
export interface PdfOutlineItem {
|
export interface PdfOutlineItem {
|
||||||
@@ -38,6 +39,7 @@ export class EmbedPdfBookService {
|
|||||||
private spread: SpreadCapability | null = null;
|
private spread: SpreadCapability | null = null;
|
||||||
private rotate: RotateCapability | null = null;
|
private rotate: RotateCapability | null = null;
|
||||||
private pan: PanCapability | null = null;
|
private pan: PanCapability | null = null;
|
||||||
|
private i18n: I18nCapability | null = null;
|
||||||
|
|
||||||
private currentDocumentId: string | null = null;
|
private currentDocumentId: string | null = null;
|
||||||
|
|
||||||
@@ -61,7 +63,7 @@ export class EmbedPdfBookService {
|
|||||||
return this.scroll?.getTotalPages() ?? 0;
|
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()
|
// Recreate event streams so the service is reusable after destroy()
|
||||||
this.pageChange$ = new Subject<PageChangeEvent>();
|
this.pageChange$ = new Subject<PageChangeEvent>();
|
||||||
this.annotationEvent$ = new Subject<AnnotationEvent>();
|
this.annotationEvent$ = new Subject<AnnotationEvent>();
|
||||||
@@ -75,6 +77,7 @@ export class EmbedPdfBookService {
|
|||||||
const EmbedPDF = (await import('@embedpdf/snippet')).default;
|
const EmbedPDF = (await import('@embedpdf/snippet')).default;
|
||||||
|
|
||||||
const wasmUrl = new URL('/assets/pdfium/pdfium.wasm', location.origin).href;
|
const wasmUrl = new URL('/assets/pdfium/pdfium.wasm', location.origin).href;
|
||||||
|
const requestedLocale = localeCode || 'en';
|
||||||
|
|
||||||
this.container = EmbedPDF.init({
|
this.container = EmbedPDF.init({
|
||||||
type: 'container',
|
type: 'container',
|
||||||
@@ -83,6 +86,10 @@ export class EmbedPdfBookService {
|
|||||||
wasmUrl,
|
wasmUrl,
|
||||||
worker: true,
|
worker: true,
|
||||||
log: false,
|
log: false,
|
||||||
|
i18n: {
|
||||||
|
defaultLocale: requestedLocale,
|
||||||
|
fallbackLocale: 'en',
|
||||||
|
},
|
||||||
theme: {preference: theme},
|
theme: {preference: theme},
|
||||||
disabledCategories: [
|
disabledCategories: [
|
||||||
'redaction',
|
'redaction',
|
||||||
@@ -144,6 +151,10 @@ export class EmbedPdfBookService {
|
|||||||
const panPlugin = this.registry.getPlugin('pan');
|
const panPlugin = this.registry.getPlugin('pan');
|
||||||
this.pan = panPlugin?.provides?.() as PanCapability ?? null;
|
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
|
// wire events
|
||||||
if (this.scroll) {
|
if (this.scroll) {
|
||||||
this.pageChangeUnsub = this.scroll.onPageChange((ev: PageChangeEvent) => {
|
this.pageChangeUnsub = this.scroll.onPageChange((ev: PageChangeEvent) => {
|
||||||
@@ -196,6 +207,10 @@ export class EmbedPdfBookService {
|
|||||||
this.container?.setTheme(theme);
|
this.container?.setTheme(theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLocale(localeCode: string): void {
|
||||||
|
this.applyLocale(localeCode || 'en');
|
||||||
|
}
|
||||||
|
|
||||||
scrollToPage(pageNumber: number, behavior: 'instant' | 'smooth' = 'smooth'): void {
|
scrollToPage(pageNumber: number, behavior: 'instant' | 'smooth' = 'smooth'): void {
|
||||||
this.scroll?.scrollToPage({pageNumber, behavior});
|
this.scroll?.scrollToPage({pageNumber, behavior});
|
||||||
}
|
}
|
||||||
@@ -371,6 +386,7 @@ export class EmbedPdfBookService {
|
|||||||
this.spread = null;
|
this.spread = null;
|
||||||
this.rotate = null;
|
this.rotate = null;
|
||||||
this.pan = null;
|
this.pan = null;
|
||||||
|
this.i18n = null;
|
||||||
this.currentDocumentId = null;
|
this.currentDocumentId = null;
|
||||||
|
|
||||||
this.restoreWorkerShims();
|
this.restoreWorkerShims();
|
||||||
@@ -378,6 +394,17 @@ export class EmbedPdfBookService {
|
|||||||
this.restoreDevicePixelRatio();
|
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[] {
|
private convertBookmarks(items: unknown[]): PdfOutlineItem[] {
|
||||||
if (!Array.isArray(items)) return [];
|
if (!Array.isArray(items)) return [];
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,18 @@
|
|||||||
let registry = null;
|
let registry = null;
|
||||||
const myOrigin = location.origin;
|
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) {
|
function log(...args) {
|
||||||
console.log('[EmbedPDF-frame]', ...args);
|
console.log('[EmbedPDF-frame]', ...args);
|
||||||
parent.postMessage({
|
parent.postMessage({
|
||||||
@@ -284,6 +296,10 @@
|
|||||||
viewer = EmbedPDF.init({
|
viewer = EmbedPDF.init({
|
||||||
type: 'container',
|
type: 'container',
|
||||||
target: target,
|
target: target,
|
||||||
|
i18n: {
|
||||||
|
defaultLocale: msg.locale || 'en',
|
||||||
|
fallbackLocale: 'en',
|
||||||
|
},
|
||||||
disabledCategories: [
|
disabledCategories: [
|
||||||
'document-open',
|
'document-open',
|
||||||
'document-close',
|
'document-close',
|
||||||
@@ -303,6 +319,7 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
registry = await viewer.registry;
|
registry = await viewer.registry;
|
||||||
|
applyLocale(registry.getPlugin('i18n')?.provides?.(), msg.locale);
|
||||||
const dm = registry.getPlugin('document-manager')?.provides();
|
const dm = registry.getPlugin('document-manager')?.provides();
|
||||||
if (dm) {
|
if (dm) {
|
||||||
dm.onDocumentOpened?.((ev) => {
|
dm.onDocumentOpened?.((ev) => {
|
||||||
@@ -399,6 +416,10 @@
|
|||||||
viewer?.setTheme?.(msg.theme);
|
viewer?.setTheme?.(msg.theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (msg.type === 'setLocale') {
|
||||||
|
applyLocale(registry?.getPlugin('i18n')?.provides?.(), msg.locale);
|
||||||
|
}
|
||||||
|
|
||||||
if (msg.type === 'scrollToPage') {
|
if (msg.type === 'scrollToPage') {
|
||||||
const scrollPlugin = registry?.getPlugin('scroll')?.provides();
|
const scrollPlugin = registry?.getPlugin('scroll')?.provides();
|
||||||
scrollPlugin?.scrollToPage({ pageNumber: msg.pageNumber, behavior: 'smooth' });
|
scrollPlugin?.scrollToPage({ pageNumber: msg.pageNumber, behavior: 'smooth' });
|
||||||
|
|||||||
@@ -65,7 +65,7 @@
|
|||||||
"annotationFailed": "Failed to save annotations"
|
"annotationFailed": "Failed to save annotations"
|
||||||
},
|
},
|
||||||
"docViewer": {
|
"docViewer": {
|
||||||
"changesSavedInfo": "Changes are written to file here",
|
"changesSavedInfo": "Edits in the Document Viewer are automatically saved to the PDF.",
|
||||||
"dismiss": "Dismiss"
|
"dismiss": "Dismiss"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user