Jump to content

Multilingual website with automatic redirection

Recommended Posts

Hi,

I know Squarespace can be made multilingual with Weglot, and Weglot uses browser language based redirection. Also, there is the Geo Targetly app that offers geo redirection based on IP address.

I have used a workaround method with some code to translate some of my clients' Squarespace websites. This requires duplicate pages for each language and uses code to display the correct ones in the navigation based on the language selection. The advantage of this method is that it's free (no app subscription which can be quite high compared to Squarespace's own plan). 

Is it possible to enhance this code to allow automatic redirection based on visitor's browser language or IP address?

Below is the code I've used for the website: https://www.wizardscreeklabradors.com/

I would appreciate it a lot if anybody figured out how to add automatic redirection to it. 🙏

Thank you in advance!

K.

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
    $(function() {
    /* SETUP MULTI-LANGUAGE */
    var defaultLanguage = 'et';
    var lang = location.pathname.split("/")[1];
    var defaultClass = 'lang-'+defaultLanguage+'';
    var itemParent = "nav [class*='collection'],nav [class*='folder'],nav [class*='index'],nav [class*='group'],nav [class*='external']"; 
    if (lang == "" || lang.length > 2 ){
      var lang = defaultLanguage;
    }
      
   /* ADD LANGUAGE CLASSES */
    $('a[href="/"]').addClass('lang-'+defaultLanguage+'').parents(itemParent).addClass('lang-'+defaultLanguage+'');
    $('nav a:link:not([href^="http://"]):not([href^="https://"])').each(function () {
      var langType = $(this).attr('href').split("/")[1];
      var multiLanguageClass = 'multilanguage lang-' + langType + '';
      if (undefined !== langType && langType.length <= 2) 
        $(this).addClass(multiLanguageClass).parents(itemParent).addClass(multiLanguageClass);
    });
    $('nav button').each(function () {
      var langTypeFolder = $(this).attr('data-controller-folder-toggle').split("/")[0];
      var multiLanguageClass = 'multilanguage lang-' + langTypeFolder + '';
      if (undefined !== langTypeFolder && langTypeFolder.length <= 2) 
        $(this).addClass(multiLanguageClass);
    });

    /* HOMEPAGE-LOGO LINKS TO PROPER LANGUAGE HOMEPAGE AND CORRECT FOOTER IS DISPLAYED */
      if (lang == "et") {
      $('a[href="/"]').attr("href", "/et/avaleht/");
      $('[data-section-id="652bc519a3596101f8999ce2"]').hide();
    }
    if (lang == "en") {
      $('a[href="/"]').attr("href", "/en/home/");
      $('[data-section-id="6499f88a46606b4c4f9a1223"]').hide();
    }

    /* ADD EXCLUSION NAV ITEMS */
    $('.exclude-me,.exclude-me a').addClass('exclude');
    $('.sqs-svg-icon--list a,.SocialLinks-link').addClass('exclude');

    /* REMOVE OTHER LANGUAGES AND KEEP EXCLUDED ITEMS */
    $('.multilanguage:not(".lang-'+lang+',.exclude")').remove();

    /* SETUP LANGUAGE SWITCHER */
    $('body').prepend('<div class="language"><a href="/et/avaleht/" class="lang-et">Est</a> | <a href="/en/home/" class="lang-en">Eng</a></div>');
      
    /* UPDATE CTA BUTTON */
    if (lang == "en") {
    $('.header-actions-action--cta a, .header-menu-cta a').text('Contact us').attr('href', 'https://www.wizardscreeklabradors.com/en/contact');
    }

  });
</script>

 

Link to comment
  • Replies 0
  • Views 1.1k
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.