Jump to content

[SOLVED] Languages SuperGlobals!


Crew-Portal

Recommended Posts

Okay, I do know how to do languages like

<? $lang['welcome'] = 'Welcome To CMX Virtual Airlines'; ?>

But when users switch languages it has to be like ?lang=french in my browser window! My question if anyone can answer it is how do I make it so that the language name gets stored in a cookie or something and gets accessed automatically without redirecting the browser like index.php NOT index.php?lang=english. If it helps anyone all of my language commands are stored in the directory 'lang' and each language is entitled by its full name like 'english.php', 'french.php', 'arabic.php' ect ect...! Im not looking for a full long huge script but if someone coud explain in a small amount of text on how to save the language selection in a cookie or session that would be very wonderful. Preferably in a session, but in a cookie if there is no other way!

 

Thank-you in advance in case this doesnt get answered till tommorrow. Be on for another 3 hours or so, so I would love it if I could get the answer by then. Ah I am rabling on so much! Thankx  ::)

Link to comment
Share on other sites

Here is a script I use. It will determine the language based upon four different parameters:

 

1. If a language is passed on the query string (i.e. ?lang=french) then it will set the language to that. It will also set a cookie and a session value for the language. I do both because cookie is a better choice because it can persist for the next visit, but I also use session in case the user has cookies disabled - then their selected session will at least persist for the current session.

 

2. If language has not been set bu the GET variable, then look and see if ther is a valid language set in the cookie. If so, set the language and set the session variable.

 

3. If language did not exist in the GET or in the cookie, check the session variable. If a valid language exists there set language to that.

 

4. If all of the above fail, set language to the default (in this case the first language in my list)

 

So, the script does require sending the language var on the query string, BUT only when the user wants to change the language. Just call the script at the top of your page to determine the language if it is already set. You do not need to send ?lang=french on every page.

 

<?php

function setLanguage($languageListAry) {
  //Check if request was made to change the language
  if (isset($_GET['language']) && in_array($_GET['language'], $languageListAry)) {
  //User has clicked link to change the language
  $language = $_GET['language'];
	setcookie('language', $language, time()+3600*24*365); //Expires in 1 year
	$_SESSION['language'] = $language;
} else if (isset($_COOKIE['language']) && in_array($_COOKIE['language'], $languageListAry)) {
  //User has language saved to cookie
  $language = $_COOKIE['language'];
	$_SESSION['language'] = $language;
} else if (isset($_SESSION['language']) && in_array($_SESSION['language'], $languageListAry)) {
  //User has language saved to session
  $language = $_SESSION['language'];
} else {
  //Default to first language in list
  $language = $languageListAry[0];
}
  return $language;
}

$languages = array ('english', 'spanish', 'french');
$current_lang = setLanguage($languages);

echo "The current language is : " . $current_lang;
echo "<br><br>Choose a language: ";
foreach ($languageListAry as $langName) {
  if ($current_lang==$langName) {
  echo "$langName ";
} else {
    echo "<a href=\"$_SERVER[php_SELF]?language=$langName\">$langName<a> ";
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.