Hi,

I'm working on a website where i have to translate the pages and the way i have to do is a little confused. Here it goes:

I have to create 2 files lang_pt.php and lang_en.php where i have common arrays where i do the translation.

lang_pt.php:

<?PHP
    $lang['Family Office']="Family Office";
    $lang['Multi Family Office']="Multi Family Office";
    $lang['Foco']="Foco";
    $lang['Serviços']="Serviços";
    $lang['Valores']="Valores";
    $lang['Seminários']="Seminários";
    $lang['Contactos']="Contactos";
    $lang['Português']="Português";
    $lang['Inglês']="Inglês";
?>

lang_en.php:

<?PHP
    $lang['Family Office']="Family Office";
    $lang['Multi Family Office']="Multi Family Office";
    $lang['Foco']="Foco";
    $lang['Serviços']="Services";
    $lang['Valores']="Values";
    $lang['Seminários']="Seminars";
    $lang['Contactos']="Contacts";
    $lang['Português']="Portuguese";
    $lang['Inglês']="English";
?>

Then i have to include this 2 files in another, common.php. An it's here where i get confused....I have to include the files in this common.php and then i have to do a switch for the languages (i'm not sure if it's correct):

<?PHP
    $lang=$_GET['lang'];
    include 'lang_pt.php';
    include 'lang_en.php';

    echo $lang['Family Office'];
    echo $lang['Family Office'];
    echo $lang['Multi Family Office'];
    echo $lang['Foco'];
    echo $lang['Serviços'];
    echo $lang['Valores'];
    echo $lang['Seminários'];
    echo $lang['Contactos'];
    echo $lang['Português'];
    echo $lang['Inglês'];

    switch ($lang) 
    {
        case "lang_pt.php":
            header ('Location: index.php?lang=pt');
            die();
            break;
        case "lang_en.php":
            header ('Location: index.php?lang=en');
            die();
            break;
        default:
            header ('Location: index.php?lang=pt');
            die();
            break;
    }
?>

Then include the common.php in all other files so it can do the translation.

I think this is the most clear i can be. If someone can help me, i appreciate.

I'll try to explain better if you want to.

Thank You,
PF2G

Hi,

Try this... this will be your common.php and must be included in all pages needing a translation..

 <?php
session_start();
## detect browser's language
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

## set that language into session
$_SESSION['lang'] = $language;

## include translation file depending on the language in the session.
switch ($_SESSION['lang'])
{
case 'en':

include 'lang_en.php';

$user_lang = "English";

break;

case 'pt':
include 'lang_pt.php';
$language = 'Purtuguese';

break;

default:

include 'lang_pt.php';
$user_lang = 'Purtuguese';

break;

}

## below can be anywhere, but make sure session is included just like above.
echo $user_lang;

echo "<br/>Translated Strings<br/>";

echo $lang['Family Office'];
echo $lang['Family Office'];
echo $lang['Multi Family Office'];
echo $lang['Foco'];
echo $lang['Serviços'];
echo $lang['Valores'];
echo $lang['Seminários'];
echo $lang['Contactos'];
echo $lang['Português'];
echo $lang['Inglês'];


?>

If you don't want the browser's auto-detection, remove it and use $_GET instead.. don't forget to unset session as necessary..

Member Avatar for diafol

Here's some old code of mine that I've refactored quickly (beware - there may be errors and a bit of duplication):

//=========LANGUAGE GET-SET
//session_start() set earlier

$allowed_langs = array('en','pt');
$default_lang = 'en';

if(isset($_GET['lang']) && in_array($_GET['lang'],$allowed_langs)) {
    $sitelang = $_GET['lang'];
    $_SESSION['lang'] = $sitelang;
    $setcookie = true;  
}else{
    if(isset($_SESSION['lang']) && in_array($_SESSION['lang'],$allowed_langs)){
        $sitelang = $_SESSION['lang'];
    }else{
        if(isset($_COOKIE['lang']) && in_array($_COOKIE['lang'],$allowed_langs)) {
            $sitelang = $_COOKIE['lang'];
        }else{
            $sitelang = $default_lang;
            $setcookie = true;
        }
        $_SESSION['lang'] = $sitelang;
    }
}

//=========WRITE LANG COOKIE
if($setcookie)setcookie('lang', $sitelang,  ...expiry... , "/", false);

//=========INCLUDE CORRECT LANGUAGE FILE
include("lang/$sitelang/lang.php");

//=========MAKE LANGUAGE BUTTON
if($sitelang == 'en'){
    $changelang = 'pt';
    $changetext = "Português";
    $changetitle = "Alterar a página para o Português";     
}else{
    $changelang = 'en';
    $changetext = "English";
    $changetitle = "Change the page to English";
}
$langnav = "<a href=\"/?lang=$changelang\" title=\"$changetitle\">$changetext</a>";

Making the switch language link is easy if you only have the 2 languages, otherwise a different routine is required to produce the various links.

So, in summary;

  1. check for url language parameter
  2. check for active session language
  3. check for cookie
  4. apply default language if nothing set in 1,2,3.

THis code was originally in a few classes so I've had to cobble it together into some simple procedural stuff. Apologies for the roughness.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.