@@ -11,6 +11,9 @@ const queueLoadText = document.getElementById('queue-load-text');
1111// Track active requests
1212let currentActiveRequests = 0 ;
1313
14+ // Initialize current language
15+ let currentLang = 'en' ;
16+
1417// Language translations
1518const translations = {
1619 en : {
@@ -56,36 +59,22 @@ const translations = {
5659// Language switching functionality
5760document . addEventListener ( 'DOMContentLoaded' , function ( ) {
5861 const langButtons = document . querySelectorAll ( '.lang-btn' ) ;
59- let currentLang = 'en' ;
60-
61- function updateLanguage ( lang ) {
62- currentLang = lang ;
63- const t = translations [ lang ] ;
64-
65- // Update text content
66- document . querySelector ( '.main-header h1' ) . textContent = t . title ;
67- document . querySelector ( '.subtitle' ) . textContent = t . subtitle ;
68- document . querySelector ( '.playground-section h2' ) . textContent = t . tryItOut ;
69- document . querySelector ( 'label[for="playground-text"]' ) . textContent = t . textToConvert ;
70- document . querySelector ( 'label[for="playground-voice"]' ) . textContent = t . voice ;
71- document . querySelector ( 'label[for="playground-instructions"]' ) . textContent = t . instructions ;
72- document . querySelector ( '.playground-button' ) . innerHTML = `<i class="fas fa-play"></i> ${ t . generateSpeech } ` ;
73- document . querySelector ( '.content-section:nth-child(3) h2' ) . textContent = t . quickStart ;
74- document . querySelector ( '.content-section:nth-child(4) h2' ) . textContent = t . availableVoices ;
75- document . querySelector ( '.content-section:nth-child(5) h2' ) . textContent = t . apiReference ;
76- document . querySelector ( '.status-header h3' ) . textContent = t . queueStatus ;
77- document . querySelector ( '.stat-item:nth-child(1) .stat-label' ) . textContent = t . activeRequests ;
78- document . querySelector ( '.stat-item:nth-child(2) .stat-label' ) . textContent = t . maxCapacity ;
79- }
80-
81- langButtons . forEach ( button => {
82- button . addEventListener ( 'click' , function ( ) {
83- const lang = this . dataset . lang ;
84- langButtons . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
85- this . classList . add ( 'active' ) ;
86- updateLanguage ( lang ) ;
87- } ) ;
62+
63+ // Set initial language based on current page
64+ const isChinesePage = window . location . pathname . includes ( '_zh.html' ) ;
65+ currentLang = isChinesePage ? 'zh' : 'en' ;
66+
67+ // Update active state of language buttons
68+ langButtons . forEach ( btn => {
69+ if ( btn . getAttribute ( 'data-lang' ) === currentLang ) {
70+ btn . classList . add ( 'active' ) ;
71+ } else {
72+ btn . classList . remove ( 'active' ) ;
73+ }
8874 } ) ;
75+
76+ // Initial queue size update
77+ updateQueueSize ( ) ;
8978} ) ;
9079
9180function updateProcessingStatus ( requestCount ) {
@@ -109,6 +98,9 @@ function updateLastUpdate() {
10998async function updateQueueSize ( ) {
11099 try {
111100 const response = await fetch ( '/api/queue-size' ) ;
101+ if ( ! response . ok ) {
102+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
103+ }
112104 const data = await response . json ( ) ;
113105
114106 // Update text values
@@ -119,13 +111,21 @@ async function updateQueueSize() {
119111 const loadPercentage = ( data . queue_size / data . max_queue_size ) * 100 ;
120112
121113 // Update progress bar width
122- queueProgressBar . style . width = `${ loadPercentage } %` ;
114+ queueProgressBar . style . width = `${ Math . min ( loadPercentage , 100 ) } %` ;
123115
124116 // Update status indicators based on load
125117 updateLoadStatus ( loadPercentage ) ;
126118
127119 } catch ( error ) {
128120 console . error ( 'Error fetching queue size:' , error ) ;
121+ // Show error state in UI
122+ document . getElementById ( 'queue-size' ) . textContent = '?' ;
123+ document . getElementById ( 'max-queue-size' ) . textContent = '?' ;
124+ queueProgressBar . style . width = '0%' ;
125+ statusIndicator . classList . remove ( 'indicator-low' , 'indicator-medium' , 'indicator-high' ) ;
126+ queueProgressBar . classList . remove ( 'progress-low' , 'progress-medium' , 'progress-high' ) ;
127+ queueLoadText . classList . remove ( 'low-load' , 'medium-load' , 'high-load' ) ;
128+ queueLoadText . textContent = 'Error' ;
129129 }
130130}
131131
@@ -142,19 +142,19 @@ function updateLoadStatus(loadPercentage) {
142142 statusIndicator . classList . add ( 'indicator-high' ) ;
143143 queueProgressBar . classList . add ( 'progress-high' ) ;
144144 queueLoadText . classList . add ( 'high-load' ) ;
145- queueLoadText . textContent = 'High Load' ;
145+ queueLoadText . textContent = translations [ currentLang ] . highLoad ;
146146 } else if ( loadPercentage >= 40 ) {
147147 // Medium load (40-75%)
148148 statusIndicator . classList . add ( 'indicator-medium' ) ;
149149 queueProgressBar . classList . add ( 'progress-medium' ) ;
150150 queueLoadText . classList . add ( 'medium-load' ) ;
151- queueLoadText . textContent = 'Medium Load' ;
151+ queueLoadText . textContent = translations [ currentLang ] . mediumLoad ;
152152 } else {
153153 // Low load (0-40%)
154154 statusIndicator . classList . add ( 'indicator-low' ) ;
155155 queueProgressBar . classList . add ( 'progress-low' ) ;
156156 queueLoadText . classList . add ( 'low-load' ) ;
157- queueLoadText . textContent = loadPercentage > 0 ? 'Low Load' : 'No Load' ;
157+ queueLoadText . textContent = loadPercentage > 0 ? translations [ currentLang ] . lowLoad : translations [ currentLang ] . noLoad ;
158158 }
159159}
160160
0 commit comments