Author Topic: Load site in correct language uppon browser settings  (Read 17406 times)

0 Members and 1 Guest are viewing this topic.

Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Load site in correct language uppon browser settings
« on: October 06, 2005, 11:24:43 AM »
Hi!

My second question today  :D

I would like to suggest a mod that loads the correct language according to the browsers language.

Example:

If a user visits http://www.domain.com/

and his browser settings are german he should be directed to

http://www.domain.com/?l=deutsch

if english than

http://www.domain.com/?l=english

if any other language that is not installed:

http://www.domain.com/l=?english (so the standard language)

How this will be possible?

lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Re: Load site in correct language uppon browser settings
« Reply #1 on: October 06, 2005, 08:50:47 PM »
I have tried to use code that I use in smarty project (where it works fine).

Code: [Select]
<?php

if (empty($_GET['l'])) {
if(strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-at") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-ch") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-li") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-lu")) {
Header("Location: ./index.php?l=deutsch");
}
elseif(strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"en") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"en-us")) {
Header("Location: ./index.php?l=english");
}
else {
Header("Location: ./index.php?l=english");
}
}

?>


I have tried to insert in on top of index.php and page_header.php but I get only lot of errors like:

Warning: Cannot modify header information - headers already sent by (output started at /****/index.php:2) in /****/index.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /****/index.php:2) in /****/includes/sessions.php on line 84

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /****/index.php:2) in /****/includes/sessions.php on line 84

Warning: Cannot modify header information - headers already sent by (output started at /****/index.php:2) in /****/includes/sessions.php on line 92

Warning: Cannot modify header information - headers already sent by (output started at /****/index.php:2) in /****/includes/sessions.php on line 92


I think to code is allright, just the place where I tried to include it is wrong. If somebody could point me where it would need to be included, I would be happy.

Greetings,
lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Load site in correct language uppon browser settings
« Reply #2 on: October 07, 2005, 12:47:28 AM »
1) if you include a code in a .php file, do not use additional <?php and ?> if they are already present there.
2) try insert that code into global.php, because that file being executed with any pages. You should added above:
Code: [Select]
$cat_cache = array();3) ones its done, make sure you didnt leave any spaces or newlines before <?php and after ?> otherwise you'll get the same error messages.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Re: Load site in correct language uppon browser settings
« Reply #3 on: October 07, 2005, 10:07:41 AM »
thanks, that's the correct place to include...

greetings,
lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline Matthias70

  • Full Member
  • ***
  • Posts: 199
    • View Profile
    • Bildergalerie
Re: Load site in correct language uppon browser settings
« Reply #4 on: October 07, 2005, 02:00:32 PM »
Hi lapinkulta,
did you find a solution for your request?
Im very interested.

Matthias

Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Re: Load site in correct language uppon browser settings
« Reply #5 on: October 07, 2005, 03:17:43 PM »
Hi Matthias!

If you place my code where Vano said, it works quite fine.

Only problem, but nothing to to with this code, is that for the standard language no l=english string is attached.
See: http://www.4homepages.de/forum/index.php?topic=6729.120

I'm waiting for a solution on that...

Greetings,

lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline Matthias70

  • Full Member
  • ***
  • Posts: 199
    • View Profile
    • Bildergalerie
Re: Load site in correct language uppon browser settings
« Reply #6 on: October 07, 2005, 05:01:51 PM »
Hi lapinkulta,
thanks a lot.
Works fine.
Only my cookie for the last language settings is not working anymore.
But I think with your mod it is useless anyway.

Matthias

Offline Matthias70

  • Full Member
  • ***
  • Posts: 199
    • View Profile
    • Bildergalerie
Re: Load site in correct language uppon browser settings
« Reply #7 on: October 07, 2005, 05:20:12 PM »
Ups I was a bit to fast.
Mod works fine but my default-links
like: .../bildergalerie/categories.php?cat_id=35
are now all redirected to index.php.  :(

Only links which include the language settings are working
like: .../bildergalerie/details.php?image_id=313&l=deutsch

Do you have any solutions for that

Matthias

Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Re: Load site in correct language uppon browser settings
« Reply #8 on: October 07, 2005, 08:58:52 PM »
That the problem I addressed in my last posting. What I have done is a small change in the sessions.php in order that even for the standard language the "l=language" is attached.

find
Code: [Select]
    if (!empty($l)) {
      $url .= strpos($url, '?') !== false ? $amp : "?";
      $url .= "l=".$l;
    }

and replace with:

Code: [Select]
   if (!empty($l)) {
      $url .= strpos($url, '?') ? '&' : '?';
      $url .= "l=".$l;
   }
   if (empty($l)) {
      $url .= strpos($url, '?') ? '&' : '?';
      $url .= "l=english";
   }

be sure to change "english" to whatever is your standard language.

works fine with me.

greetings,
lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline Matthias70

  • Full Member
  • ***
  • Posts: 199
    • View Profile
    • Bildergalerie
Re: Load site in correct language uppon browser settings
« Reply #9 on: October 07, 2005, 11:11:01 PM »
Mmh, is there no solution that keeps the default links
I tried it with this code. Just without the language extension "?l=deutsch" in the fifth line, but it's not working  :cry:

Code: [Select]
if (empty($_GET['l'])) {
if(strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-at") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-ch") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-li") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"de-lu")) {
Header("Location: ./index.php");
}
elseif(strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"en") || strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"],"en-us")) {
Header("Location: ./index.php?l=english");
}
else {
Header("Location: ./index.php?l=english");
}
}


Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Re: Load site in correct language uppon browser settings
« Reply #10 on: October 07, 2005, 11:45:37 PM »
Not with this code as

