Guest User

MULTI-LANGUAGE Javscript for Website

a guest
Jul 8th, 2020
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.6.0/css/flag-icon.css">
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  3. <script>
  4.     $(function() {
  5.     /* SETUP MULTI-LANGUAGE */
  6.     var defaultLanguage = 'en';
  7.     var lang = location.pathname.split("/")[1];
  8.     var defaultClass = 'lang-'+defaultLanguage+'';
  9.     var itemParent = "nav [class*='collection'],nav [class*='folder'],nav [class*='index'],nav [class*='group']";
  10.     if (lang == "" || lang.length > 2 ){
  11.       var lang = defaultLanguage;
  12.     }
  13.  
  14.     /* ADD LANGUAGE CLASSES */
  15.     $('a[href="/"]').addClass('lang-'+defaultLanguage+'').parents(itemParent).addClass('lang-'+defaultLanguage+'');
  16.     $('nav a:link:not([href^="http://"]):not([href^="https://"])').each(function () {
  17.       var langType = $(this).attr('href').split("/")[1];
  18.       var multiLanguageClass = 'multilanguage lang-' + langType + '';
  19.       if (undefined !== langType && langType.length <= 2)
  20.         $(this).addClass(multiLanguageClass).parents(itemParent).addClass(multiLanguageClass);
  21.     });
  22.     $('nav button').each(function () {
  23.       var langTypeFolder = $(this).attr('data-controller-folder-toggle').split("/")[0];
  24.       var multiLanguageClass = 'multilanguage lang-' + langTypeFolder + '';
  25.       if (undefined !== langTypeFolder && langTypeFolder.length <= 2)
  26.         $(this).addClass(multiLanguageClass);
  27.     });
  28.  
  29.     /* HOMEPAGE-LOGO LINKS TO PROPER LANGUAGE HOMEPAGE */
  30.     if (lang == "fr") {
  31.       $('a[href="/"]').attr("href", "/fr/home/");
  32.     }
  33.  
  34.     /* ADD EXCLUSION NAV ITEMS */
  35.     $('.exclude-me,.exclude-me a').addClass('exclude');
  36.     $('.sqs-svg-icon--list a,.SocialLinks-link').addClass('exclude');
  37.  
  38.     /* REMOVE OTHER LANGUAGES AND KEEP EXCLUDED ITEMS */
  39.     $('.multilanguage:not(".lang-'+lang+',.exclude")').remove();
  40.  
  41.     /* SETUP LANGUAGE SWITCHER */
  42.     $('body').prepend('<div class="language"><a href="/en/home" class="lang-en"><span class="flag-icon flag-icon-gb"></span></a> <a href="/fr/home/" class="lang-fr"><span class="flag-icon flag-icon-fr"></span></a></div>');
  43.  
  44.   });
  45. </script>
RAW Paste Data