0

I'm developing a multilanguage webpage with php and mysql on my local dev environment. Everything fine till session default language value.

My language detect script looks like this:

<?php
ob_start();
session_start();
header('Cache-control: private');
if(isset($_GET["lang"]))
    {
    $lang = $_GET["lang"];
    $_SESSION["lang"] = $lang;
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
    }
elseif(isset($_SESSION["lang"])) {
    $lang = $_SESSION["lang"];
}
elseif(isset($_COOKIE["lang"])) {
    $lang = $_COOKIE["lang"];
}
else {
    $lang = "tr";
}
switch ($lang) {

  case "tr":
  break;

  default:
  $lang = "tr";

}
ob_end_flush();
?>

When i try to call a page

localhost/production/sample.php?i=1

it didnt call values from mysql because of can not detect default language.

But when i try to call

localhost/production/sample.php?i=1&lang=tr

It works perfectly. What am i missing? What is wrong with my language detect script? Any help greatly appricated.

HddnTHA
  • 1,041
  • 3
  • 19
  • 38
  • 1
    so how does the rest of your code look for a language-to-use? If everything falls through and ends up at your default `else` clause, you never seem to be storing that language anywhere. So if the rest of your code is looking for `$_SESSION['lang']`, that'll never get set because you never store that default language anywhere except in $lang. – Marc B Sep 02 '14 at 17:15
  • @MarcB Thanks for comment. So how could i achieve it? – HddnTHA Sep 02 '14 at 17:19
  • put the session/cookie stuff into your `else` clause as well? – Marc B Sep 02 '14 at 17:19
  • @MarcB aha it works. thanks so much :) I can accept your comment an answer if you want to change it as an answer. – HddnTHA Sep 02 '14 at 17:30

1 Answers1

-1

If you want to get the default language from the client's browser, you can check out this question: Simplest way to detect client locale in PHP

Make sure to have an array of acceptable languages that your website supports. If the browser client's language is not in your list, you should fallback to your own default language.

If you are interested in using the Zend Framework (v1), I have written a few articles on multilingual support on my blog.

Community
  • 1
  • 1
Wouter Thielen
  • 1,016
  • 9
  • 21
  • This is not answer of my question. This is a comment. – HddnTHA Oct 10 '14 at 09:33
  • Well, I am telling you you are making it too hard for yourself with that PHP code. There are better ways, and that is what my post is about. You are right it is not an answer, but thought you'd appreciate the extra information. – Wouter Thielen Oct 10 '14 at 09:39
  • You can make it as comment. Because people visiting stackoverflow for correct answers. Anyway thank you for advise. – HddnTHA Oct 10 '14 at 09:41