[ Video Sharing CMS v4 ] automatic language change according browser

Started by krejcis,

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

krejcisTopic starter

Hi guys and Mario,
I have a question about languages concept in phpvibe. I have 2 domains (one is .com and second is .fr. ) and two languages in phpvibe english and french.

I wrote also this peace of code:
<?php
$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
    $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
// Now we simply evaluate that variable to detect specific languages
if($lc == "fr"){
    header("location: www.mywebsite.fr");
    exit();

}
?>


This Code detects user browser language and redirect to the language version of the phpvibe.
Question is where to insert this code? ???If I will insert it to index.php than I will have a loop on my site.  Should it be somewhere in template right?

Or do you know somebody some smarter way how to do that?

Thanks
Jan
  •  

krejcisTopic starter

Or maybe more clear code will it be if it will be coded to the functions..
Quote
function redirect() {
   
    // only get the first two characters
    $lang=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    switch($lang) {
        case "fr": {
            header("Location: http://www.mydomain.fr");
            break;
        }
        case "es": {
            header("Location: http://www.mydomain.es");
            break;
        }
        default: {
            header("Location: http://www.mydomain.com");
        }
    }

}
redirect(); 

Still dont know where to insert this...Thanks for help
  •  

Marius P.

I'm not 100% sure what you need.

I assume what you should do is this:

On the .com site match if lang is france and redirect to .fr
On the .fr site match if lang is not france redirect to .com

This is the language logic I would see fit, but feel free to explain what you wish exactly.

If you look at load.php it has this:

//Get current translation
if(isset($_GET["clang"])) {
$_SESSION['phpvibe-language'] = toDB($_GET["clang"]);
$trans = lang_terms(toDB($_GET["clang"]));
} else {
$trans = lang_terms();
}



If you want to try to match the translation in your own database, just try to mix this two:






//Get current translation
$browserlang=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if(isset($_GET["clang"]) ) {
$_SESSION['phpvibe-language'] = toDB($_GET["clang"]);
$trans = lang_terms(toDB($_GET["clang"]));
}elseif(!empty($browserlang)) {$trans = lang_terms($browserlang);

} else {
$trans = lang_terms();
}


Happy with my help? Buy me a coffee.
Please, always use the search before opening a new topic! We're all here on our (limited) free time! Make sure you help yourself too!
  •  

Marius P.

I've played around with match language before (think in version 2) and it did more wrong than good.
Just so you know.
For example I use english as my browser language, many people use english (even if locale is different), and if I want to see your .fr site you will always push me to .com
Happy with my help? Buy me a coffee.
Please, always use the search before opening a new topic! We're all here on our (limited) free time! Make sure you help yourself too!
  •  

krejcisTopic starter

yes the logic whcih you described was my target..I see also your second comment.. What is yor opinion to do best? Best for user and best for seo/google..

just for clarification again:
I have:
www.mydomain.com where phpvibe is installed
translation in admin of english and french
additional domain www.maindomain.fr

What I basically want is detect user browser language and then display the translation (if exists).

I thought it would be better to redirect user to additional domain www.maindomain.fr when french browser is detected than display this url http://www.maindomain.com/&clang=fr

Did you get my idea??
  •  

Marius P.

Yes.
Redirecting to set clang will create an infinite loop, my second case is best, it fallbacks to english if lang is not available.
Haven't tested it, but it would work. Not sure if the code you've provided matches the real language id or just the first two letters of the language (which is different thing in some cases, unless you also change the country code when adding a language).
Happy with my help? Buy me a coffee.
Please, always use the search before opening a new topic! We're all here on our (limited) free time! Make sure you help yourself too!
  •  

Similar topics (7)