@@ -575,6 +575,10 @@ function initRouter() {
575575 } ;
576576}
577577
578+ // Import utilities
579+ import { detectAndImportModules , filterScriptTags } from './utils/component-loader.js' ;
580+ import { translateContainer , applyStoredLanguage } from './utils/translation-helper.js' ;
581+
578582/**
579583 * Load a page from the server
580584 * @param {string } url - Page URL
@@ -585,17 +589,7 @@ async function loadPage(url) {
585589 console . log ( `Loading page: ${ url } ` ) ;
586590
587591 // Apply stored language before loading the page
588- const storedLang = localStorage . getItem ( 'profullstack-language' ) ;
589- if ( storedLang && window . app && window . app . localizer ) {
590- console . log ( `Pre-load: Applying stored language: ${ storedLang } ` ) ;
591- window . app . localizer . setLanguage ( storedLang ) ;
592-
593- // Force language application
594- if ( window . app . localizer . getLanguage ( ) !== storedLang ) {
595- console . log ( `Language mismatch in loadPage, forcing to: ${ storedLang } ` ) ;
596- window . app . localizer . setLanguage ( storedLang ) ;
597- }
598- }
592+ const storedLang = applyStoredLanguage ( ) ;
599593
600594 // Add cache-busting parameter to prevent caching
601595 const cacheBuster = `?_=${ Date . now ( ) } ` ;
@@ -630,61 +624,21 @@ async function loadPage(url) {
630624 // Create a temporary div to hold the content
631625 const tempDiv = document . createElement ( 'div' ) ;
632626
633- // Clone all child nodes except script tags
634- Array . from ( doc . body . children ) . forEach ( child => {
635- if ( child . tagName !== 'SCRIPT' ) {
636- tempDiv . appendChild ( child . cloneNode ( true ) ) ;
637- }
638- } ) ;
627+ // Automatically detect and import module scripts
628+ await detectAndImportModules ( doc ) ;
639629
640- content = tempDiv . innerHTML ;
630+ // Filter out script tags (they'll be imported dynamically)
631+ const contentWithoutScripts = filterScriptTags ( doc . body ) ;
632+ content = contentWithoutScripts . innerHTML ;
641633 }
642634
643635 // Add the content to the wrapper
644636 wrapper . innerHTML = content ;
645637
646638 // Pre-translate the content before it's returned to the router
647- if ( storedLang && window . app && window . app . _t ) {
639+ if ( storedLang ) {
648640 console . log ( `Pre-translating content to ${ storedLang } before DOM insertion` ) ;
649-
650- // Force language application
651- window . app . localizer . setLanguage ( storedLang ) ;
652-
653- // Translate elements with data-i18n attribute
654- wrapper . querySelectorAll ( '[data-i18n]' ) . forEach ( element => {
655- const key = element . getAttribute ( 'data-i18n' ) ;
656- element . textContent = window . app . _t ( key ) ;
657- } ) ;
658-
659- // Translate other i18n attributes
660- wrapper . querySelectorAll ( '[data-i18n-placeholder]' ) . forEach ( element => {
661- const key = element . getAttribute ( 'data-i18n-placeholder' ) ;
662- element . placeholder = window . app . _t ( key ) ;
663- } ) ;
664-
665- wrapper . querySelectorAll ( '[data-i18n-title]' ) . forEach ( element => {
666- const key = element . getAttribute ( 'data-i18n-title' ) ;
667- element . title = window . app . _t ( key ) ;
668- } ) ;
669-
670- wrapper . querySelectorAll ( '[data-i18n-html]' ) . forEach ( element => {
671- const key = element . getAttribute ( 'data-i18n-html' ) ;
672- element . innerHTML = window . app . _t ( key ) ;
673- } ) ;
674-
675- // Handle elements with data-i18n-params attribute (for interpolation)
676- wrapper . querySelectorAll ( '[data-i18n-params]' ) . forEach ( element => {
677- const key = element . getAttribute ( 'data-i18n' ) ;
678- if ( ! key ) return ;
679-
680- try {
681- const paramsAttr = element . getAttribute ( 'data-i18n-params' ) ;
682- const params = JSON . parse ( paramsAttr ) ;
683- element . textContent = window . app . _t ( key , params ) ;
684- } catch ( error ) {
685- console . error ( `Error parsing data-i18n-params for key ${ key } :` , error ) ;
686- }
687- } ) ;
641+ translateContainer ( wrapper , storedLang ) ;
688642 }
689643
690644 // Get the translated content
@@ -693,14 +647,6 @@ async function loadPage(url) {
693647 // Remove the wrapper
694648 document . body . removeChild ( wrapper ) ;
695649
696- // Special handling for state-demo.html
697- if ( url . includes ( 'state-demo.html' ) ) {
698- // Import the components directly in the HTML
699- await import ( '/js/components/state-example.js' ) ;
700- await import ( '/js/components/test-component.js' ) ;
701- console . log ( 'Imported state-example.js and test-component.js directly' ) ;
702- }
703-
704650 // Create DOM elements instead of using template strings
705651 const container = document . createElement ( 'div' ) ;
706652
0 commit comments