Skip to content Skip to sidebar Skip to footer

Change Language Code Within URL Javascript/nodejs

I have common header file includes navbar in which have multilanguage dropdown. when i select dropdown language, translates page without any issues. But when i move to other pages,

Solution 1:

  1. on load see if language is set or not in cookie.
  2. set selected language in cookie on setLanguage call.

$(document).ready(function() {
  //read previously set cookie value
    var selectedLanguage = $.cookie( 'selectedLanguage' );
  // 1. on load see if language is set or not in cookie.
     selectedLanguage = selectedLanguage ? selectedLanguage : 'en';
     setLanguage(selectedLanguage);
     
     
     function setLanguage(lan){
        $.cookie('selectedLanguage', lan);
     }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js" integrity="sha256-1A78rJEdiWTzco6qdn3igTBv9VupN3Q1ozZNTR4WE/Y=" crossorigin="anonymous"></script>

Post a Comment for "Change Language Code Within URL Javascript/nodejs"