Dynamic Switching Language PHP, Javascript, jQuery UI

Recently, I began to implement language settings for my site. Here's how it basically works:

if(isset($_GET['lang'])) { $langr= $_GET['lang']; $_SESSION['lang'] = $langr; setcookie('lang', $langr, time() + (3600 * 24 * 30)); } elseif(isset($_SESSION['lang'])) { $langr = $_SESSION['lang']; } elseif(isset($_COOKIE['lang'])) { $langr = $_COOKIE['lang']; } else { $langr = 'en'; } switch ($langr) { case 'en': $lang_file = $_SERVER['DOCUMENT_ROOT']."/includes/lang/en.php"; break; case 'fr': setlocale (LC_ALL, 'fr_FR.utf8','fra'); $lang_file = $_SERVER['DOCUMENT_ROOT']."/includes/lang/fr.php"; break; default: $lang_file = $_SERVER['DOCUMENT_ROOT']."/includes/lang/en.php"; } include_once $lang_file; 

It downloads the corresponding language file at the top of every page of my website. However, problems start to arise when using jQuery UI datepicker. I found the regional settings file for datepicker online and created a separate file for it:

 ///jquery.ui.datepicker-fr.js file jQuery(function($){ $.datepicker.regional['fr'] = { closeText: 'Fermer', prevText: 'Précédent', nextText: 'Suivant', currentText: 'Aujourd\'hui', monthNames: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'], monthNamesShort: ['janv.', 'févr.', 'mars', 'avril', 'mai', 'juin', 'juil.', 'août', 'sept.', 'oct.', 'nov.', 'déc.'], dayNames: ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'], dayNamesShort: ['dim.', 'lun.', 'mar.', 'mer.', 'jeu.', 'ven.', 'sam.'], dayNamesMin: ['D','L','M','M','J','V','S'], weekHeader: 'Sem.', dateFormat: 'dd/mm/yy', firstDay: 1, isRTL: false, showMonthAfterYear: false, yearSuffix: ''}; $.datepicker.setDefaults($.datepicker.regional['fr']); }); 

The problem is that I need to dynamically load jquery.ui.datepicker-fr.js whenever the language changes so that my datpick switches to French. What I thought was to create a PHP function that I would put in my <head> that will output my file, but the problem is that not all my web pages are PHP, and I do not consider this a very efficient method Any other ideas on how to do this?

+6
source share
6 answers

You need to put a check in your .php header, which will look like this:

 <script type="text/javascript>" src="/js/<?php if($_SESSION['lang'] == "en"){ echo 'datepicker-en.js'}else echo 'datepicker-fr.js'; ?>"></script> 

Thus, it will automatically detect the datepicker file according to your language setting.

+1
source

I would do so, I have a separate PHP file responsible for getting the correct JS file, so you can use it in the HTML pages of the plan too ...

 <script type="text/javascript" src="/getCorrectJsFile.php"></script> 
+1
source

What do you think of this approach?

 function getBaseURL() { var url = location.href; var baseURL = url.substring(0, url.indexOf('/', 14)); if (baseURL.indexOf('http://localhost') != -1) { var url = location.href; var pathname = location.pathname; var index1 = url.indexOf(pathname); var index2 = url.indexOf("/", index1 + 1); var baseLocalUrl = url.substr(0, index2); return baseLocalUrl + "/"; } else { return baseURL + "/"; } } function getCookie(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==c_name) { return unescape(y); } } } function getLang() { var lang = getCookie("lang"); switch(lang) { case "fr": document.write("<script src='"+getBaseURL()+"js/lang/jquery.ui.datepicker-"+lang+".js'></script>"); break; default: document.write(""); } } getLang(); 

The functions will be located in a common js file, and I would call the getLang() function on all my pages ...

0
source

What about using .change () and .post ()? You add the id from lang to your form where the user can change the language.

 $('#lang').change(function(){ //link to your php file var findlangfile = "<?php echo index.php/findlang.php;?> $.post(findlangfile, { lang: $('#lang').val(), }, function(response){ // you do things }); return false; }); 
0
source

You can use jQuery cookie plugin, good one. Then follow the same procedure with jQuery as with php. If the language is set as the url parameter, set the cookie with the plugin (get the language value using the getBaseURL () function above). Otherwise, download the cookie and download the appropriate javascript file. Something like $('head').append('<script..'); must complete the task.

Be sure to run this entire script when loading the page using $(function() { /* code here */ });

Although it’s possible to accomplish this task, I highly recommend switching all your pages to php, as this will simplify things like this.

0
source

Improved version of Moid's answer:



You need to put a check in your .php header, which will look like this:
 <script src="/js/<?php echo 'datepicker-' . *COOKIE/SESSION VALUE, LIKE en* . '.js'; ?>"></script> 

This will dynamically select datepicker-???.js , where ??? is the value represented by the cookie / session cookie.

0
source

Source: https://habr.com/ru/post/946913/


All Articles