Translate text with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PF2G
Forum Newbie
Posts: 13
Joined: Sat Sep 10, 2011 10:51 am

Translate text with PHP

Post 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...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Translate text with PHP

Post 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.
Post Reply