[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/ -> translate.class.php (source)

   1  <?php
   2  /* ***************************************************************************
   3   * Copyright (C) 2001      Eric Seigne         <erics@rycks.com>
   4   * Copyright (C) 2004-2005 Destailleur Laurent <eldy@users.sourceforge.net>
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License
  17   * along with this program; if not, write to the Free Software
  18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19   * ************************************************************************* */
  20  
  21  /**
  22          \file       htdocs/translate.class.php
  23          \brief      Fichier de la classe de traduction
  24          \author        Laurent Destailleur
  25          \version    $Revision: 1.30 $
  26  */
  27  
  28  
  29  /** 
  30          \class      Translate
  31          \brief      Classe permettant de gérer les traductions
  32  */
  33  
  34  class Translate {
  35  
  36      var $dir;
  37      var $defaultlang;
  38  
  39      var $tab_loaded=array();
  40      var $tab_translate=array();
  41  
  42  
  43      /**
  44       *  \brief      Constructeur de la classe
  45       *  \param      dir             Repertoire racine des fichiers de traduction
  46       */
  47      function Translate($dir = "")
  48      {
  49          $this->dir=$dir;
  50      }
  51  
  52  
  53      /**
  54       *  \brief      Accesseur de this->defaultlang
  55       *  \param      defaultlang     Langue par defaut à utiliser
  56       */
  57      function setDefaultLang($defaultlang)
  58      {
  59          if ($defaultlang == 'auto')
  60          {
  61              $langpref=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
  62              $langpref=eregi_replace(";[^,]*","",$langpref);
  63              $langpref=eregi_replace("-","_",$langpref);
  64  
  65              $langlist=split("[;,]",$langpref);
  66  
  67              $langpart=split("_",$langlist[0]);
  68  
  69              if (isset($langpart[1])) $defaultlang=strtolower($langpart[0])."_".strtoupper($langpart[1]);
  70              else $defaultlang=strtolower($langpart[0])."_".strtoupper($langpart[0]);
  71          }
  72  
  73          $this->defaultlang=$defaultlang;
  74      }
  75  
  76      
  77      /**
  78       *  \brief      Accesseur de this->defaultlang
  79       *  \return     string      Langue utilisée
  80       */
  81      function getDefaultLang()
  82      {
  83          return $this->defaultlang;
  84      }
  85  
  86      
  87      /**
  88              \brief      Positionne environnement PHP en fonction du langage
  89              \remarks    Le code langue long (fr_FR, en_US, ...) doit être positionné
  90              \return     int             >0 si ok, <0 so ko
  91      */
  92      function setPhpLang()
  93      {
  94          //dolibarr_syslog("Translate::set_php_lang: code_lang=$code_lang code_lang_tirer=$code_lang_tiret");
  95         
  96          $code_lang_tiret=ereg_replace('_','-',$this->defaultlang);
  97          setlocale(LC_ALL, $this->defaultlang);    // Compenser pb de locale avec windows
  98          setlocale(LC_ALL, $code_lang_tiret);
  99          if (defined("MAIN_FORCE_SETLOCALE_LC_ALL") && MAIN_FORCE_SETLOCALE_LC_ALL) setlocale(LC_ALL, MAIN_FORCE_SETLOCALE_LC_ALL);
 100          if (defined("MAIN_FORCE_SETLOCALE_LC_TIME") && MAIN_FORCE_SETLOCALE_LC_TIME) setlocale(LC_TIME, MAIN_FORCE_SETLOCALE_LC_TIME);
 101          if (defined("MAIN_FORCE_SETLOCALE_LC_NUMERIC") && MAIN_FORCE_SETLOCALE_LC_NUMERIC) setlocale(LC_NUMERIC, MAIN_FORCE_SETLOCALE_LC_NUMERIC);
 102          if (defined("MAIN_FORCE_SETLOCALE_LC_MONETARY") && MAIN_FORCE_SETLOCALE_LC_MONETARY) setlocale(LC_MONETARY, MAIN_FORCE_SETLOCALE_LC_MONETARY);
 103      
 104          return 1;
 105      }
 106  
 107  
 108      /**
 109       *  \brief      Charge en mémoire le tableau de traduction pour un domaine particulier
 110       *              Si le domaine est deja chargé, la fonction ne fait rien
 111       *  \param      domain      Nom du domain (fichier lang) à charger
 112       *  \param      alt         Utilise le fichier alternatif meme si fichier dans la langue est trouvée
 113       */
 114      function Load($domain,$alt=0)
 115      {
 116          if (isset($this->tab_loaded[$domain]) && $this->tab_loaded[$domain]) { return; }    // Le fichier de ce domaine est deja chargé
 117          
 118          // Repertoire de traduction
 119          $scandir = $this->dir."/".$this->defaultlang;
 120          $file_lang =  $scandir . "/$domain.lang";
 121          $filelangexists=is_file($file_lang);
 122  
 123          if ($alt || ! $filelangexists)
 124          {
 125              // Repertoire de la langue alternative
 126              if ($this->defaultlang != "en_US") $scandiralt = $this->dir."/en_US";   
 127              else $scandiralt = $this->dir."/fr_FR";
 128              $file_lang = $scandiralt . "/$domain.lang";
 129              $filelangexists=is_file($file_lang);
 130              $alt=1;
 131          }
 132          
 133          if ($filelangexists)
 134          {
 135              if($fp = @fopen($file_lang,"rt"))
 136              {
 137                  $finded = 0;
 138                  while (($ligne = fgets($fp,4096)) && ($finded == 0))
 139                  {
 140                      if ($ligne[0] != "\n" && $ligne[0] != " " && $ligne[0] != "#")
 141                      {
 142                          $tab=split('=',$ligne,2);
 143                          //print "Domain=$domain, found a string for $tab[0] with value $tab[1]<br>";
 144                          if (! isset($this->tab_translate[$tab[0]])) $this->tab_translate[$tab[0]]=trim(isset($tab[1])?$tab[1]:'');
 145                      }
 146                  }
 147                  fclose($fp);
 148  
 149                  // Pour les langues aux fichiers parfois incomplets, on charge la langue alternative
 150                  if (! $alt && $this->defaultlang != "fr_FR" && $this->defaultlang != "en_US")
 151                  {
 152                      dolibarr_syslog("translate::load loading alternate translation file");
 153                      $this->load($domain,1);
 154                  }
 155  
 156                  $this->tab_loaded[$domain]=1;           // Marque ce fichier comme chargé
 157              }
 158          }
 159      }
 160  
 161  
 162      /**     
 163       *    \brief      Retourne la liste des domaines chargées en memoire
 164       *  \return     array       Tableau des domaines chargées
 165       */
 166      function list_domainloaded()
 167      {
 168          return join(",",array_keys($this->tab_loaded));
 169      }
 170      
 171      
 172      /**
 173       *  \brief       Retourne la version traduite du texte passé en paramètre
 174       *               Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif
 175       *               et si toujours pas trouvé, il est retourné tel quel
 176       *  \param       str         chaine a traduire
 177       *  \param       param1      chaine de param1
 178       *  \param       param2      chaine de param1
 179       *  \param       param3      chaine de param1
 180       *  \return      string      chaine traduite
 181       */
 182      function trans($str, $param1='', $param2='', $param3='')
 183      {
 184          return $this->transnoentities($str,htmlentities($param1),htmlentities($param2),htmlentities($param3));
 185      }
 186  
 187  
 188      /**
 189       *  \brief       Retourne la version traduite du texte passé en paramètre
 190       *               Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif
 191       *               et si toujours pas trouvé, il est retourné tel quel
 192       *  \param       str         chaine a traduire
 193       *  \param       param1      chaine de param1
 194       *  \param       param2      chaine de param1
 195       *  \param       param3      chaine de param1
 196       *  \return      string      chaine traduite
 197       */
 198      function transnoentities($str, $param1='', $param2='', $param3='')
 199      {
 200          if (isset($this->tab_translate[$str]) && $this->tab_translate[$str])
 201          {
 202              // Si la traduction est disponible
 203              return sprintf($this->tab_translate[$str],$param1,$param2,$param3);
 204          }
 205          return $str;
 206      }
 207  
 208  
 209      /**
 210       *  \brief       Retourne la version traduite du texte passé en paramètre complété du code pays
 211       *  \param       str            chaine a traduire
 212       *  \param       countrycode    code pays (FR, ...)
 213       *  \return      string         chaine traduite
 214       */
 215      function transcountry($str, $countrycode)
 216      {
 217          if ($this->tab_translate["$str$countrycode"]) return $this->trans("$str$countrycode");
 218          else return $this->trans("$str");
 219      }
 220  
 221  
 222      /**
 223       *  \brief       Retourne la liste des langues disponibles
 224       *  \return      array     list of languages
 225       */
 226      function get_available_languages($langdir=DOL_DOCUMENT_ROOT)
 227      {
 228          // On parcour le répertoire langs pour détecter les langues disponibles
 229          $handle=opendir($langdir ."/langs");
 230          $langs_available=array();
 231          while ($file = trim(readdir($handle)))
 232          {
 233              if (eregi('^[a-z]+_[A-Z]+',$file))
 234              {
 235                  array_push($langs_available,$file);
 236              }
 237          }
 238          return $langs_available;
 239      }
 240      
 241      /**
 242       *  \brief       Expédie le header correct et retourne le début de la page html
 243       *  [en]         Send header and return a string of html start page
 244       *  \return      string      html header avec charset
 245       */
 246          
 247      function lang_header()
 248      {
 249          $this->load("main");
 250          $charset=$this->trans("charset");
 251          if (! $charset) $charset="iso-8859-1";
 252      
 253          //header("Content-Type: text/html; charset=$charset");
 254          $texte = "<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$charset\">\n";
 255      
 256          return $texte;
 257      }
 258  
 259  
 260     /**
 261       *  \brief      Renvoi si le fichier $filename existe dans la version de la langue courante ou alternative
 262       *  \param      filename        nom du fichier à rechercher
 263       *  \param      searchalt       cherche aussi dans langue alternative
 264       *  \return     boolean         true si existe, false sinon
 265       */
 266           
 267      function file_exists($filename,$searchalt=0)
 268      {
 269          // Test si fichier dans répertoire de la langue
 270          $htmlfile=$this->dir."/".$this->defaultlang."/".$filename;
 271          if (is_readable($htmlfile)) return true;
 272  
 273          if ($searchalt) {
 274              // Test si fichier dans répertoire de la langue alternative
 275              if ($this->defaultlang != "en_US") $htmlfilealt = $this->dir."/en_US/".$filename;   
 276              else $htmlfilealt = $this->dir."/fr_FR/".$filename;
 277              if (is_readable($htmlfilealt)) return true;
 278          }
 279          
 280          return false;
 281      }
 282  
 283  
 284     /**
 285       *  \brief      Renvoi le fichier $filename dans la version de la langue courante, sinon alternative
 286       *  \param      filename        nom du fichier à rechercher
 287       *  \param      searchalt       cherche aussi dans langue alternative
 288       */
 289      function print_file($filename,$searchalt=0)
 290      {
 291          // Test si fichier dans répertoire de la langue
 292          $htmlfile=$this->dir."/".$this->defaultlang."/".$filename;
 293          if (is_readable($htmlfile))
 294          {
 295              include $htmlfile;
 296              return true;
 297          }
 298  
 299          if ($searchalt) {
 300              // Test si fichier dans répertoire de la langue alternative
 301              if ($this->defaultlang != "en_US") $htmlfilealt = $this->dir."/en_US/".$filename;   
 302              else $htmlfilealt = $this->dir."/fr_FR/".$filename;
 303              if (is_readable($htmlfilealt))
 304              {
 305                  include $htmlfilealt;
 306                  return true;
 307              }
 308          }
 309          
 310          return false;
 311      }
 312  
 313  }
 314  
 315  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics