Tagged: 

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1257987

    Dear all,

    I just updated the Enfold theme to the latest version. Our website is bilingual, where text of each language is stored within a designated span-tag on one and the same page. E.g.:

    <span lang=”en”>Hello World!</span><span lang=”de”>Hallo Welt!</span>

    A JavaScript in the “Google Analytics Tracking Code” textarea is responsible for displaying the one or the other language (depending on which cookie was set by deciding for a language). Here is the code:

    <script>
    
    jQuery(document).ready(function(){
    
     if( getCookie("language") == "de" ) {
    
      langElements = getAllElementsWithAttributeValue('lang','en');
    
      for (var i in langElements) {
    
       langElements[i].style.visibility = 'hidden';
    
       langElements[i].style.display = 'none';
    
      }
    
     }
    
     else {
    
      langElements = getAllElementsWithAttributeValue('lang','de');
    
      for (var i in langElements) {
    
       langElements[i].style.visibility = 'hidden';
    
       langElements[i].style.display = 'none';
    
      }
    
     }
    
    });
    
    function getCookie(cname) {
    
        var name = cname + "=";
    
        var ca = document.cookie.split(';');
    
        for(var i = 0; i <ca.length; i++) {
    
            var c = ca[i];
    
            while (c.charAt(0)==' ') {
    
                c = c.substring(1);
    
            }
    
            if (c.indexOf(name) == 0) {
    
                return c.substring(name.length,c.length);
    
            }
    
        }
    
        return "";
    
    }
    
    function getAllElementsWithAttributeValue(attribute,attributevalue)
    
    {
    
      var matchingElements = [];
    
      var allElements = document.getElementsByTagName('*');
    
      for (var i = 0, n = allElements.length; i < n; i++)
    
      {
    
        if (allElements[i].getAttribute(attribute) == attributevalue)
    
        {
    
          // Element exists with attribute. Add to array.
    
          matchingElements.push(allElements[i]);
    
        }
    
      }
    
      return matchingElements;
    
    }
    
    </script>

    Since the update this no longer works (I checked out, the code is still in the “Google Analytics Tracking Code” textarea). Why is the script no longer executed? Any suggestions?

    Thanks a lot and all the best,
    Christian

    #1259085

    Hey kristianjohann,
    Sorry for the late reply and thanks for the link and code. Looking at your script above I note that it is looking for a two-character language code, when typically four characters, like de-DE or en-US I could not quite make out how you are switching languages with your links, I believe there is another script at work. Anyways, I tested your script on my localhost by setting the language with this document.documentElement.setAttribute('lang','de-DE');
    and I changed your script a little:

    
    <script>
    jQuery(document).ready(function(){
     if( document.documentElement.lang.toLowerCase() === "de-de" ) {
    
      langElements = getAllElementsWithAttributeValue('lang','en');
    
      for (var i in langElements) {
    
       langElements[i].style.visibility = 'hidden';
    
       langElements[i].style.display = 'none';
    
      }
    
     }
    
     else {
    
      langElements = getAllElementsWithAttributeValue('lang','de');
    
      for (var i in langElements) {
    
       langElements[i].style.visibility = 'hidden';
    
       langElements[i].style.display = 'none';
    
      }
    
     }
    });
    
    function getAllElementsWithAttributeValue(attribute,attributevalue)
    
    {
    
      var matchingElements = [];
    
      var allElements = document.getElementsByTagName('*');
    
      for (var i = 0, n = allElements.length; i < n; i++)
    
      {
    
        if (allElements[i].getAttribute(attribute) == attributevalue)
    
        {
    
          // Element exists with attribute. Add to array.
    
          matchingElements.push(allElements[i]);
    
        }
    
      }
    
      return matchingElements;
    
    }
    </script>
    

    and I used your example HTML:

    <span lang="en">Hello World!</span><span lang="de">Hallo Welt!</span>

    and this worked for me, but on your site I can not change the language with the icons, so you could check your script for the two-character language code, or you could try this script instead.

    Best regards,
    Mike

    #1259493

    Dear Mike,

    Thanks a lot for your feedback and the information. Indeed, the language switcher is an extra-script that sets a cookie-value depending on which language-icon of the website was pressed. And then, the script in enfold’s “Google Analytics Tracking Code” area checks out whether “de” or “en” is set as cookie value and hides the respective content in the alternative language.

    I’ve tried out your script and it still does not work. I think the problem is that the ready-function is not triggered.

    To check, I tried out, e.g.:

    <script>
    jQuery(document).ready(function(){
    alert("Ready!");
    });
    </script>

    which did not work; as an alternative, I tried out:

    <script>
    window.addEventListener('load', function () {
    alert("Ready!");
    });
    </script>

    which also did not work. Is there any possibility to trigger the ready-function (or the eventListener “load”)?

    Thanks again and all the best,
    Christian

    #1259549

    Hi,
    Please include the language switcher script so we can test it also.
    I tested your two alert on load scripts above and both worked for me.

    Best regards,
    Mike

    #1260099
    This reply has been marked as private.
    #1260537

    Hi,
    Sorry for the late reply and thanks for the login. I couldn’t get your script to work on my localhost. I did find that there is an issue with the Google Analytics theme option, this has been reported to the dev team.
    Meanwhile, a better approach to adding your scripts would be in your functions.php file in Appearance > ThemeS > Editor
    So for example your alert script would look like this:

    function custom_script(){
      ?>
      <script>
    
    jQuery(document).ready(function(){
    alert("Ready!");
    });
    
    </script>
    <?php
    }
    add_action('wp_footer', 'custom_script');

    Please note that for each script you add you will need to change the custom_script part of the code to be unique, no two functions can have the same name.

    Best regards,
    Mike

    #1260860

    Dear Mike,

    Thanks a lot for the information. That’s very helpful. I placed the code in the functions.php file and now everything works properly. Clearly, if the Google Analytics area would work out, this would be preferable, because changes in the functions.php file have to be restored manually after an update whereas changes in the Google Analytics area are kept also across updates. But that everything works now again is already really very helpful. Thanks again — very much appreciated.

    All the best and take care,
    Christian

    #1260866

    Hi,

    Did you need additional help with this topic or shall we close?

    Best regards,
    Jordan Shannon

    #1260867

    Hi,

    Thanks a lot, I’m fine. I think we can close this topic.

    Thanks again and all the best,
    Christian

    #1260868

    Hi,

    If you need additional help, please let us know here in the forums.

    Best regards,
    Jordan Shannon

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Language Switcher JavaScript stopped working after Enfold update’ is closed to new replies.