2003-05-02 12:33:06 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
/* ***************************************************************************
|
2005-01-02 17:57:37 +01:00
|
|
|
|
* Copyright (C) 2001 Eric Seigne <erics@rycks.com>
|
2009-02-14 01:36:49 +01:00
|
|
|
|
* Copyright (C) 2004-2009 Destailleur Laurent <eldy@users.sourceforge.net>
|
2003-05-02 12:33:06 +02:00
|
|
|
|
*
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
|
* any later version.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
* ************************************************************************* */
|
|
|
|
|
|
|
2005-01-02 17:57:37 +01:00
|
|
|
|
/**
|
2008-08-07 08:33:35 +02:00
|
|
|
|
* \file htdocs/translate.class.php
|
2008-10-09 19:29:32 +02:00
|
|
|
|
* \brief File for Tanslate class
|
2008-08-07 08:33:35 +02:00
|
|
|
|
* \author Eric Seigne
|
|
|
|
|
|
* \author Laurent Destailleur
|
|
|
|
|
|
* \version $Id$
|
|
|
|
|
|
*/
|
2004-08-15 20:09:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
2008-01-21 03:19:25 +01:00
|
|
|
|
/**
|
2008-08-07 08:33:35 +02:00
|
|
|
|
* \class Translate
|
|
|
|
|
|
* \brief Class to manage translations
|
|
|
|
|
|
*/
|
2004-08-15 20:09:30 +02:00
|
|
|
|
class Translate {
|
2004-07-25 19:43:23 +02:00
|
|
|
|
|
2008-12-08 01:01:00 +01:00
|
|
|
|
var $dir; // Directories that contains /langs subdirectory
|
2008-03-31 17:17:39 +02:00
|
|
|
|
|
2006-08-07 04:20:57 +02:00
|
|
|
|
var $defaultlang; // Langue courante en vigueur de l'utilisateur
|
2005-02-06 19:41:45 +01:00
|
|
|
|
|
2008-01-21 03:19:25 +01:00
|
|
|
|
var $tab_loaded=array(); // Tableau pour signaler les fichiers deja charges
|
2006-05-20 11:19:20 +02:00
|
|
|
|
var $tab_translate=array(); // Tableau des traductions
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-03-01 02:12:55 +01:00
|
|
|
|
var $cache_labels=array(); // Cache for labels
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2009-01-19 00:42:26 +01:00
|
|
|
|
var $charset_inputfile=array(); // To store charset encoding used for language
|
2009-02-14 01:36:49 +01:00
|
|
|
|
var $charset_output='UTF-8'; // Codage used by "trans" method outputs
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2003-05-02 12:33:06 +02:00
|
|
|
|
|
2004-08-15 20:09:30 +02:00
|
|
|
|
/**
|
2005-01-02 17:57:37 +01:00
|
|
|
|
* \brief Constructeur de la classe
|
2008-12-08 01:01:00 +01:00
|
|
|
|
* \param dir Force directory that contains /langs subdirectory
|
2007-05-25 22:02:23 +02:00
|
|
|
|
* \param conf Objet qui contient la config Dolibarr
|
2004-10-20 23:15:17 +02:00
|
|
|
|
*/
|
2007-05-25 22:02:23 +02:00
|
|
|
|
function Translate($dir = "",$conf)
|
2005-10-30 01:42:54 +02:00
|
|
|
|
{
|
2008-10-09 19:29:32 +02:00
|
|
|
|
// If charset output is forced
|
2009-01-15 23:49:06 +01:00
|
|
|
|
if (! empty($conf->character_set_client))
|
2007-05-25 22:02:23 +02:00
|
|
|
|
{
|
|
|
|
|
|
$this->charset_output=$conf->character_set_client;
|
|
|
|
|
|
}
|
2008-12-07 20:19:32 +01:00
|
|
|
|
if ($dir) $this->dir=array($dir);
|
|
|
|
|
|
else $this->dir=$conf->dol_document_root;
|
2005-10-30 02:07:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-05-20 11:19:20 +02:00
|
|
|
|
/**
|
2008-06-30 21:34:53 +02:00
|
|
|
|
* \brief Return string translated for a key
|
|
|
|
|
|
* Translation array must have been loaded before.
|
|
|
|
|
|
* \param key Key to translate
|
|
|
|
|
|
* \return string Translated string
|
2006-05-20 11:19:20 +02:00
|
|
|
|
*/
|
|
|
|
|
|
function getTransFromTab($key)
|
|
|
|
|
|
{
|
2008-07-28 11:09:03 +02:00
|
|
|
|
if (! empty($this->tab_translate[$key]))
|
2006-05-20 11:19:20 +02:00
|
|
|
|
{
|
|
|
|
|
|
return $this->tab_translate[$key];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-01-21 03:19:25 +01:00
|
|
|
|
* \brief Positionne la chaine traduite pour une cl<EFBFBD> donn<EFBFBD>e.
|
2008-06-30 21:34:53 +02:00
|
|
|
|
* \param key Key to translate
|
|
|
|
|
|
* \return string Translated string
|
2006-05-20 11:19:20 +02:00
|
|
|
|
*/
|
|
|
|
|
|
function setTransFromTab($key,$value)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->tab_translate[$key]=$value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-30 02:07:00 +01:00
|
|
|
|
/**
|
2008-07-15 20:09:22 +02:00
|
|
|
|
* \brief Set accessor for this->defaultlang
|
2008-06-30 21:34:53 +02:00
|
|
|
|
* \param srclang Language to use
|
2005-10-30 02:07:00 +01:00
|
|
|
|
*/
|
2006-08-07 04:20:57 +02:00
|
|
|
|
function setDefaultLang($srclang='fr_FR')
|
2005-10-30 02:07:00 +01:00
|
|
|
|
{
|
2009-02-20 21:28:16 +01:00
|
|
|
|
//dol_syslog("Translate::setDefaultLang ".$this->defaultlang,LOG_DEBUG);
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-07-15 20:09:22 +02:00
|
|
|
|
$this->origlang=$srclang;
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-08 01:01:00 +01:00
|
|
|
|
if (empty($srclang) || $srclang == 'auto')
|
2005-10-30 01:42:54 +02:00
|
|
|
|
{
|
2005-10-30 02:07:00 +01:00
|
|
|
|
$langpref=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
|
|
|
|
|
|
$langpref=eregi_replace(";[^,]*","",$langpref);
|
|
|
|
|
|
$langpref=eregi_replace("-","_",$langpref);
|
2005-10-30 02:52:48 +01:00
|
|
|
|
|
|
|
|
|
|
$langlist=split("[;,]",$langpref);
|
|
|
|
|
|
|
|
|
|
|
|
$langpart=split("_",$langlist[0]);
|
2009-01-02 23:02:52 +01:00
|
|
|
|
//print $langpart[0].'/'.$langpart[1];
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2006-08-07 04:20:57 +02:00
|
|
|
|
if (isset($langpart[1])) $srclang=strtolower($langpart[0])."_".strtoupper($langpart[1]);
|
2009-01-02 23:02:52 +01:00
|
|
|
|
else {
|
|
|
|
|
|
// Array to convert short lang code into long code.
|
|
|
|
|
|
$longforshort=array('ca'=>'ca_ES');
|
|
|
|
|
|
if (isset($longforshort[strtolower($langpart[0])])) $srclang=$longforshort[strtolower($langpart[0])];
|
|
|
|
|
|
else $srclang=strtolower($langpart[0])."_".strtoupper($langpart[0]);
|
|
|
|
|
|
}
|
2005-10-30 01:42:54 +02:00
|
|
|
|
}
|
2005-10-30 02:07:00 +01:00
|
|
|
|
|
2006-08-07 04:20:57 +02:00
|
|
|
|
$this->defaultlang=$srclang;
|
2004-07-13 23:25:53 +02:00
|
|
|
|
}
|
2003-05-02 12:33:06 +02:00
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2005-11-01 00:49:35 +01:00
|
|
|
|
/**
|
2008-08-07 08:33:35 +02:00
|
|
|
|
* \brief Return active language code for current user
|
|
|
|
|
|
* \remarks Accessor for this->defaultlang
|
|
|
|
|
|
* \return string Language code used (en_US, en_AU, fr_FR, ...)
|
2005-11-01 00:49:35 +01:00
|
|
|
|
*/
|
|
|
|
|
|
function getDefaultLang()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->defaultlang;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2005-11-01 00:49:35 +01:00
|
|
|
|
/**
|
|
|
|
|
|
\brief Positionne environnement PHP en fonction du langage
|
2008-07-15 20:09:22 +02:00
|
|
|
|
\remarks Le code langue long (fr_FR, en_US, ...) doit avoir etre positionne par setDefaultLang
|
2005-11-01 00:49:35 +01:00
|
|
|
|
\return int >0 si ok, <0 so ko
|
2009-02-20 13:29:55 +01:00
|
|
|
|
\deprecated
|
2005-11-01 00:49:35 +01:00
|
|
|
|
*/
|
|
|
|
|
|
function setPhpLang()
|
|
|
|
|
|
{
|
2009-02-20 21:28:16 +01:00
|
|
|
|
//dol_syslog("Translate::setPhpLang ".$this->defaultlang,LOG_DEBUG);
|
2009-02-20 13:29:55 +01:00
|
|
|
|
return;
|
|
|
|
|
|
/*
|
2005-11-01 00:49:35 +01:00
|
|
|
|
$code_lang_tiret=ereg_replace('_','-',$this->defaultlang);
|
2009-01-02 23:02:52 +01:00
|
|
|
|
//print 'code_lang_tiret='.$code_lang_tiret;
|
|
|
|
|
|
setlocale(LC_ALL, $this->defaultlang); // Some OS (Windows) need local with _
|
|
|
|
|
|
setlocale(LC_ALL, $code_lang_tiret); // Other OS need local with -
|
2008-10-20 03:05:41 +02:00
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
if (defined("MAIN_FORCE_SETLOCALE_LC_ALL") && MAIN_FORCE_SETLOCALE_LC_ALL)
|
2008-10-20 03:05:41 +02:00
|
|
|
|
$res_lc_all=setlocale(LC_ALL, MAIN_FORCE_SETLOCALE_LC_ALL.'.UTF-8', MAIN_FORCE_SETLOCALE_LC_ALL);
|
|
|
|
|
|
if (defined("MAIN_FORCE_SETLOCALE_LC_NUMERIC") && MAIN_FORCE_SETLOCALE_LC_NUMERIC)
|
|
|
|
|
|
$res_lc_numeric=setlocale(LC_NUMERIC, MAIN_FORCE_SETLOCALE_LC_NUMERIC.'.UTF-8', MAIN_FORCE_SETLOCALE_LC_NUMERIC);
|
|
|
|
|
|
if (defined("MAIN_FORCE_SETLOCALE_LC_MONETARY") && MAIN_FORCE_SETLOCALE_LC_MONETARY)
|
|
|
|
|
|
$res_lc_monetary=setlocale(LC_MONETARY, MAIN_FORCE_SETLOCALE_LC_MONETARY.'UTF-8', MAIN_FORCE_SETLOCALE_LC_MONETARY);
|
|
|
|
|
|
//print 'x'.$res_lc_all;
|
2005-11-01 00:49:35 +01:00
|
|
|
|
return 1;
|
2009-02-20 13:29:55 +01:00
|
|
|
|
*/
|
|
|
|
|
|
}
|
2005-11-01 00:49:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2005-01-02 17:57:37 +01:00
|
|
|
|
/**
|
2008-10-17 02:24:44 +02:00
|
|
|
|
* \brief Load in a memory array, translation key-value for a particular file.
|
|
|
|
|
|
* If data for file already loaded, do nothing.
|
2009-01-30 23:18:07 +01:00
|
|
|
|
* All data in translation array are stored in UTF-8 format.
|
2008-10-26 16:55:50 +01:00
|
|
|
|
* \param domain File name to load (.lang file). Use @ before value if domain is in a module directory.
|
2008-10-17 02:24:44 +02:00
|
|
|
|
* \param alt Use alternate file even if file in target language is found
|
2008-03-31 17:17:39 +02:00
|
|
|
|
* \return int <0 if KO, >0 if OK
|
2009-01-15 23:49:06 +01:00
|
|
|
|
* \remarks tab_loaded is completed with $domain key.
|
2008-03-31 17:17:39 +02:00
|
|
|
|
* Value for key is: 1:Loaded from disk, 2:Not found, 3:Loaded from cache
|
|
|
|
|
|
*/
|
2005-02-06 19:41:45 +01:00
|
|
|
|
function Load($domain,$alt=0)
|
|
|
|
|
|
{
|
2009-02-20 21:28:16 +01:00
|
|
|
|
//dol_syslog("Translate::Load domain=".$domain." alt=".$alt);
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-02-19 22:04:23 +01:00
|
|
|
|
// Check parameters
|
|
|
|
|
|
if (empty($domain))
|
|
|
|
|
|
{
|
2009-02-20 21:28:16 +01:00
|
|
|
|
dol_print_error('',"Translate::Load ErrorWrongParameters");
|
2009-02-02 19:33:44 +01:00
|
|
|
|
exit;
|
2008-02-19 22:04:23 +01:00
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-02-19 22:04:23 +01:00
|
|
|
|
// Check cache
|
|
|
|
|
|
if (! empty($this->tab_loaded[$domain])) { return; } // Le fichier de ce domaine est deja charge
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
foreach($this->dir as $searchdir)
|
|
|
|
|
|
{
|
2008-12-07 21:54:08 +01:00
|
|
|
|
$newalt=$alt;
|
|
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
// If $domain is @xxx instead of xxx then we look for module lang file htdocs/xxx/langs/code_CODE/xxx.lang
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// instead of global lang file htdocs/langs/code_CODE/xxx.lang
|
|
|
|
|
|
if (eregi('@',$domain)) // It's a language file of a module, we look in dir of this module.
|
2009-01-15 23:49:06 +01:00
|
|
|
|
{
|
2008-12-07 20:19:32 +01:00
|
|
|
|
$domain=eregi_replace('@','',$domain);
|
|
|
|
|
|
$searchdir=$searchdir ."/".$domain."/langs";
|
|
|
|
|
|
}
|
|
|
|
|
|
else $searchdir=$searchdir."/langs";
|
2009-01-02 23:02:52 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// Directory of translation files
|
|
|
|
|
|
$scandir = $searchdir."/".$this->defaultlang;
|
2008-03-31 17:17:39 +02:00
|
|
|
|
$file_lang = $scandir . "/".$domain.".lang";
|
|
|
|
|
|
$filelangexists=is_file($file_lang);
|
2009-01-02 23:02:52 +01:00
|
|
|
|
//print 'Load default_lang='.$this->defaultlang.' alt='.$alt.' newalt='.$newalt.' '.$file_lang."-".$filelangexists.'<br>';
|
|
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// Check in "always available" alternate file if not found or if asked
|
2008-12-07 21:54:08 +01:00
|
|
|
|
if ($newalt || ! $filelangexists)
|
2008-12-07 20:19:32 +01:00
|
|
|
|
{
|
|
|
|
|
|
// Dir of always available alternate file (en_US or fr_FR)
|
|
|
|
|
|
if ($this->defaultlang == "en_US") $scandiralt = $searchdir."/fr_FR";
|
|
|
|
|
|
elseif (eregi('^fr',$this->defaultlang) && $this->defaultlang != 'fr_FR') $scandiralt = $searchdir."/fr_FR";
|
|
|
|
|
|
elseif (eregi('^en',$this->defaultlang) && $this->defaultlang != 'en_US') $scandiralt = $searchdir."/en_US";
|
2009-02-20 13:38:58 +01:00
|
|
|
|
elseif (eregi('^es',$this->defaultlang) && $this->defaultlang != 'es_ES') $scandiralt = $searchdir."/es_ES";
|
2008-12-07 20:19:32 +01:00
|
|
|
|
else $scandiralt = $searchdir."/en_US";
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
$file_lang = $scandiralt . "/".$domain.".lang";
|
|
|
|
|
|
$filelangexists=is_file($file_lang);
|
2008-12-07 21:54:08 +01:00
|
|
|
|
$newalt=1;
|
2008-12-07 20:19:32 +01:00
|
|
|
|
}
|
2009-01-02 23:02:52 +01:00
|
|
|
|
//print 'Load alt='.$alt.' newalt='.$newalt.' '.$file_lang."-".$filelangexists.'<br>';
|
|
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
if ($filelangexists)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Enable cache of lang file in session (faster but need more memory)
|
|
|
|
|
|
// Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file)
|
|
|
|
|
|
$enablelangcacheinsession=false;
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
if ($enablelangcacheinsession && isset($_SESSION['lang_'.$domain]))
|
2008-01-20 17:05:50 +01:00
|
|
|
|
{
|
2008-12-07 20:19:32 +01:00
|
|
|
|
foreach($_SESSION['lang_'.$domain] as $key => $value)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->tab_translate[$key]=$value;
|
|
|
|
|
|
$this->tab_loaded[$domain]=3; // Marque ce fichier comme charge depuis cache session
|
|
|
|
|
|
}
|
2008-01-20 17:05:50 +01:00
|
|
|
|
}
|
2008-12-07 20:19:32 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2008-12-07 21:54:08 +01:00
|
|
|
|
if ($fp = @fopen($file_lang,"rt"))
|
2008-12-07 20:19:32 +01:00
|
|
|
|
{
|
2008-12-07 21:54:08 +01:00
|
|
|
|
if ($enablelangcacheinsession) $tabtranslatedomain=array(); // To save lang in session
|
2008-12-07 20:19:32 +01:00
|
|
|
|
$finded = 0;
|
|
|
|
|
|
while (($ligne = fgets($fp,4096)) && ($finded == 0))
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($ligne[0] != "\n" && $ligne[0] != " " && $ligne[0] != "#")
|
|
|
|
|
|
{
|
|
|
|
|
|
$tab=split('=',$ligne,2);
|
2009-02-21 00:15:36 +01:00
|
|
|
|
$key=trim($tab[0]);
|
2008-12-07 20:19:32 +01:00
|
|
|
|
//print "Domain=$domain, found a string for $tab[0] with value $tab[1]<br>";
|
|
|
|
|
|
//if (! $this->getTransFromTab($key))
|
|
|
|
|
|
if (empty($this->tab_translate[$key]) && isset($tab[1]))
|
|
|
|
|
|
{
|
|
|
|
|
|
$value=trim(ereg_replace('\\\n',"\n",$tab[1]));
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
if (eregi('^CHARSET$',$key))
|
2008-10-20 04:18:54 +02:00
|
|
|
|
{
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// On est tombe sur une balise qui declare le format du fichier lu
|
2009-01-19 00:42:26 +01:00
|
|
|
|
$this->charset_inputfile[$domain]=strtoupper($value);
|
|
|
|
|
|
//print 'File '.$file_lang.' is declared to have format '.$this->charset_inputfile[$domain].'<br>';
|
2008-10-20 04:18:54 +02:00
|
|
|
|
}
|
2008-12-07 20:19:32 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// On stocke toujours dans le tableau Tab en UTF-8
|
2009-01-19 00:42:26 +01:00
|
|
|
|
//if (empty($this->charset_inputfile[$domain]) || $this->charset_inputfile[$domain] == 'UTF-8') $value=utf8_decode($value);
|
|
|
|
|
|
if (empty($this->charset_inputfile[$domain]) || $this->charset_inputfile[$domain] == 'ISO-8859-1') $value=utf8_encode($value);
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// We do not load Separator values for alternate files
|
2008-12-07 21:54:08 +01:00
|
|
|
|
if (! $newalt || (! eregi('^Separator',$key)))
|
2008-12-07 20:19:32 +01:00
|
|
|
|
{
|
2009-01-02 23:02:52 +01:00
|
|
|
|
//print 'XX'.$key;
|
2008-12-07 20:19:32 +01:00
|
|
|
|
$this->tab_translate[$key]=$value;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($enablelangcacheinsession) $tabtranslatedomain[$key]=$value; // To save lang in session
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose($fp);
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// Pour les langues aux fichiers parfois incomplets, on charge la langue alternative
|
2008-12-07 21:54:08 +01:00
|
|
|
|
if (! $newalt && $this->defaultlang != "fr_FR" && $this->defaultlang != "en_US")
|
2008-12-07 20:19:32 +01:00
|
|
|
|
{
|
2009-02-21 01:11:13 +01:00
|
|
|
|
// This function MUST NOY contains call to syslog
|
|
|
|
|
|
//dol_syslog("Translate::Load loading alternate translation file (to complete ".$this->defaultlang."/".$domain.".lang file)", LOG_DEBUG);
|
2008-12-07 20:19:32 +01:00
|
|
|
|
$this->load($domain,1);
|
|
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
$this->tab_loaded[$domain]=1; // Marque ce fichier comme charge
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
// To save lang in session
|
|
|
|
|
|
if ($enablelangcacheinsession && sizeof($tabtranslatedomain)) $_SESSION['lang_'.$domain]=$tabtranslatedomain;
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
break; // Break loop on each root dir
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-07 20:19:32 +01:00
|
|
|
|
if (empty($this->tab_loaded[$domain])) $this->tab_loaded[$domain]=2; // Marque ce fichier comme non trouve
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-03-31 17:17:39 +02:00
|
|
|
|
return 1;
|
2005-10-30 02:07:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2003-05-02 12:33:06 +02:00
|
|
|
|
|
2009-01-30 23:18:07 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Mark domain as not loaded to be able to load it again.
|
|
|
|
|
|
* \param domain File name to load (.lang file). Use @ before value if domain is in a module directory.
|
|
|
|
|
|
*/
|
|
|
|
|
|
function UnLoad($domain)
|
|
|
|
|
|
{
|
2009-02-20 21:28:16 +01:00
|
|
|
|
dol_syslog("Translate::UnLoad domain=".$domain." marked as not loaded", LOG_DEBUG);
|
2009-01-30 23:18:07 +01:00
|
|
|
|
$this->tab_loaded[$domain]=0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
/**
|
2008-01-21 03:19:25 +01:00
|
|
|
|
* \brief Retourne la liste des domaines charg<EFBFBD>es en memoire
|
|
|
|
|
|
* \return array Tableau des domaines charg<EFBFBD>es
|
2005-01-02 17:57:37 +01:00
|
|
|
|
*/
|
2005-10-30 01:42:54 +02:00
|
|
|
|
function list_domainloaded()
|
|
|
|
|
|
{
|
2008-03-31 17:17:39 +02:00
|
|
|
|
$ret='';
|
|
|
|
|
|
foreach($this->tab_loaded as $key=>$val)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($ret) $ret.=',';
|
|
|
|
|
|
$ret.=$key.'='.$val;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $ret;
|
2004-07-25 19:43:23 +02:00
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
2004-10-20 23:15:17 +02:00
|
|
|
|
/**
|
2008-03-15 13:17:15 +01:00
|
|
|
|
* \brief Retourne la version traduite du texte passe en parametre en la codant en HTML
|
2006-12-12 20:25:30 +01:00
|
|
|
|
* Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif
|
2008-03-15 13:17:15 +01:00
|
|
|
|
* et si toujours pas trouve, il est retourne tel quel
|
|
|
|
|
|
* Les parametres de cette methode peuvent contenir de balises HTML.
|
2008-07-28 11:09:03 +02:00
|
|
|
|
* \param key cle de chaine a traduire
|
2006-12-12 20:25:30 +01:00
|
|
|
|
* \param param1 chaine de param1
|
2008-01-20 20:24:05 +01:00
|
|
|
|
* \param param2 chaine de param2
|
|
|
|
|
|
* \param param3 chaine de param3
|
|
|
|
|
|
* \param param4 chaine de param4
|
2006-12-12 20:25:30 +01:00
|
|
|
|
* \param maxsize taille max
|
2008-07-28 11:09:03 +02:00
|
|
|
|
* \return string Chaine traduite et code en HTML
|
2004-10-20 23:15:17 +02:00
|
|
|
|
*/
|
2006-12-12 20:25:30 +01:00
|
|
|
|
function trans($key, $param1='', $param2='', $param3='', $param4='', $maxsize=0)
|
2005-10-30 01:42:54 +02:00
|
|
|
|
{
|
2006-12-12 20:25:30 +01:00
|
|
|
|
if ($this->getTransFromTab($key))
|
|
|
|
|
|
{
|
2008-03-15 13:17:15 +01:00
|
|
|
|
// Translation is available
|
2006-12-12 20:25:30 +01:00
|
|
|
|
$str=sprintf($this->tab_translate[$key],$param1,$param2,$param3,$param4);
|
2009-02-20 23:53:15 +01:00
|
|
|
|
if ($maxsize) $str=dol_trunc($str,$maxsize);
|
2006-12-12 20:43:41 +01:00
|
|
|
|
// On remplace les tags HTML par __xx__ pour eviter traduction par htmlentities
|
|
|
|
|
|
$newstr=ereg_replace('<','__lt__',$str);
|
|
|
|
|
|
$newstr=ereg_replace('>','__gt__',$newstr);
|
2006-12-16 14:45:59 +01:00
|
|
|
|
$newstr=ereg_replace('"','__quot__',$newstr);
|
2007-05-21 02:41:46 +02:00
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
$newstr=$this->convToOutputCharset($newstr); // Convert string to $this->charset_output
|
|
|
|
|
|
|
2006-12-12 20:43:41 +01:00
|
|
|
|
// Cryptage en html de la chaine
|
2007-05-25 22:43:47 +02:00
|
|
|
|
// $newstr est une chaine stockee en memoire au format $this->charset_output
|
2007-05-25 22:02:23 +02:00
|
|
|
|
$newstr=htmlentities($newstr,ENT_QUOTES,$this->charset_output);
|
2007-05-21 02:41:46 +02:00
|
|
|
|
|
2006-12-12 20:43:41 +01:00
|
|
|
|
// On restaure les tags HTML
|
|
|
|
|
|
$newstr=ereg_replace('__lt__','<',$newstr);
|
|
|
|
|
|
$newstr=ereg_replace('__gt__','>',$newstr);
|
2006-12-16 14:45:59 +01:00
|
|
|
|
$newstr=ereg_replace('__quot__','"',$newstr);
|
2006-12-12 20:43:41 +01:00
|
|
|
|
return $newstr;
|
2006-12-12 20:25:30 +01:00
|
|
|
|
}
|
2007-05-26 00:23:03 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2008-03-15 13:17:15 +01:00
|
|
|
|
// Translation is not available
|
2008-03-16 15:52:31 +01:00
|
|
|
|
$newstr=$key;
|
2009-02-03 03:39:28 +01:00
|
|
|
|
if (eregi('CurrencyShort([A-Z]+)$',$key,$reg))
|
2008-03-16 15:52:31 +01:00
|
|
|
|
{
|
|
|
|
|
|
global $db;
|
|
|
|
|
|
//$newstr=$this->getLabelFromKey($db,$reg[1],'c_currencies','code_iso','labelshort');
|
|
|
|
|
|
$newstr=$this->getLabelFromKey($db,$reg[1],'c_currencies','code_iso','code');
|
|
|
|
|
|
}
|
2009-02-03 03:39:28 +01:00
|
|
|
|
else if (eregi('Currency([A-Z]+)$',$key,$reg))
|
2008-03-16 15:52:31 +01:00
|
|
|
|
{
|
|
|
|
|
|
global $db;
|
|
|
|
|
|
$newstr=$this->getLabelFromKey($db,$reg[1],'c_currencies','code_iso','label');
|
|
|
|
|
|
}
|
2009-02-03 03:39:28 +01:00
|
|
|
|
else if (eregi('SendingMethod([0-9A-Z]+)$',$key,$reg))
|
|
|
|
|
|
{
|
|
|
|
|
|
global $db;
|
|
|
|
|
|
$newstr=$this->getLabelFromKey($db,$reg[1],'expedition_methode','code','libelle');
|
|
|
|
|
|
}
|
2008-10-29 22:26:59 +01:00
|
|
|
|
return $this->convToOutputCharset($newstr);
|
2007-05-26 00:23:03 +02:00
|
|
|
|
}
|
2005-03-21 20:04:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2005-10-30 02:07:00 +01:00
|
|
|
|
|
2005-03-21 20:04:39 +01:00
|
|
|
|
/**
|
2009-01-19 23:41:26 +01:00
|
|
|
|
* \brief Return translated value of a text string
|
2005-03-21 20:04:39 +01:00
|
|
|
|
* Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif
|
2009-01-19 23:41:26 +01:00
|
|
|
|
* et si toujours pas trouve, il est retourne tel quel.
|
|
|
|
|
|
* Parameters of this method must not contains any HTML tags.
|
2009-01-25 17:45:04 +01:00
|
|
|
|
* \param key key of string to translate
|
2005-03-21 20:04:39 +01:00
|
|
|
|
* \param param1 chaine de param1
|
|
|
|
|
|
* \param param2 chaine de param1
|
|
|
|
|
|
* \param param3 chaine de param1
|
2006-11-21 21:02:49 +01:00
|
|
|
|
* \param param4 chaine de param1
|
2005-03-21 20:04:39 +01:00
|
|
|
|
* \return string chaine traduite
|
|
|
|
|
|
*/
|
2006-11-21 21:02:49 +01:00
|
|
|
|
function transnoentities($key, $param1='', $param2='', $param3='', $param4='')
|
2005-10-30 02:07:00 +01:00
|
|
|
|
{
|
2008-10-08 21:55:44 +02:00
|
|
|
|
$newstr=$key;
|
|
|
|
|
|
if ($this->getTransFromTab($newstr))
|
2005-10-30 02:07:00 +01:00
|
|
|
|
{
|
2004-06-11 01:38:00 +02:00
|
|
|
|
// Si la traduction est disponible
|
2008-10-08 21:55:44 +02:00
|
|
|
|
$newstr=sprintf($this->tab_translate[$newstr],$param1,$param2,$param3,$param4);
|
2004-06-11 01:38:00 +02:00
|
|
|
|
}
|
2008-10-29 22:26:59 +01:00
|
|
|
|
return $this->convToOutputCharset($newstr);
|
2004-06-11 01:38:00 +02:00
|
|
|
|
}
|
2004-07-13 23:25:53 +02:00
|
|
|
|
|
2005-10-30 02:07:00 +01:00
|
|
|
|
|
2009-01-25 17:45:04 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return translated value of a text string
|
|
|
|
|
|
* Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif
|
|
|
|
|
|
* et si toujours pas trouve, il est retourne tel quel.
|
2009-01-25 17:45:07 +01:00
|
|
|
|
* No convert to encoding charset of lang object is done.
|
2009-01-25 17:45:04 +01:00
|
|
|
|
* Parameters of this method must not contains any HTML tags.
|
|
|
|
|
|
* \param key key of string to translate
|
|
|
|
|
|
* \param param1 chaine de param1
|
|
|
|
|
|
* \param param2 chaine de param1
|
|
|
|
|
|
* \param param3 chaine de param1
|
|
|
|
|
|
* \param param4 chaine de param1
|
|
|
|
|
|
* \return string chaine traduite
|
|
|
|
|
|
*/
|
|
|
|
|
|
function transnoentitiesnoconv($key, $param1='', $param2='', $param3='', $param4='')
|
|
|
|
|
|
{
|
|
|
|
|
|
$newstr=$key;
|
|
|
|
|
|
if ($this->getTransFromTab($newstr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Si la traduction est disponible
|
|
|
|
|
|
$newstr=sprintf($this->tab_translate[$newstr],$param1,$param2,$param3,$param4);
|
|
|
|
|
|
}
|
|
|
|
|
|
return $newstr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-10 22:32:46 +01:00
|
|
|
|
/**
|
2008-07-28 11:09:03 +02:00
|
|
|
|
* \brief Retourne la version traduite du texte passe en parametre complete du code pays
|
2005-02-10 22:32:46 +01:00
|
|
|
|
* \param str chaine a traduire
|
2005-03-01 22:44:32 +01:00
|
|
|
|
* \param countrycode code pays (FR, ...)
|
2005-02-10 22:32:46 +01:00
|
|
|
|
* \return string chaine traduite
|
|
|
|
|
|
*/
|
2005-10-30 02:07:00 +01:00
|
|
|
|
function transcountry($str, $countrycode)
|
|
|
|
|
|
{
|
2005-02-10 22:32:46 +01:00
|
|
|
|
if ($this->tab_translate["$str$countrycode"]) return $this->trans("$str$countrycode");
|
2008-07-28 11:09:03 +02:00
|
|
|
|
else return $this->trans($str);
|
2005-02-10 22:32:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-26 00:23:03 +02:00
|
|
|
|
/**
|
2008-07-28 11:09:03 +02:00
|
|
|
|
* \brief Retourne la version traduite du texte passe en parametre complete du code pays
|
|
|
|
|
|
* \param str chaine a traduire
|
|
|
|
|
|
* \param countrycode code pays (FR, ...)
|
|
|
|
|
|
* \return string chaine traduite
|
|
|
|
|
|
*/
|
|
|
|
|
|
function transcountrynoentities($str, $countrycode)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($this->tab_translate["$str$countrycode"]) return $this->transnoentities("$str$countrycode");
|
|
|
|
|
|
else return $this->transnoentities($str);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-10-20 03:05:41 +02:00
|
|
|
|
/**
|
2008-10-31 03:03:26 +01:00
|
|
|
|
* \brief Convert a string into output charset (this->charset_output that should be defined to conf->character_set_client)
|
|
|
|
|
|
* \param str String to convert
|
2008-10-21 00:12:21 +02:00
|
|
|
|
* \param pagecodefrom Page code of src string
|
2008-10-31 03:03:26 +01:00
|
|
|
|
* \return string Converted string
|
2007-05-26 00:23:03 +02:00
|
|
|
|
*/
|
2008-10-29 22:26:59 +01:00
|
|
|
|
function convToOutputCharset($str,$pagecodefrom='UTF-8')
|
2007-05-26 00:23:03 +02:00
|
|
|
|
{
|
2008-10-21 00:12:21 +02:00
|
|
|
|
if ($pagecodefrom == 'ISO-8859-1' && $this->charset_output == 'UTF-8') $str=utf8_encode($str);
|
|
|
|
|
|
if ($pagecodefrom == 'UTF-8' && $this->charset_output == 'ISO-8859-1') $str=utf8_decode($str);
|
2007-05-26 00:23:03 +02:00
|
|
|
|
return $str;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-06-11 01:38:00 +02:00
|
|
|
|
/**
|
2005-01-02 17:57:37 +01:00
|
|
|
|
* \brief Retourne la liste des langues disponibles
|
|
|
|
|
|
* \return array list of languages
|
|
|
|
|
|
*/
|
2005-02-12 22:25:39 +01:00
|
|
|
|
function get_available_languages($langdir=DOL_DOCUMENT_ROOT)
|
2004-06-11 01:38:00 +02:00
|
|
|
|
{
|
2008-10-20 03:05:41 +02:00
|
|
|
|
// We scan directory langs to detect available languages
|
2005-12-16 01:44:34 +01:00
|
|
|
|
$handle=opendir($langdir ."/langs");
|
|
|
|
|
|
$langs_available=array();
|
|
|
|
|
|
while ($file = trim(readdir($handle)))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (eregi('^[a-z]+_[A-Z]+',$file))
|
|
|
|
|
|
{
|
|
|
|
|
|
array_push($langs_available,$file);
|
|
|
|
|
|
}
|
2004-07-31 13:36:01 +02:00
|
|
|
|
}
|
2005-12-16 01:44:34 +01:00
|
|
|
|
return $langs_available;
|
2003-05-02 12:33:06 +02:00
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2005-05-14 00:43:56 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \brief Renvoi si le fichier $filename existe dans la version de la langue courante ou alternative
|
2008-01-21 03:19:25 +01:00
|
|
|
|
* \param filename nom du fichier <EFBFBD> rechercher
|
2005-05-14 00:43:56 +02:00
|
|
|
|
* \param searchalt cherche aussi dans langue alternative
|
|
|
|
|
|
* \return boolean true si existe, false sinon
|
|
|
|
|
|
*/
|
2005-10-30 02:07:00 +01:00
|
|
|
|
function file_exists($filename,$searchalt=0)
|
|
|
|
|
|
{
|
2008-09-30 01:18:52 +02:00
|
|
|
|
// Test si fichier dans repertoire de la langue
|
2008-12-12 03:16:14 +01:00
|
|
|
|
foreach($this->dir as $searchdir)
|
|
|
|
|
|
{
|
|
|
|
|
|
$htmlfile=$searchdir."/langs/".$this->defaultlang."/".$filename;
|
|
|
|
|
|
if (is_readable($htmlfile)) return true;
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-12-12 03:16:14 +01:00
|
|
|
|
if ($searchalt) {
|
|
|
|
|
|
// Test si fichier dans repertoire de la langue alternative
|
2009-01-15 23:49:06 +01:00
|
|
|
|
if ($this->defaultlang != "en_US") $htmlfilealt = $searchdir."/langs/en_US/".$filename;
|
2008-12-12 03:16:14 +01:00
|
|
|
|
else $htmlfilealt = $searchdir."/langs/fr_FR/".$filename;
|
|
|
|
|
|
if (is_readable($htmlfilealt)) return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2005-05-14 00:43:56 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-01 02:12:55 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* \brief Return a label for a key. Store key-label in a cache.
|
|
|
|
|
|
* \param db Database handler
|
2009-02-03 03:39:28 +01:00
|
|
|
|
* \param key Key to get label (key in language file)
|
2009-01-02 16:12:13 +01:00
|
|
|
|
* \param tablename Table name without prefix
|
2008-03-01 02:12:55 +01:00
|
|
|
|
* \param fieldkey Field for key
|
2009-01-15 23:49:06 +01:00
|
|
|
|
* \param fieldlabel Field for label
|
2009-02-03 03:39:28 +01:00
|
|
|
|
* \param fieldval Value to find record
|
2008-03-01 02:12:55 +01:00
|
|
|
|
* \return string Label
|
2009-02-03 03:39:28 +01:00
|
|
|
|
* \remarks This function can be used to get label in database but more often to get code from key id.
|
2008-03-01 02:12:55 +01:00
|
|
|
|
*/
|
|
|
|
|
|
function getLabelFromKey($db,$key,$tablename,$fieldkey,$fieldlabel)
|
|
|
|
|
|
{
|
2008-06-30 21:34:53 +02:00
|
|
|
|
// If key empty
|
2008-05-03 23:44:32 +02:00
|
|
|
|
if ($key == '') return '';
|
2008-06-30 21:34:53 +02:00
|
|
|
|
|
|
|
|
|
|
// Check in language array
|
|
|
|
|
|
if ($this->transnoentities($key) != $key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->transnoentities($key); // Found in language array
|
|
|
|
|
|
}
|
2009-01-15 23:49:06 +01:00
|
|
|
|
|
2008-06-30 21:34:53 +02:00
|
|
|
|
// Check in cache
|
2009-01-02 16:12:13 +01:00
|
|
|
|
if (isset($this->cache_labels[$tablename][$key])) // Can be defined to 0 or ''
|
2008-03-01 02:12:55 +01:00
|
|
|
|
{
|
2009-01-02 16:12:13 +01:00
|
|
|
|
return $this->cache_labels[$tablename][$key]; // Found in cache
|
2008-03-01 02:12:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT ".$fieldlabel." as label";
|
|
|
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX.$tablename;
|
|
|
|
|
|
$sql.= " WHERE ".$fieldkey." = '".$key."'";
|
2009-02-20 21:28:16 +01:00
|
|
|
|
dol_syslog('Translate::getLabelFromKey sql='.$sql,LOG_DEBUG);
|
2008-03-01 02:12:55 +01:00
|
|
|
|
$resql = $db->query($sql);
|
|
|
|
|
|
if ($resql)
|
|
|
|
|
|
{
|
|
|
|
|
|
$obj = $db->fetch_object($resql);
|
2009-01-02 16:12:13 +01:00
|
|
|
|
if ($obj) $this->cache_labels[$tablename][$key]=$obj->label;
|
|
|
|
|
|
else $this->cache_labels[$tablename][$key]='';
|
2008-06-30 21:34:53 +02:00
|
|
|
|
$db->free($resql);
|
2008-03-01 02:12:55 +01:00
|
|
|
|
return $this->cache_labels[$tablename][$key];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2008-03-16 15:52:31 +01:00
|
|
|
|
$this->error=$db->lasterror();
|
2009-02-20 21:28:16 +01:00
|
|
|
|
dol_syslog("Translate::getLabelFromKey error=".$this->error,LOG_ERR);
|
2008-03-01 02:12:55 +01:00
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2008-10-20 03:05:41 +02:00
|
|
|
|
|
2003-05-02 12:33:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|