dimanche 26 juin 2016

PHP multi language support website

I'm working on a website, where I want to add multi language support. So far my translation works for the index page and every other page in the main directory.

My structure looks like this:
/index.php
/aboutus.php
/products/viewproduct.php
and so on.

The index and about us sites are working, but on the product page the default language gets called.

small sample header.php

<?php if ($folder=="yes") {
    require("../lang-config.php");
} else {
    require("lang-config.php");
}


?>
<html dir="ltr" lang="<?php echo $language; ?>">
<head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="<?php lang("metaName"); ?>" content="<?php lang("metaNameContent"); ?>">

small sample viewproduct.php

<?php
    $thisPage="";
    $folder="yes";
    include("../templates/fixed/header.php");

    $productsID = $db->prepare('SELECT productID, productTitle, productColor, productCont, productDate, name, tags, productpic1, productpic2, productpic3 FROM products WHERE productID = :productID');
    $productsID->execute(array(':productID' => $_GET['id']));
    $row = $productsID->fetch();

    //if post does not exists redirect user.
    if($row['productID'] == ''){
        header('Location: ../');
        exit;
    } 
?>

    <!-- Page Title
    ============================================= -->
    <section id="page-title">

On the pages index and about us I got the variable $folder="" and on the viewproduct I got $folder="yes".

Another thing is that I needed to translate the products, so I created another Database with a different login and created a second config for this. The configs are called config.php and config-de.php.

So when the language de is detected it should switch the config to config-de.php. Like I tried to do so in the following code sample.

Part of the lang-config.php:

// Include the right language file  
include($lang_folder."/".$language.".php");

// Helper function to echo the values of the $lang array
function lang($key)
{
    global $lang;
    echo $lang[$key];
}

function language_navigation()
{
    global $lang;
    $languages = $lang["available_language"];
    foreach( $languages as $single_language )
    {
        echo ' | <a href="' . $_SERVER['PHP_SELF'] . '?lang=' . key($languages) . '">'. $single_language . '</a>';
        next($languages);
    }
}

if ($language=="de"){
    if ($folder=="yes") {
        include("../includes/config-de.php");
    } else {
        include("includes/config-de.php");
    }
}else{
    if ($folder=="yes") {
        include("../includes/config.php");
    } else {
        include("includes/config.php");
    }
}

I'm german, so I thought, even when I open the view product page I should see everything in german, because my browser language is in german, but the database call looks like it's calling the english one.

When you know a better way to implement multi language support, I'm open to any new idea, too.

A small part, without the language implementation is already online on http://doggo.de/english/ and not finalized yet.

Thanks for any help.

Edit:

I can't figure out why the english version is called on viewproduct.php, when the page is called, it should run the same config files as the index.php. On every page I call, there is an include("../templates/fixed/header.php"); where the lang-config.php is called, but it doesn't seem to work. How can I fix this?

Aucun commentaire:

Enregistrer un commentaire