// WEEBLY MULTILANGUAGE TOOLS // http://blessingsweb.weebly.com/weebly-multilanguage.html /* // Things that should go in SETTINGS -> SEO -> Header Code : // (all page names in should go in lowercase) var langs = [ //lang_code, name of first page of that language, HTML of the language changing link ['en', 'home', 'English'], ['es', 'inicio', 'EspaƱol'], ]; var not_clickable = []; var all_langs = []; */ var all_langs = []; var not_clickable = []; var multilanguage_icon = null; var LANG_LINKS_TOP_RIGHT = 'T-R'; var LANG_LINKS_TOP_LEFT = 'T-L'; var LANG_LINKS_SOCIAL = 'SOC'; var LANG_LINKS_MY_OWN = 'OWN'; var lang_links_pos = LANG_LINKS_TOP_RIGHT; var lang_links_top_add_background = true; //inner variables from now on var localStorage_enabled = typeof(Storage) !== "undefined"; try {language = language;} catch(e) {var language = null;} //if language is not defined, use null console.log('lenguaje es: '+language); // Function that sets up all features of multilanguage. // Alternatively, you can call these functions one by one. function setup_multilanguage() { hide_menus(); hide_items(); add_lang_links(); make_not_clickable(); changeLogo(); } function add_lang_links() { jQuery(document).ready(function() { console.log('adding language links - ' + lang_links_pos); var IS_TOP = lang_links_pos == LANG_LINKS_TOP_LEFT || lang_links_pos == LANG_LINKS_TOP_RIGHT if (is_weebly_mobile()) { //jQuery('body').prepend(''); //jQuery('#header').after('') //jQuery('body').prepend(''); jQuery('body').append(''); jQuery('#lang_links').css('position', 'absolute').css('top', '0').css('right', '0'); //jQuery('#lang_links').css('background', 'white'); jQuery('#lang_links').css('z-index', 1000); //jQuery('#lang_links').css('left', '0'); } else if (IS_TOP) { console.log('adding #langs_link to top of the page'); jQuery('body').append(''); jQuery('#lang_links').css('position', 'absolute').css('top', '0'); jQuery('#lang_links').css(lang_links_pos == LANG_LINKS_TOP_LEFT ? 'left' : 'right', '0'); if (lang_links_top_add_background) jQuery('#lang_links').css('background', 'rgba(255,255,255,0.5)'); jQuery('#lang_links').css('z-index', '999'); } else if (lang_links_pos == LANG_LINKS_SOCIAL) { console.log('adding #langs_link to .social'); jQuery('.wsite-social').prepend(''); jQuery('#lang_links').css('vertical-align', 'middle'); } else if (lang_links_pos == LANG_LINKS_MY_OWN) { console.log('not adding #langs_link'); } jQuery('#lang_links').css('text-align', 'right'); jQuery('#lang_links .lang_link').css('padding', '4px'); jQuery('#lang_links').css('padding', '0px 5px'); if (multilanguage_icon) { jQuery('#lang_links').prepend(''+multilanguage_icon+' '); if (lang_links_pos == LANG_LINKS_SOCIAL) jQuery('.icon_lang_link').css('vertical-align', 'sub'); } else { if (IS_TOP) jQuery('#lang_links').css('padding', '5px'); } append_lang_links(); //if (is_weebly_mobile()) jQuery('#lang_links_spacer').height(jQuery('#lang_links').height()); }); } function append_lang_links() { lang_links_div = jQuery('#lang_links'); console.log('appending lang links to:'); console.log(lang_links_div); //add the language links for (var i = 0; i < langs.length; i++) { var lang_code = langs[i][0]; var lang_link_text = langs[i][2]; var link = (lang_code == language) ? lang_link_text : '' + lang_link_text + ''; console.log('language ' + lang_code + ': ' + link); if (i > 0) lang_links_div.append(' '); lang_links_div.append('' + link + ''); } } function hide_items() { setup_language(); var lang_class = 'lang_'+language; console.log('hiding language items... checking for ' + lang_class); jQuery('begin_lang').each(function() { var begin = jQuery(this); var must_hide = !begin.hasClass(lang_class); console.log(this); console.log(must_hide ? 'HIDE' : 'SHOW'); if (must_hide) { elems = begin.parent().parent().nextAll().toArray(); for (var i = 0; i < elems.length; i++) { var elem = jQuery(elems[i]); var is_end_lang = elem.find('end_lang').is('end_lang'); if (is_end_lang) break; elem.hide(); } } }); } function hide_menus() { console.log('hide menus - ' + (is_weebly_mobile() ? 'mobile' : 'desktop')); var menu_selector = '.wsite-menu-default'; setup_language(); console.log('Language: ' + language); var current_lang = null; var matched_langs = []; //jQuery('.wsite-menu-item-wrap').each(function(){ jQuery('.wsite-menu-default li').each(function(){ var menu = jQuery(this); var a = menu.children('a'); var page_url = a.attr('href'); var text = get_menu_name(a); in_all_langs = all_langs.indexOf(text) >= 0; matched_langs = []; //matched_langs eems to be causing trouble here, erasing it every time! var lang_info = get_lang_info_for_page(text, matched_langs); if (lang_info != null) { if (lang_info.length == 3) lang_info.push(page_url); current_lang = lang_info[0].toLowerCase(); //if this is the first page of this language, set the logo to go to this page url if (current_lang == language) { jQuery('.wsite-logo a').attr('href', page_url); } } var show = current_lang == language; if (in_all_langs) { current_lang = null; show = true; } if (show) menu.show(); else { menu.hide(); menu.css('display', 'none !important!'); } console.log( (show ? 'SHOW - ' : 'HIDE - ') + current_lang + ' - ' + text + (langs[text] != null ? ' - in langs: ' + langs[text] : '') + (in_all_langs ? ' - in all_langs' : '')); }); } function make_not_clickable() { //top_menus = jQuery('.wsite-menu-item-wrap'); //submenus = jQuery('.wsite-menu-subitem-wrap'); top_menus = jQuery('.wsite-menu-default li'); submenus = jQuery('.wsite-menus li'); menus = jQuery.merge(top_menus, submenus); menus.each(function(){ var a = jQuery(this).children('a'); var text = get_menu_name(a); clickable = (not_clickable.indexOf(text) < 0); console.log('clickable - ' + (clickable ? 'yes' : 'NOT') + ' - ' + text); if (!clickable) { a.attr('href', 'javascript:void'); a.hover(function() { jQuery(this).css('cursor', 'default'); }); } }); } function setup_language() { if (language == 'all' || language == null) { if(localStorage_enabled == null) language = localStorage.getItem("language"); else language = langs[0][0]; //default_language } language = language.toLowerCase(); if(localStorage_enabled) localStorage.setItem("language", language); } function get_lang_info_for_page(page_name, matched_langs) { for (var i = 0; i < langs.length; i++) { var lang_info = langs[i]; if ( lang_info[1].toLowerCase() == page_name.toLowerCase() && matched_langs.indexOf(lang_info[0]) == -1) { matched_langs.push(lang_info[0]); return lang_info; } } return null; } function get_lang_info_for_lang(lang) { for (var i = 0; i < langs.length; i++) { var lang_info = langs[i]; if (lang_info[0].toLowerCase() == lang) return lang_info; } return null; } function change_lang(lang) { var lang_info = get_lang_info_for_lang(lang); if(lang_info[1] == 'inicio'){ lang_info[1] = '/'; } document.location.href = lang_info[1]; } function get_menu_name(menu_a) { var span = menu_a.children('span'); var text = (span.length == 0) ? menu_a.html() : span.html(); return text.toLowerCase().trim(); } // check if it is web or mobile version function is_weebly_mobile() { return jQuery('.wsite-mobile').length || jQuery('.mobile-nav').length || jQuery('#navmobile').length; } function changeLogo(){ console.log('el item es: '+language); if(language == 'en'){ $(".wsite-logo a img").attr('src','/files/theme/images/logo-en.png'); }else if(language == 'es'){ //console.log('el item es: '+language); $(".wsite-logo a img").attr('src','/uploads/5/9/3/7/59374109/editor/logo-es_1.png?1535657801'); } }