0

I have a translation plugin that utilizes Google's free website translation tool. I don't use their API, but only provide some creative options in how the tool is used on the website.

How it works: User clicks on flag or drop-down that fires event, then adds ?lang variable to the end of url after translation. User is able to change the url directly by modifying the ?lang url variable in the address bar.

This is tricky, especially because right now I'm using client-side functionality to do the work. I am using location.href to refresh the page, which also adds back the lang variable to the url after navigating to new page.

My problem is when user clicks on link to new page, this is what happens:

  1. User clicks on new page link

  2. Page refreshes to the new url WITHOUT the lang variable.

  3. jQuery kicks in and refreshes page a 2nd time to add the url variable.

  4. New page is now shown to user with the requested url variable.

This is 2 refreshes! Obviously not efficient.

I'm seeing that it might be best to change this to server side refresh instead, using PHP. I need some guidance on how jQuery and PHP will interact, if someone could show me an simple example.

Here is the jQuery code I have now for a single language case, when user changes url directly in the browser.

<script type="text/javascript">
         jQuery(document).ready(function($) {

           $.cookie("language_name", "Afrikaans");
           $.cookie("flag_url", "<?php echo home_url(); ?>/wp-content/plugins/google-language-translator-premium/images/flags24/Afrikaans.png");

           var language_name = $.cookie("language_name");
           var lang = GetURLParameter('lang');
           var googtrans = $.cookie("googtrans");
           var lang_prefix = $('a.af').attr("class").split(" ")[2];

           if (lang == null && language_name == 'Afrikaans') {location.href = document.location.href.split("?")[0] + "?lang=" + lang_prefix;} 

           function GetURLParameter(sParam) {
             var sPageURL = window.location.search.substring(1);
             var sURLVariables = sPageURL.split('&');
               for (var i = 0; i < sURLVariables.length; i++) {
                 var sParameterName = sURLVariables[i].split('=');
                   if (sParameterName[0] == sParam) {
                     return sParameterName[1];
                   }
               }
            }

           if (googtrans != '/en/af') {
             doGoogleLanguageTranslator('en|af');
           }               
         });
       </script>
user229044
  • 232,980
  • 40
  • 330
  • 338
Rob Myrick
  • 859
  • 11
  • 28
  • Use Ajax That way you will only refresh a portion of the site instead of the whole page and still maintain the lang variable you want – Eddwin Paz Jul 04 '14 at 03:21
  • @eddwindpaz I like that idea....can you show me how this would work? Even general example would push me in the right direction. – Rob Myrick Jul 04 '14 at 03:25
  • Go to the jquery documentation site section and type $.get or $.post and see the demos. It's full explained there... – Eddwin Paz Jul 04 '14 at 03:27
  • @RobMyrick if you want to do with jquery and ajax than check this http://stackoverflow.com/q/5004233/1723893 – NullPoiиteя Jul 04 '14 at 03:40

2 Answers2

0

You can use JavaScript to append the lang parameter to every link on page load. You'll need logic to ignore external links and add ?lang or &lang based on the preexistence of a query string. And of course, it wouldn't do anything unless lang was already specified in the current URL.

You could modify your existing code to something like this:

...
if (lang) {
  jQuery("a").each(function () {
    // Logic here
  });
}
else if (language_name == 'Afrikaans') {
  location.href = document.location.href.split("?")[0] + "?lang=" + lang_prefix;
}
...

Take a look at Jquery : Append querystring to all links for examples of how to append the query string parameter.

Community
  • 1
  • 1
Brandon Gano
  • 6,430
  • 1
  • 25
  • 25
0
<?php
    if (empty($_GET['lang'])) {
       header('Location: $_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING'].'&lang=en');
       die;
    }
?>

This could have a bit more logic to it, but it is the basic idea. This code would have to be as close to the top of the page as possible, because headers are only allowed to be sent before any output is printed on the page.

i--
  • 4,349
  • 2
  • 27
  • 35