var translations = new Array (); window.translations = new Array (); var currentLanguage = 'es'; window.currentLanguage = 'es'; function addTranslation (englishString, language, translatedString) { try { if (translations [englishString] == null) { translations [englishString] = new Array (); translations [englishString] ['en'] = englishString; } translations [englishString] [language] = translatedString; } catch (e) { alert ('addTranslation failed : ' + e.name + ', ' + e.message); } } function setupTranslations () { try { } catch (e) { alert ('setupTranslations (es) failed : ' + e.name + ', ' + e.message); } } // BUG : Je n'arrive pas à mettre translations en variable globale, statique function translate (sourceString) { if (sourceString == '') { return ''; } if (translations.length == 0) { try { setupTranslations (); } catch (e) { alert ('Failed trying to setup the translations : ' + e.toString ()); } } //alert ('CL:' + currentLanguage); if (translations [sourceString] != null) { return translations [sourceString] [currentLanguage]; } else { //alert ('No translation found for "' + sourceString + '"'); return sourceString; } } function selectLanguage (languageCode) { currentLanguage = languageCode; window.currentLanguage = languageCode; } function translateImgAltAttributes (element) { try { if (element != null) { var imageElements = element.getElementsByTagName('IMG'); var i = 0; for (imageIndex = 0; imageIndex < imageElements.length; imageIndex ++) { image = imageElements [imageIndex]; if (i) {alert ('image source : ' + image.src);} if (i) {alert ('image alt : ' + image.alt); i--;} var translatedAlt = translate (image.alt); if (translatedAlt != image.alt) { //alert (image.alt + "=>" + translatedAlt); image.alt = translatedAlt; } } } } catch (e) { alert ('translateImgAltAttributes failed : ' + e.name + ', ' + e.message); } } function translateSpanTexts (element) { if (element != null) { var spanElements = element.getElementsByTagName ('SPAN'); //alert (spanElements.length + ' spans to translate'); for (spanIndex = 0; spanIndex < spanElements.length; spanIndex ++) { if (spanElements[spanIndex].className == 'translatable') { var translatedText = translate (spanElements[spanIndex].innerText); if (translatedText != spanElements[spanIndex].innerText) { spanElements[spanIndex].innerText = translatedText; } } } } } function translateTags (element, tag) { if (element != null) { var tagElements = element.getElementsByTagName (tag.toUpperCase ()); //alert (tagElements.length + ' ' + tag + ' elements found'); for (tagIndex = 0; tagIndex < tagElements.length; tagIndex ++) { var innerText = tagElements[tagIndex].innerText; var translatedText = translate (innerText); if (translatedText != innerText) { //alert (innerText + "=>" + translatedText); tagElements[tagIndex].innerText = translatedText; } } } } function translateDocumentTitle () { try { document.title = translate (document.title); } catch (e) { alert ('translateDocumentTitle () failed : ' + e.name + ', ' + e.message); } } // TODO : rename to translateOptionTexts function translateOptionValues (element) { try { if (element != null) { var selectElements = element.getElementsByTagName ('SELECT'); //alert (selectElements.length + ' selects to translate'); for (selectIndex = 0; selectIndex < selectElements.length; selectIndex ++) { for (optionIndex = 0; optionIndex < selectElements [selectIndex].options.length; optionIndex ++) { //alert (selectElements[selectIndex].options [optionIndex].text); var translatedText = translate (selectElements[selectIndex].options [optionIndex].text); if (translatedText != selectElements[selectIndex].options [optionIndex].text) { /* if (selectElements[selectIndex].options [optionIndex].value == '') { selectElements[selectIndex].options [optionIndex].value = selectElements[selectIndex].options [optionIndex].text; } */ selectElements[selectIndex].options [optionIndex].text = translatedText; } } } } } catch (e) { alert ('translateOptionValues (' + element + ') failed : ' + e.name + ', ' + e.message); } } function translateTagAttributes (element, tagName, attributeName) { try { if (element != null) { var tagElements = element.getElementsByTagName(tagName); //alert (tagElements.length + ' elements ' + tagName + ' found'); for (elementIndex = 0; elementIndex < tagElements.length; elementIndex ++) { tagElement = tagElements [elementIndex]; //alert ('Attribute : ' + tagElement.getAttribute (attributeName)); var translatedAttribute = translate (tagElement.getAttribute (attributeName)); if (translatedAttribute != tagElement.getAttribute (attributeName)) { //alert (tagElement.getAttribute (attributeName) + "=>" + translatedAttribute); tagElement.setAttribute (attributeName, translatedAttribute); } } } } catch (e) { /*alert ('translateTagAttributes (' + element + ', ' + tagName + ', ' + attributeName + ') failed : ' + e.name + ', ' + e.message);*/ } }