1import { debug, debugError } from './debug.js'; 2 3/** 4 * Get the current language from URL parameter 5 * @returns {string} - Language code ('ru' or 'en') 6 */ 7export function getCurrentLanguage() { 8 const urlParams = new URLSearchParams(window.location.search); 9 const lang = urlParams.get('lang'); 10 11 // Default to Russian if no parameter or invalid value 12 if (lang === 'en') { 13 return 'en'; 14 } 15 return 'ru'; 16} 17 18/** 19 * Set the current language by updating URL parameter 20 * @param {string} lang - Language code ('ru' or 'en') 21 */ 22export function setLanguage(lang) { 23 const url = new URL(window.location.href); 24 25 if (lang === 'ru') { 26 // Remove lang parameter for Russian (default) 27 url.searchParams.delete('lang'); 28 } else { 29 url.searchParams.set('lang', lang); 30 } 31 32 debug('ui', `Switching language to: ${lang}`); 33 // Update the URL without reloading the page 34 window.history.pushState({}, '', url.toString()); 35 updateTranslatedContent(); // Call updateTranslatedContent after changing language 36} 37 38/** 39 * Get translated content based on current language 40 * @param {Object} translations - Object with 'ru' and 'en' properties 41 * @returns {*} - Translated content 42 */ 43export function translate(translations) { 44 const lang = getCurrentLanguage(); 45 return translations[lang] || translations.ru; 46} 47 48// Define the event handler function outside initLanguageSelector 49function handleLanguageButtonClick(e) { 50 e.preventDefault(); 51 const currentLang = getCurrentLanguage(); // Get current language inside handler 52 const newLang = currentLang === 'ru' ? 'en' : 'ru'; 53 setLanguage(newLang); 54} 55 56/** 57 * Initialize language selector 58 */ 59export function initLanguageSelector() { 60 const currentLang = getCurrentLanguage(); 61 debug('init', `Current language: ${currentLang}`); 62 63 // Update HTML lang attribute 64 document.documentElement.lang = currentLang; 65 66 // Update language button 67 const langButton = document.querySelector('.lang-button'); 68 if (langButton) { 69 const langText = langButton.querySelector('span'); 70 if (langText) { 71 langText.textContent = currentLang === 'ru' ? 'EN' : 'RU'; 72 } 73 74 // Remove existing event listener to prevent duplicates 75 langButton.removeEventListener('click', handleLanguageButtonClick); 76 77 // Add the new event listener 78 langButton.addEventListener('click', handleLanguageButtonClick); 79 } 80} 81 82/** 83 * Dynamically update all translated content on the page. 84 * This function should be called after translations are loaded or language is switched. 85 */ 86export function updateTranslatedContent() { 87 // Update HTML lang attribute 88 const currentLang = getCurrentLanguage(); 89 document.documentElement.lang = currentLang; 90 91 // Update language button text 92 const langButton = document.querySelector('.lang-button'); 93 if (langButton) { 94 const langText = langButton.querySelector('span'); 95 if (langText) { 96 langText.textContent = currentLang === 'ru' ? 'EN' : 'RU'; 97 debug('ui', `Language button updated to: ${langText.textContent}`); 98 } 99 } 100 101 // Re-trigger content population to apply translations without reloading the page 102 if (typeof window.refreshContent === 'function') { 103 window.refreshContent(); 104 } else { 105 debugError('ui', 'refreshContent function not found'); 106 } 107} 108 109
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.