-4

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>

</div>

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

  • Google "php multiple languages" and you'll find many resources. See https://stackoverflow.com/questions/19249159/best-practice-multi-language-website – Barmar Oct 03 '17 at 06:43

1 Answers1

0

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
if(isset($_GET['langSelect'])){
    //allow only 2 for now ( english / turkish)
    $langForSelection = "";
    switch($_GET['langSelect']){
        case 'en':
        $langForSelection = 'en';
        break;
        case 'tr':
        $langForSelection = 'tr';
        break;
        default:
        break;
    }
    if(isset($langForSelection)){
        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'];
Mike X
  • 129
  • 1
  • 7
  • thank you very much for your help but I don't understand that the you save cookie with variable $langForSelection and you use $defaultLanguage – muhammad ahmed Oct 03 '17 at 06:57
  • when I click on any other link the language is return to english lang even if I choose turkish language – muhammad ahmed Oct 03 '17 at 07:13
  • The cookie is created only if you selected an language, $langForSelection is null ( empty ) at the start, and after the checks we create or not the cookie. After $defaultLanguage checks if there is an cookie and it is outside the if(..$_GET) , the $defaultLanguage keeps the current language that it is selected ( if none, english is default ). – Mike X Oct 03 '17 at 08:26