Code: [Select]
if (empty($_GET['l'])) { ....

in the first row checks if there is a language specified in the URL. If not, it will always choose the language based on browser setting.

greetings,
lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Load site in correct language uppon browser settings
« Reply #11 on: October 08, 2005, 08:39:47 AM »
Code: [Select]
if ((!isset($HTTP_GET_VARS['l']) || empty($HTTP_GET_VARS['l'])) && (!isset($HTTP_POST_VARS) || empty($HTTP_POST_VARS))) {
  $lang_array = array(
      "de" => "deutsch",
      "de-at" => "deutsch",
      "de-ch" => "deutsch",
      "de-li" => "deutsch",
      "de-lu" => "deutsch",
      "en" => "english",
      "en-us" => "english",
  );
  foreach($lang_array as $key => $val)
  {
    if (strstr($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"],$key))
    {
      $l = $val;
      break;
    }
  }
  $l = ($l) ? $l : "english";
  Header("Location: ".$HTTP_SERVER_VARS['REQUEST_URI'].(strstr($HTTP_SERVER_VARS['REQUEST_URI'], "?") ? "&" : "?")."l=".$l);
  exit;
}
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline lapinkulta

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Avatar Essentials
Re: Load site in correct language uppon browser settings
« Reply #12 on: October 08, 2005, 09:37:43 AM »
Hi Vano!

With this code I can not access the admin area anymore...

Greetings,
lapinkulta
Two hours of try and error can save ten minutes of manual reading.

Offline Matthias70

  • Full Member
  • ***
  • Posts: 199
    • View Profile
    • Bildergalerie
Re: Load site in correct language uppon browser settings
« Reply #13 on: October 08, 2005, 11:51:06 AM »
Hi V@no and lapinkulta
the code works fine for me. No admin problems and at the moment no other problems with the language settings  :D

Thanks a lot
Matthias

Offline Matthias70

  • Full Member
  • ***
  • Posts: 199
    • View Profile
    • Bildergalerie
Re: Load site in correct language uppon browser settings
« Reply #14 on: October 08, 2005, 04:15:12 PM »
Hi V@no,
Perhaps it is the wrong place for this post.
But after the code changes above the code for my "new multilang images outside 4images" is not working anymore

Here the code. Perhabs there is a conflict with the changed global.php
Code: [Select]
<?php
// PATH to your 4images Gallery / PFAD zu Ihrer 4images Gallerie
define('SCRIPT_URL''http://www.*****/bildergalerie/');
define('ROOT_PATH''/*****/bildergalerie/');
include(
ROOT_PATH.'config.php');
include(
ROOT_PATH.'includes/db_mysql.php');
include(
ROOT_PATH.'includes/constants.php');
$site_db = new Db($db_host$db_user$db_password$db_name);
function 
is_remote($file_name) {
  return (
preg_match('#^https?\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i'$file_name)) ? 0;
}
$sql "SELECT a.image_id, a.cat_id, a.image_name, a.image_active, a.image_thumb_file, a.image_hits
        FROM "
.IMAGES_TABLE." a, ".CATEGORIES_TABLE." b
        WHERE a.image_active=1
        AND a.cat_id = b.cat_id
        AND b.auth_viewcat="
.AUTH_ALL."
        AND b.auth_viewimage="
.AUTH_ALL."
        ORDER BY image_date DESC
        LIMIT 3"
;
$result $site_db->query($sql);
while (
$row $site_db->fetch_array($result)) {
$image_id $row['image_id'];
$cat_id $row['cat_id'];
$image_name $row['image_name'];
$image_hits $row['image_hits'];
$thumb_src = (is_remote($row['image_thumb_file'])) ? $row['image_thumb_file'] : SCRIPT_URL.THUMB_DIR."/".$cat_id."/".$row['image_thumb_file'];
echo 
"<a href=\"".SCRIPT_URL."details.php?image_id=$image_id\" target=_parent><img src=\"".$thumb_src."\" border=\"0\"></a>\n";
echo 
"<br><b>$image_name</b><br>\n";
echo 
"Hits: $image_hits<br><br>\n";
}
?>