[ Index ]
 

Code source de SPIP 1.9.2c

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ecrire/inc/ -> lang.php (source)

   1  <?php
   2  
   3  /***************************************************************************\
   4   *  SPIP, Systeme de publication pour l'internet                           *
   5   *                                                                         *
   6   *  Copyright (c) 2001-2007                                                *
   7   *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
   8   *                                                                         *
   9   *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
  10   *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
  11  \***************************************************************************/
  12  
  13  if (!defined("_ECRIRE_INC_VERSION")) return;
  14  
  15  include_spip('inc/actions');
  16  
  17  //
  18  // Changer la langue courante
  19  //
  20  // http://doc.spip.org/@changer_langue
  21  function changer_langue($lang) {
  22      global $all_langs, $spip_lang_rtl, $spip_lang_right, $spip_lang_left, $spip_lang_dir, $spip_dir_lang;
  23  
  24      $liste_langues = ',' . $all_langs.','.@$GLOBALS['meta']['langues_multilingue'] . ',';
  25  
  26      // Si la langue demandee n'existe pas, on essaie d'autres variantes
  27      // Exemple : 'pt-br' => 'pt_br' => 'pt'
  28      $lang = str_replace('-', '_', trim($lang));
  29      if (!$lang)
  30          return false;
  31  
  32      if (ereg(",$lang,", $liste_langues)
  33      OR ($lang = preg_replace(',_.*,', '', $lang)
  34      AND ereg(",$lang,", $liste_langues))) {
  35  
  36          $GLOBALS['spip_lang'] = $lang;
  37          $spip_lang_rtl =   lang_dir($lang, '', '_rtl');
  38          $spip_lang_left =  lang_dir($lang, 'left', 'right');
  39          $spip_lang_right = lang_dir($lang, 'right', 'left');
  40          $spip_lang_dir =   lang_dir($lang);
  41          $spip_dir_lang = " dir='$spip_lang_dir'";
  42  
  43          return true;
  44      } else
  45          return false;
  46  
  47  }
  48  
  49  // http://doc.spip.org/@traduire_nom_langue
  50  function traduire_nom_langue($lang) {
  51      include_spip('inc/lang_liste');
  52      include_spip('inc/charsets');
  53      return html2unicode(isset($GLOBALS['codes_langues'][$lang]) ? $GLOBALS['codes_langues'][$lang] : $lang);
  54  }
  55  
  56  //
  57  // Filtres de langue
  58  //
  59  
  60  // Donne la direction d'ecriture a partir de la langue. Retourne 'gaucher' si
  61  // la langue est arabe, persan, kurde, pachto, ourdou (langues ecrites en
  62  // alphabet arabe a priori), hebreu, yiddish (langues ecrites en alphabet
  63  // hebreu a priori), 'droitier' sinon.
  64  // C'est utilise par #LANG_DIR, #LANG_LEFT, #LANG_RIGHT.
  65  // http://doc.spip.org/@lang_dir
  66  function lang_dir($lang, $droitier='ltr', $gaucher='rtl') {
  67      if ($lang=='ar' OR $lang=='fa' OR $lang == 'ku' OR $lang == 'ps'
  68      OR $lang == 'ur' OR $lang == 'he' OR $lang == 'yi')
  69          return $gaucher;
  70      else
  71          return $droitier;
  72  }
  73  
  74  // http://doc.spip.org/@lang_typo
  75  function lang_typo($lang) {
  76      if ($lang == 'eo' OR $lang == 'fr' OR substr($lang, 0, 3) == 'fr_' OR $lang == 'cpf')
  77          return 'fr';
  78      else if ($lang)
  79          return 'en';
  80      else
  81          return false;
  82  }
  83  
  84  // service pour que l'espace prive reflete la typo et la direction des objets affiches
  85  // http://doc.spip.org/@changer_typo
  86  function changer_typo($lang = '', $source = '') {
  87      global $lang_objet, $lang_dir, $dir_lang;
  88  
  89      if (ereg("^(article|rubrique|breve|auteur)([0-9]+)", $source, $regs)) {
  90          $r = spip_fetch_array(spip_query("SELECT lang FROM spip_".$regs[1]."s WHERE id_".$regs[1]."=".$regs[2]));
  91          $lang = $r['lang'];
  92      }
  93  
  94      if (!$lang)
  95          $lang = $GLOBALS['meta']['langue_site'];
  96  
  97      $lang_objet = $lang;
  98      $lang_dir = lang_dir($lang);
  99      $dir_lang = " dir='$lang_dir'";
 100  }
 101  
 102  //
 103  // Afficher un menu de selection de langue
 104  // - 'var_lang_ecrire' = langue interface privee,
 105  // - 'var_lang' = langue de l'article, espace public
 106  // - 'changer_lang' = langue de l'article, espace prive
 107  // 
 108  // http://doc.spip.org/@menu_langues
 109  function menu_langues($nom_select = 'var_lang', $default = '', $texte = '', $herit = '', $lien='') {
 110      global $couleur_foncee, $connect_id_auteur;
 111  
 112      $ret = liste_options_langues($nom_select, $default, $herit);
 113  
 114      if (!$ret) return '';
 115  
 116      if (!$couleur_foncee) $couleur_foncee = '#044476';
 117  
 118      if (!$lien)
 119          $lien = self();
 120  
 121      if ($nom_select == 'changer_lang') {
 122          $lien = parametre_url($lien, 'changer_lang', '');
 123          $lien = parametre_url($lien, 'url', '');
 124          $cible = '';
 125      } else {
 126          if (_DIR_RESTREINT) {
 127              $cible = $lien;
 128              $lien = generer_url_action('cookie');
 129          } else {
 130              $cible = _DIR_RESTREINT_ABS . $lien;
 131              if (_FILE_CONNECT) {
 132                $lien = generer_action_auteur('converser','');
 133              } else $lien = generer_url_action('cookie');
 134          }
 135      }
 136      // attention, en Ajax ce select doit etre suivi de
 137      // <span><input type='submit'
 138      $change = ($lien === 'ajax')
 139      ? "\nonchange=\"this.nextSibling.firstChild.style.visibility='visible';\""
 140      : ("\nonchange=\"document.location.href='"
 141         . parametre_url($lien, 'url', str_replace('&amp;', '&', $cible))
 142         ."&amp;$nom_select='+this.options[this.selectedIndex].value\"");
 143  
 144      $ret = $texte
 145        . "<select name='$nom_select' "
 146        . (_DIR_RESTREINT ?
 147           ("class='forml' style='vertical-align: top; max-height: 24px; margin-bottom: 5px; width: 120px;'") :
 148           (($nom_select == 'var_lang_ecrire')  ?
 149            ("class='verdana1' style='background-color: " . $couleur_foncee
 150             . "; max-height: 24px; border: 1px solid white; color: white; width: 100px;'") :
 151            "class='fondl'"))
 152        . $change
 153        . ">\n"
 154        . $ret
 155        . "</select>";
 156      if ($lien === 'ajax') return $ret;
 157  
 158      $ret .= "<noscript><div><input type='submit' class='fondo' value='". _T('bouton_changer')."' /></div></noscript>";
 159  
 160      return "\n<form action='$lien' method='post' style='margin:0px; padding:0px;'>\n<div>"
 161        . (!$cible ? '' : "<input type='hidden' name='url' value='$cible' />")
 162        . $ret
 163        . "</div></form>\n";
 164  }
 165  
 166  // http://doc.spip.org/@liste_options_langues
 167  function liste_options_langues($nom_select, $default='', $herit='') {
 168  
 169      if ($default == '') $default = $GLOBALS['spip_lang'];
 170      switch($nom_select) {
 171          # #MENU_LANG
 172          case 'var_lang':
 173          # menu de changement de la langue d'un article
 174          # les langues selectionnees dans la configuration "multilinguisme"
 175          case 'changer_lang':
 176              $langues = explode(',', $GLOBALS['meta']['langues_multilingue']);
 177              break;
 178          # menu de l'interface (privee, installation et panneau de login)
 179          # les langues presentes sous forme de fichiers de langue
 180          case 'var_lang_ecrire':
 181          default:
 182              $langues = explode(',', $GLOBALS['meta']['langues_proposees']);
 183              break;
 184  
 185  # dernier choix possible : toutes les langues = langues_proposees 
 186  # + langues_multilingues ; mais, ne sert pas
 187  #            $langues = explode(',', $GLOBALS['all_langs']);
 188      }
 189      if (count($langues) <= 1) return '';
 190      $ret = '';
 191      sort($langues);
 192      foreach ($langues as $l) {
 193          $selected = ($l == $default) ? ' selected=\'selected\'' : '';
 194          if ($l == $herit) {
 195              $ret .= "<option class='maj-debut' style='font-weight: bold;' value='herit'$selected>"
 196                  .traduire_nom_langue($herit)." ("._T('info_multi_herit').")</option>\n";
 197          }
 198          ## ici ce serait bien de pouvoir choisir entre "langue par defaut"
 199          ## et "langue heritee"
 200          else
 201              $ret .= "<option class='maj-debut' value='$l'$selected>".traduire_nom_langue($l)."</option>\n";
 202      }
 203      return $ret;
 204  }
 205  
 206  // Cette fonction calcule la liste des langues reellement utilisees dans le
 207  // site public
 208  // http://doc.spip.org/@calculer_langues_utilisees
 209  function calculer_langues_utilisees () {
 210      $langues_utilisees = array();
 211  
 212      $langues_utilisees[$GLOBALS['meta']['langue_site']] = 1;
 213  
 214      $result = spip_query("SELECT DISTINCT lang FROM spip_articles WHERE statut='publie'");
 215      while ($row = spip_fetch_array($result)) {
 216          $langues_utilisees[$row['lang']] = 1;
 217      }
 218  
 219      $result = spip_query("SELECT DISTINCT lang FROM spip_breves WHERE statut='publie'");
 220      while ($row = spip_fetch_array($result)) {
 221          $langues_utilisees[$row['lang']] = 1;
 222      }
 223  
 224      $result = spip_query("SELECT DISTINCT lang FROM spip_rubriques WHERE statut='publie'");
 225      while ($row = spip_fetch_array($result)) {
 226          $langues_utilisees[$row['lang']] = 1;
 227      }
 228  
 229      $langues_utilisees = array_filter(array_keys($langues_utilisees));
 230      sort($langues_utilisees);
 231      $langues_utilisees = join(',',$langues_utilisees);
 232  
 233      include_spip('inc/meta');
 234      ecrire_meta('langues_utilisees', $langues_utilisees);
 235      ecrire_metas();
 236  }
 237  
 238  //
 239  // Cette fonction est appelee depuis public/global si on a installe
 240  // la variable de personnalisation $forcer_lang ; elle renvoie le brouteur
 241  // si necessaire vers l'URL xxxx?lang=ll
 242  //
 243  // http://doc.spip.org/@verifier_lang_url
 244  function verifier_lang_url() {
 245      global $spip_lang;
 246  
 247      // quelle langue est demandee ?
 248      $lang_demandee = $GLOBALS['meta']['langue_site'];
 249      if (isset($_COOKIE['spip_lang_ecrire']))
 250          $lang_demandee = $_COOKIE['spip_lang_ecrire'];
 251      if (isset($_COOKIE['spip_lang']))
 252          $lang_demandee = $_COOKIE['spip_lang'];
 253      if (isset($_GET['lang']))
 254          $lang_demandee = $_GET['lang'];
 255  
 256      // Renvoyer si besoin (et si la langue demandee existe)
 257      if ($spip_lang != $lang_demandee
 258      AND changer_langue($lang_demandee)
 259      AND $lang_demandee != @$_GET['lang']) {
 260          $destination = parametre_url(self(),'lang', $lang_demandee, '&');
 261          if (isset($GLOBALS['var_mode']))
 262              $destination = parametre_url($destination, 'var_mode', $GLOBALS['var_mode'], '&');
 263          include_spip('inc/headers');
 264          redirige_par_entete($destination);
 265      }
 266  
 267      // Subtilite : si la langue demandee par cookie est la bonne
 268      // alors on fait comme si $lang etait passee dans l'URL
 269      // (pour criteres {lang}).
 270      $GLOBALS['lang'] = $_GET['lang'] = $spip_lang;
 271  }
 272  
 273  
 274  //
 275  // Selection de langue haut niveau
 276  //
 277  // http://doc.spip.org/@utiliser_langue_site
 278  function utiliser_langue_site() {
 279      changer_langue($GLOBALS['meta']['langue_site']);
 280  }
 281  
 282  // http://doc.spip.org/@utiliser_langue_visiteur
 283  function utiliser_langue_visiteur() {
 284  
 285      $l = (_DIR_RESTREINT  ? 'spip_lang' : 'spip_lang_ecrire');
 286      if (isset($_COOKIE[$l]))
 287          if (changer_langue($l = $_COOKIE[$l])) return $l;
 288  
 289      if (isset($GLOBALS['auteur_session']['lang']))
 290          if (changer_langue($l = $GLOBALS['auteur_session']['lang']))
 291              return $l;
 292  
 293      foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $s)  {
 294          if (eregi('^([a-z]{2,3})(-[a-z]{2,3})?(;q=[0-9.]+)?$', trim($s), $r)) {
 295              if (changer_langue($l=strtolower($r[1]))) return $l;
 296          }
 297      }
 298  
 299      return changer_langue($GLOBALS['langue_site']);
 300  }
 301  
 302  // Une fonction qui donne le repertoire ou trouver des fichiers de langue
 303  // note : pourrait en donner une liste... complique
 304  // http://doc.spip.org/@repertoire_lang
 305  function repertoire_lang($module='spip', $lang='fr') {
 306      # valeur forcee (par ex.sur spip.net), old style, a faire disparaitre
 307      if (defined('_DIR_LANG'))
 308          return _DIR_LANG;
 309  
 310      # regarder s'il existe une v.f. qq part
 311      if ($f = include_spip('lang/'.$module.'_'.$lang, false));
 312          return dirname($f).'/';
 313  
 314      # sinon, je ne sais trop pas quoi dire...
 315      return _DIR_RESTREINT . 'lang/';
 316  }
 317  
 318  //
 319  // Initialisation
 320  //
 321  // http://doc.spip.org/@init_langues
 322  function init_langues() {
 323      global $all_langs, $langue_site;
 324      global $pile_langues, $lang_objet, $lang_dir;
 325  
 326      $all_langs = @$GLOBALS['meta']['langues_proposees'];
 327      $pile_langues = array();
 328      $lang_objet = '';
 329      $lang_dir = '';
 330  
 331      $toutes_langs = Array();
 332      if (!$all_langs || !$langue_site || !_DIR_RESTREINT) {
 333          if (!$d = @opendir(repertoire_lang())) return;
 334          while (($f = readdir($d)) !== false) {
 335              if (ereg('^spip_([a-z_]+)\.php[3]?$', $f, $regs))
 336                  $toutes_langs[] = $regs[1];
 337          }
 338          closedir($d);
 339          sort($toutes_langs);
 340          $all_langs2 = join(',', $toutes_langs);
 341          // Si les langues n'ont pas change, ne rien faire
 342          if ($all_langs2 != $all_langs) {
 343              include_spip('inc/meta');
 344              $all_langs = $all_langs2;
 345              if (!$langue_site) {
 346                  // Initialisation : le francais par defaut, sinon la premiere langue trouvee
 347                  if (ereg(',fr,', ",$all_langs,")) $langue_site = 'fr';
 348                  else list(, $langue_site) = each($toutes_langs);
 349                  ecrire_meta('langue_site', $langue_site);
 350              }
 351              ecrire_meta('langues_proposees', $all_langs);
 352              ecrire_metas();
 353          }
 354      }
 355  }
 356  
 357  // http://doc.spip.org/@html_lang_attributes
 358  function html_lang_attributes()
 359  {
 360      return  "<html lang='"
 361      . $GLOBALS['spip_lang']
 362      . "' dir='"
 363      . ($GLOBALS['spip_lang_rtl'] ? 'rtl' : 'ltr')
 364        . "'>\n" ;
 365  }
 366  init_langues();
 367  utiliser_langue_site();
 368  ?>


Généré le : Wed Nov 21 10:20:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics