muhammad ahmed muhammad ahmed - 5 months ago 35
Javascript Question

Multiple Language Website PHP

I want to make my PHP website into a multiple language website, with exactly two languages (English, Turkish).
At the top of the web page, there are two icons, one for English and the other for Turkish. When a user clicks on Turkish icon how can I detect that Turkish is selected?

<div><a href="" title="English" id="English" class="active_lang"><img src="images/united-kingdom.png" class= "active" style="float: right; width: 24px;height:24px ;padding: 4px"> </a>

<a href="" title="Turkish" id="Turkish"><img src="images/turkey.png" style="float: right; width: 24px;height:24px; padding: 4px"> </a>


How can I do that?
I have two files for languages, one for English language and then another one for Turkish.

$arrLang['alert_admin_email_wrong']='kullanci email yanliştır '
$arrLang['alert_admin_email_wrong']='your email is wrong '

I must use session or cookies for this problem

Answer Source

The easiest way to do that is to create an cookie when the user clicks on the wanted language. As for the array i would split it per language

$arrLang['en']['alert_admin_email_wrong'] = 'text here';
$arrLang['tr']['alert_admin_email_wrong'] = 'text here turkish';

As for language selection would do this on the webpage

<a href="?langSelect=en">English</a><a href="?langSelect=tr">Turkish</a>

On the language selection page :

$defaultLanguage = isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en'; //default to english language
    //allow only 2 for now ( english / turkish)
    $langForSelection = "";
        case 'en':
        $langForSelection = 'en';
        case 'tr':
        $langForSelection = 'tr';
        setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days

After to show the variables

echo $arrLang[$defaultLanguage]['alert_admin_email_wrong'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download