Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Drop down box language selection

  • 12-12-2007 8:59pm
    #1
    Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭


    I'm trying to use AJAX to have a select box at the top of the page and then it would have multiple language choices, when a user clicks say French i want:
    [php]include('languages/english.template.php');[/php]to be changed to:
    [php]include('languages/french.template.php');[/php]and the page reloads with French all the way through.

    I was thinking of some sort of if statement like(english being the default language):
    [php]if($language == "fr")
    {
    include('french.template.php');
    }
    else
    {
    include('english.template.php');
    }[/php]But i cant figure out how to assign the selected language to $language and reload the page in AJAX, as i want the language change to be seamless!
    Its just the initial selection change that i cant sort as i can use a session variable to follow the user with the language once changed. All help appreciated!

    This is what i have so far:
    index.php
    [php]<?php session_start();
    if(isset($_SESSION))
    {
    include('lang/'.$_SESSION.'.template.php');
    $lang = $_SESSION;
    }
    else
    {
    include('lang/en.template.php');
    $lang = "en";
    }
    ?>
    <html>
    <head>
    <title><?php echo $txt; ?></title>
    <link href="style/style.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" language="javascript">
    var loading_img = 'images/loading.gif';
    var loading_msg = 'Loading Language...';
    var xmlhttp_obj = false;

    function ewd_xmlhttp()
    {
    if(window.XMLHttpRequest)
    {
    xmlhttp_obj = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    try
    {
    xmlhttp_obj = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
    try
    {
    xmlhttp_obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {

    }
    }
    }
    else
    {
    xmlhttp_obj = false;
    }
    return xmlhttp_obj;
    }
    function getcontent(url, containerid)
    {
    var xmlhttp_obj = ewd_xmlhttp();
    document.getElementById(containerid).innerHTML = '<img src="'+loading_img+'"> '+loading_msg;
    xmlhttp_obj.onreadystatechange=function()
    {
    loadpage(xmlhttp_obj, containerid);
    }
    xmlhttp_obj.open('GET', url, true);
    xmlhttp_obj.send(null);
    }
    function loadpage(xmlhttp_obj, containerid)
    {
    if(xmlhttp_obj.readyState == 4 && xmlhttp_obj == 200)
    {
    document.getElementById(containerid).innerHTML = xmlhttp_obj.responseText;
    }
    }
    //]]>
    </script>

    </head>
    <body>
    <div id="primary_wrapper">
    <div id="top_bar">
    <div id="secondary_wrapper_top">
    <div id="lang_text"><?php echo $txt; ?>:</div>
    <div id="lang_selectbox">
    <form method="post" action="http://localhost<?php echo $_SERVER; ?>">
    <select name="lang" id="lang" onchange="getcontent('getLang.php?lang='+this.value, 'lang_load');" class="form">
    <option value="en">English</option>
    <option value="fr">Français</option>
    </select>
    </form>
    </div>
    <div id="lang_load"></div>
    </div>
    </div>
    <div id="secondary_wrapper">
    <div id="test_text"><?php echo $txt; ?></div>
    </div>
    </div>
    </body>
    </html>[/php]getLang.php
    [php]<?php session_start();
    $qs = $_GET != "" ? preg_replace("#[^0-9#","",$_GET) : "1";

    if($qs != "")
    {
    if($_GET != "en")
    {
    $_SESSION = $_GET;
    }
    else
    {
    $_SESSION = "en";
    }
    }
    ?>[/php]


Comments

  • Closed Accounts Posts: 1,200 ✭✭✭louie


    i thought you got that sorted out on irish webmaster forums?


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    I wouldn't use ajax there at all, it means your javascript has to pull in all translations for the new language and go through the page replacing them, or am I missing something?

    I'd just put two lang links, each does a normal page fetch that passes the lang arg in $_GET, then on the server include the right template and set your session vars.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    sorry democrates, but that's exactly what AJAX is supposed to do.

    Do not compare it with javascript itself.


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    louie wrote: »
    sorry democrates, but that's exactly what AJAX is supposed to do.

    Do not compare it with javascript itself.
    In fairness, Ajax - Asynchronous Javascript and Xml. Hard not to bring up javascript.

    Yes you can do it in ajax, but that doesn't mean it's the right tool for this job is my point.

    Why use ajax in a once off job of trawling through the page now sitting in the browser to swap all labels for the other language just selected, when for the rest of the session the selected language will be served out based on the template indicated in the session variable?

    Leave that job on the server would be my approach, whereas this seems a bit like ajax for the sake of ajax.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    Why?

    Because with AJAX, you can bring fresh content from the server and database, without the complete page refresh which saves time and bandwith.

    Is there any point in refreshing the entire page when all I want to do is give a one line message after submitting a form, or isn't it better to make just 1-2 calls to the DB instead 10-20?


  • Advertisement
  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Have you seen this article from 24ways?


Advertisement