0 ? library_fetch_document($documentId, $publicOnly) : null; if ($document && $context !== 'admin') { library_track_request('reader_opened', (int) $document['id'], [ 'document_type' => (string) ($document['document_type'] ?? ''), ]); } if (!$document || empty($document['file_path'])) { http_response_code(404); die('Document not found or no file attached.'); } $fileUrl = library_file_url((string) $document['file_path']); // --- Localization --- // Use global helper to respect session/get $lang = library_get_language(); $isRtl = $lang === 'ar'; $trans = [ 'en' => [ 'title_suffix' => 'Reader', 'default_title' => 'Document Viewer', 'back_tooltip' => 'Back to Details', 'search_placeholder' => 'Search...', 'prev_match' => 'Previous Match', 'next_match' => 'Next Match', 'read_aloud' => 'Read Aloud (Play/Stop)', 'prev_page' => 'Previous Page', 'next_page' => 'Next Page', 'download' => 'Download PDF', 'loading' => 'Loading Document...', 'downloading' => 'Downloading...', 'rendering' => 'Rendering pages...', 'error_prefix' => 'Error: ', 'searching' => 'Searching...', 'no_matches' => 'No matches', 'matches_found' => 'matches', 'page_label' => 'Page', 'switch_lang' => 'العربية', 'switch_lang_code' => 'ar' ], 'ar' => [ 'title_suffix' => 'القارئ', 'default_title' => 'عارض المستندات', 'back_tooltip' => 'العودة للتفاصيل', 'search_placeholder' => 'بحث...', 'prev_match' => 'المطابقة السابقة', 'next_match' => 'المطابقة التالية', 'read_aloud' => 'قراءة بصوت عالٍ (تشغيل/إيقاف)', 'prev_page' => 'الصفحة السابقة', 'next_page' => 'الصفحة التالية', 'download' => 'تحميل PDF', 'loading' => 'جاري تحميل المستند...', 'downloading' => 'جاري التنزيل...', 'rendering' => 'جاري عرض الصفحات...', 'error_prefix' => 'خطأ: ', 'searching' => 'جاري البحث...', 'no_matches' => 'لا يوجد نتائج', 'matches_found' => 'تطابقات', 'page_label' => 'صفحة', 'switch_lang' => 'English', 'switch_lang_code' => 'en' ] ]; $t = $trans[$lang]; // Determine Title $docTitle = $document['title_' . $lang] ?: $document['title_en'] ?: $document['title_ar'] ?: $t['default_title']; $docTitleLang = library_text_lang( (string) $docTitle, !empty($document['title_ar']) && empty($document['title_en']) ? 'ar' : $lang ); $docTitleDir = library_text_dir((string) $docTitle, $docTitleLang); ?>