Page 1 of 1

Translate text with PHP

Posted: Wed May 30, 2012 4:28 am
by PF2G
Hi,
i'm working on a website where i have to translate the text. i have to create 3 files:

lang_pt.php

Code: Select all

<?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

Code: Select all

<?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";
?>
common.php

Code: Select all

<?php
include 'lang_pt.php';
include 'lang_en.php';

$lang = array('pt', 'en');
session_start();

if(isset($_SESSION['lang']) && array($_SESSION['lang'], $lang))
{
	include 'lang_'.$_SESSION['lang'].'.php';
}
else
{
	//Default
	header ('Location: ?lang=pt');
	die();
	break;
}

echo $lang['Family Office'].'<br />';
echo $lang['Multi Family Office'].'<br />';
echo $lang['Foco'].'<br />';
echo $lang['Serviços'].'<br />';
echo $lang['Valores'].'<br />';
echo $lang['Seminários'].'<br />';
echo $lang['Contactos'].'<br />';
echo $lang['Português'].'<br />';
echo $lang['Inglês'];
?>
lang_pt and lang_en i create the array writing the translations and the common.php i include those files and call the arrays according to the language. if it's 'pt' it goes portuguese if 'en' english then the default - portuguese.

Then include the common in all the pages and do the "echo $lang['Contactos'].'<br />';".

Did you understand, i think i can be more clear.

Please help me...

Re: Translate text with PHP

Posted: Wed May 30, 2012 6:24 am
by Celauran
Why are you including the files twice? What about something like this?

Code: Select all

<?php

$lang = array('pt', 'en');
session_start();

if(isset($_SESSION['lang']) && in_array($_SESSION['lang'], $lang))
{
    include 'lang_'.$_SESSION['lang'].'.php';
}
else
{
    //Default
    include 'lang_pt.php';
}

echo $lang['Family Office'].'<br />';
echo $lang['Multi Family Office'].'<br />';
echo $lang['Foco'].'<br />';
echo $lang['Serviços'].'<br />';
echo $lang['Valores'].'<br />';
echo $lang['Seminários'].'<br />';
echo $lang['Contactos'].'<br />';
echo $lang['Português'].'<br />';
echo $lang['Inglês'];
?>
You haven't actually told us what the problem is.