The Gettext extension may not be available on the hosting service. It won’t help you much with translating Urls or BD records. Therefore, my suggestion is that you work with a more complete translation system, which can be deployed on any PHP site without dependency on the Gettext extension. This solution involves 3 parts:
Translation of static texts
It involves translating the fixed texts of your website, which are encoded directly in HTML (i.e., those texts that are not retrieved from a database). For this text create translation files and a function that maps it to you. Record the user’s language somewhere (session or cookie) so that you can know the user’s preference and which mapping to use:
en_us.php
<?php
return array(
'A Empresa'=>'The Company',
'Contato'=> 'Contact',
...
);
Translator.php
<?php
function _($text)
{
session_start();
// Exemplo recuperando o idioma da sessao.
$lang = isset($_SESSION['lang'])?$_SESSION['lang']:'';
if (!empty($lang))
{
// Carrega o array de mapeamento de textos.
$mapping = require_once $lang.'.php';
return isset($mapping[$text])?$mapping[$text]:$text;
}
return $text;
}
example-of-use.php
<?php
require_once 'translator.php';
session_start();
// Apenas para teste. O idioma deve ser
// escolhido pelo usuário.
$_SESSION['lang'] = 'en_us';
echo _('Contato');
Translation of dynamic texts (from the database)
Create an additional column in each table, which specifies the language of the record.
For example, a news table could be:
id titulo texto data **idioma_id**
----------------------------------------------------------------------------
1 Noticia de teste Apenas uma noticia 2014-05-16 1
2 Test new Just a new 2014-05-16 2
When consulting the records, bring only those of the desired language:
SELECT * FROM noticia WHERE idioma_id = 2;
Translation of Urls
It involves the translation of the Urls of the site. For this to work it is necessary to use a single entry script for your site. Through a file . htaccess you can do this by redirecting any access to an index.php file. After redirecting, you can use a mapping of Urls, as well as done to static texts:
.htaccess
RewriteEngine on
# Nao aplica o redirecionamento caso
# o que esteja sendo acessado seja um arquivo ou pasta.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Redireciona para o arquivo index.php
RewriteRule . index.php
en_us_routes.php
<?php
return array(
'the-company'=>'a_empresa.php',
'contact'=> 'contato.php',
...
);
index php.
// Remove da URL a pasta da aplicacao,
// deixando apenas os parametros.
$aux = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
$parameters = str_replace($aux, '', $_SERVER['REQUEST_URI']);
// Recupera as partes da URL.
// Se você acessar http://meusite.com.br/empresa
// $urlParts será:
// array('empresa')
//
// Se você acessar http://meusite.com.br/contato
// $urlParts será:
// array('contato')
$urlParts = explode('/', $parameters);
// Para simplificar, aqui trata uma URL com
// apenas uma parte. Mas você pode generalizar para urls
// com suburls também (Ex: "empresa/onde-estamos").
$url = $urlParts[0];
// Apenas para teste. O idioma pode estar
// associado ao perfil do usuário e ser setado
// na sessão no login.
$_SESSION['lang'] = 'en_us';
// Carrega o array de mapeamento de URLs.
$mappingFileName = $_SESSION['lang'].'_routes.php';
$mapping = require_once $mappingFileName ;
if (isset($mapping[$url]))
{
require_once $mapping[$url];
}
else
{
require_once 'erro_404.php';
}
The codes cited are just examples to pass the idea (have not been tested).
Take a look at this site, it might help: http://www.devin.com.br/php-gettext/
– abfurlan
If you are in the habit of reading Ocs in English, here is another tutorial as well. http://www.onlamp.com/pub/a/php/2002/06/13/php.html
– romulos
Thank you, @abfurlan, that’s what I needed!
– digofreitas
It might be interesting to think of an alternative way. Translating the site, I imagine, is to have a site in more than one language, right? So I strongly recommend using a CMS, which already brings this ready for you. I know your site is small, but even so, a good CMS can greatly reduce your work. The best candidates are Wordpress and Joomla. Note: I have a site in Joomla, with parts made in PHP code.
– RSLyra
@digofreitas wonder if you could give me some lights/tutos for beginners to create . po files and how to use gettext? Or some tool? I’m completely in the dark.
– Jorge B.
@Jorge-b just go on the link that romulos recommended, which explains very well
– digofreitas
Thanks @digofreitas but I already got there alone ;)
– Jorge B.