From 357e6774c5cacd3546faf71e4f57642f0c4d658b Mon Sep 17 00:00:00 2001 From: "Committo,Ergo:sum" <esj@rezo.net> Date: Fri, 27 Oct 2006 10:34:55 +0000 Subject: [PATCH] =?UTF-8?q?La=20fonction=20de=20traduction=20devient=20sur?= =?UTF-8?q?chargeable=20sous=20le=20nom=20de=20inc=5Ftraduire,=20le=20fich?= =?UTF-8?q?ier=20inc/traduire=20=C3=A9tant=20originellement=20une=20partie?= =?UTF-8?q?=20de=20inc/lang.=20Les=20traductions=20sont=20m=C3=A9moris?= =?UTF-8?q?=C3=A9es=20dans=20une=20statique,=20l'exp=C3=A9rience=20montran?= =?UTF-8?q?t=20qu'elles=20souvent=20redemand=C3=A9es.=20Par=20ailleurs,=20?= =?UTF-8?q?inc/lang=20=C3=A9vite=20d'appeler=20changer=5Flang=20en=20rafal?= =?UTF-8?q?e,=20une=20fois=20suffit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 1 + ecrire/action/inscription.php | 1 - ecrire/exec/lang_raccourcis.php | 2 +- ecrire/inc/lang.php | 153 +++----------------------------- ecrire/inc/traduire.php | 121 +++++++++++++++++++++++++ ecrire/inc/utils.php | 17 ++-- 6 files changed, 143 insertions(+), 152 deletions(-) create mode 100644 ecrire/inc/traduire.php diff --git a/.gitattributes b/.gitattributes index 9d32116f0f..5d26130dda 100644 --- a/.gitattributes +++ b/.gitattributes @@ -481,6 +481,7 @@ ecrire/inc/sites_voir.php -text ecrire/inc/suivi_versions.php -text ecrire/inc/syndic.php -text ecrire/inc/tourner.php -text +ecrire/inc/traduire.php -text ecrire/inc/utils.php -text ecrire/inc/vieilles_defs.php -text ecrire/inc/virtualiser.php -text diff --git a/ecrire/action/inscription.php b/ecrire/action/inscription.php index 380999f19e..d7be491032 100644 --- a/ecrire/action/inscription.php +++ b/ecrire/action/inscription.php @@ -20,7 +20,6 @@ include_spip('inc/headers'); // http://doc.spip.org/@action_inscription_dist function action_inscription_dist() { - utiliser_langue_site(); utiliser_langue_visiteur(); http_no_cache(); diff --git a/ecrire/exec/lang_raccourcis.php b/ecrire/exec/lang_raccourcis.php index 781b5ad4c1..0388b21fcd 100644 --- a/ecrire/exec/lang_raccourcis.php +++ b/ecrire/exec/lang_raccourcis.php @@ -13,7 +13,7 @@ if (!defined("_ECRIRE_INC_VERSION")) return; include_spip('inc/presentation'); -include_spip('inc/lang'); +include_spip('inc/traduire'); // http://doc.spip.org/@exec_lang_raccourcis_dist function exec_lang_raccourcis_dist() { diff --git a/ecrire/inc/lang.php b/ecrire/inc/lang.php index c28a5f9ee1..6ce99ff3c4 100644 --- a/ecrire/inc/lang.php +++ b/ecrire/inc/lang.php @@ -14,68 +14,6 @@ if (!defined("_ECRIRE_INC_VERSION")) return; include_spip('inc/actions'); -// -// Charger un fichier langue -// -// http://doc.spip.org/@chercher_module_lang -function chercher_module_lang($module, $lang = '') { - if ($lang) - $lang = '_'.$lang; - - // 1) dans un repertoire nomme lang/ se trouvant sur le chemin - if ($f = include_spip('lang/'.$module.$lang, false)) - return $f; - - // 2) directement dans le chemin (old style) - return include_spip($module.$lang, false); -} - -// http://doc.spip.org/@charger_langue -function charger_langue($lang, $module = 'spip') { - if ($lang AND $fichier_lang = chercher_module_lang($module, $lang)) { - $GLOBALS['idx_lang']='i18n_'.$module.'_'.$lang; - include_once($fichier_lang); - } else { - // si le fichier de langue du module n'existe pas, on se rabat sur - // la langue par defaut du site -- et au pire sur le francais, qui - // *par definition* doit exister, et on copie le tableau dans la - // var liee a la langue - $l = $GLOBALS['meta']['langue_site']; - if (!$fichier_lang = chercher_module_lang($module, $l)) - $fichier_lang = chercher_module_lang($module, 'fr'); - - if ($fichier_lang) { - $GLOBALS['idx_lang']='i18n_'.$module.'_' .$l; - include($fichier_lang); - $GLOBALS['i18n_'.$module.'_'.$lang] - = &$GLOBALS['i18n_'.$module.'_'.$l]; - #spip_log("module de langue : ${module}_$l.php"); - } - } -} - -// -// Surcharger le fichier de langue courant avec un autre (tordu, hein...) -// -// http://doc.spip.org/@surcharger_langue -function surcharger_langue($fichier) { - - $idx_lang_normal = $GLOBALS['idx_lang']; - $idx_lang_surcharge = $GLOBALS['idx_lang'].'_temporaire'; - $GLOBALS['idx_lang'] = $idx_lang_surcharge; - include($fichier); - if (is_array($GLOBALS[$idx_lang_surcharge])) { - $GLOBALS[$idx_lang_normal] = array_merge( - $GLOBALS[$idx_lang_normal], - $GLOBALS[$idx_lang_surcharge] - ); - } - unset ($GLOBALS[$idx_lang_surcharge]); - $GLOBALS['idx_lang'] = $idx_lang_normal; -} - - - // // Changer la langue courante // @@ -108,77 +46,6 @@ function changer_langue($lang) { } -// -// Regler la langue courante selon les infos envoyees par le brouteur -// -// http://doc.spip.org/@regler_langue_navigateur -function regler_langue_navigateur() { - $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); - if (is_array($accept_langs)) { - while(list(, $s) = each($accept_langs)) { - if (eregi('^([a-z]{2,3})(-[a-z]{2,3})?(;q=[0-9.]+)?$', trim($s), $r)) { - $lang = strtolower($r[1]); - if (changer_langue($lang)) return $lang; - } - } - } - return false; -} - - -// -// Traduire une chaine internationalisee -// -// http://doc.spip.org/@traduire_chaine -function traduire_chaine($code) { - global $spip_lang; - - // modules par defaut - - $modules = array('spip', 'ecrire'); - - // modules demandes explicitement - $code_ori = $code; # le garder pour le fallback plus tard - if (strpos($code, ':')) { - if (ereg("^([a-z/]+):(.*)$", $code, $regs)) { - $modules = explode("/",$regs[1]); - $code = $regs[2]; - } - } - $text = ''; - // parcourir tous les modules jusqu'a ce qu'on trouve - foreach ($modules as $module) { - $var = "i18n_".$module."_".$spip_lang; - if (empty($GLOBALS[$var])) { - charger_langue($spip_lang, $module); - - // surcharge perso -- on cherche (lang/)local_xx.php ... - if ($f = chercher_module_lang('local', $spip_lang)) - surcharger_langue($f); - // ... puis (lang/)local.php - if ($f = chercher_module_lang('local')) - surcharger_langue($f); - } - if (isset($GLOBALS[$var][$code])) { - $text = $GLOBALS[$var][$code]; - break; - } - } - - // fallback langues pas finies ou en retard (eh oui, c'est moche...) - if ($spip_lang<>'fr') { - $text = ereg_replace("^<(NEW|MODIF)>","",$text); - if (!$text) { - $spip_lang_temp = $spip_lang; - $spip_lang = 'fr'; - $text = traduire_chaine($code_ori); - $spip_lang = $spip_lang_temp; - } - } - return $text; -} - - // http://doc.spip.org/@traduire_nom_langue function traduire_nom_langue($lang) { include_spip('inc/lang_liste'); @@ -414,18 +281,22 @@ function utiliser_langue_site() { // http://doc.spip.org/@utiliser_langue_visiteur function utiliser_langue_visiteur() { - global $_COOKIE; - if (!regler_langue_navigateur()) - utiliser_langue_site(); + $l = (_DIR_RESTREINT ? 'spip_lang' : 'spip_lang_ecrire'); + if (isset($_COOKIE[$l])) + if (changer_langue($l = $_COOKIE[$l])) return $l; - if (!empty($GLOBALS['auteur_session']['lang'])) - changer_langue($GLOBALS['auteur_session']['lang']); + if (isset($GLOBALS['auteur_session']['lang'])) + if (changer_langue($l = $GLOBALS['auteur_session']['lang'])) + return $l; - $cookie_lang = (_DIR_RESTREINT ? 'spip_lang' : 'spip_lang_ecrire'); - if (!empty($_COOKIE[$cookie_lang])) + foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $s) { + if (eregi('^([a-z]{2,3})(-[a-z]{2,3})?(;q=[0-9.]+)?$', trim($s), $r)) { + if (changer_langue($l=strtolower($r[1]))) return $l; + } + } - changer_langue($_COOKIE[$cookie_lang]); + return changer_langue($GLOBALS['langue_site']); } // Une fonction qui donne le repertoire ou trouver des fichiers de langue diff --git a/ecrire/inc/traduire.php b/ecrire/inc/traduire.php new file mode 100644 index 0000000000..e705086ed4 --- /dev/null +++ b/ecrire/inc/traduire.php @@ -0,0 +1,121 @@ +<?php + +/***************************************************************************\ + * SPIP, Systeme de publication pour l'internet * + * * + * Copyright (c) 2001-2006 * + * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * + * * + * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * + * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * +\***************************************************************************/ + +if (!defined("_ECRIRE_INC_VERSION")) return; + +// +// Charger un fichier langue +// +// http://doc.spip.org/@chercher_module_lang +function chercher_module_lang($module, $lang = '') { + if ($lang) + $lang = '_'.$lang; + + // 1) dans un repertoire nomme lang/ se trouvant sur le chemin + if ($f = include_spip('lang/'.$module.$lang, false)) + return $f; + + // 2) directement dans le chemin (old style) + return include_spip($module.$lang, false); +} + +// http://doc.spip.org/@charger_langue +function charger_langue($lang, $module = 'spip') { + if ($lang AND $fichier_lang = chercher_module_lang($module, $lang)) { + $GLOBALS['idx_lang']='i18n_'.$module.'_'.$lang; + include_once($fichier_lang); + } else { + // si le fichier de langue du module n'existe pas, on se rabat sur + // la langue par defaut du site -- et au pire sur le francais, qui + // *par definition* doit exister, et on copie le tableau dans la + // var liee a la langue + $l = $GLOBALS['meta']['langue_site']; + if (!$fichier_lang = chercher_module_lang($module, $l)) + $fichier_lang = chercher_module_lang($module, 'fr'); + + if ($fichier_lang) { + $GLOBALS['idx_lang']='i18n_'.$module.'_' .$l; + include($fichier_lang); + $GLOBALS['i18n_'.$module.'_'.$lang] + = &$GLOBALS['i18n_'.$module.'_'.$l]; + #spip_log("module de langue : ${module}_$l.php"); + } + } +} + +// +// Surcharger le fichier de langue courant avec un autre (tordu, hein...) +// +// http://doc.spip.org/@surcharger_langue +function surcharger_langue($fichier) { + + $idx_lang_normal = $GLOBALS['idx_lang']; + $idx_lang_surcharge = $GLOBALS['idx_lang'].'_temporaire'; + $GLOBALS['idx_lang'] = $idx_lang_surcharge; + include($fichier); + if (is_array($GLOBALS[$idx_lang_surcharge])) { + $GLOBALS[$idx_lang_normal] = array_merge( + $GLOBALS[$idx_lang_normal], + $GLOBALS[$idx_lang_surcharge] + ); + } + unset ($GLOBALS[$idx_lang_surcharge]); + $GLOBALS['idx_lang'] = $idx_lang_normal; +} + +// +// Traduire une chaine internationalisee +// +// http://doc.spip.org/@traduire_chaine +function inc_traduire_dist($ori, $lang) { + + static $deja_vu = array(); + + if (isset($deja_vu[$lang][$ori])) { + return $deja_vu[$lang][$ori]; + } + + // modules demandes explicitement + + if (preg_match(",^([a-z/]+):(.*)$,", $ori, $regs)) { + $modules = explode("/",$regs[1]); + $code = $regs[2]; + } else {$modules = array('spip', 'ecrire'); $code = $ori;} + + $text = ''; + // parcourir tous les modules jusqu'a ce qu'on trouve + foreach ($modules as $module) { + $var = "i18n_".$module."_".$lang; + if (empty($GLOBALS[$var])) { + charger_langue($lang, $module); + + // surcharge perso -- on cherche (lang/)local_xx.php ... + if ($f = chercher_module_lang('local', $lang)) + surcharger_langue($f); + // ... puis (lang/)local.php + if ($f = chercher_module_lang('local')) + surcharger_langue($f); + } + if (isset($GLOBALS[$var][$code])) { + $text = $GLOBALS[$var][$code]; + break; + } + } + + // filet pour traduction pas finies + if (($lang<>'fr') AND ereg("^<(NEW|MODIF)>", $text)) + $text = traduire_chaine($ori, 'fr'); + $deja_vu[$lang][$code] = $text; + + return $text; +} +?> diff --git a/ecrire/inc/utils.php b/ecrire/inc/utils.php index 1629249821..30ab41fd74 100644 --- a/ecrire/inc/utils.php +++ b/ecrire/inc/utils.php @@ -25,22 +25,22 @@ include(_DIR_RESTREINT . 'inc/vieilles_defs.php'); // charge un fichier perso ou, a defaut, standard // et retourne si elle existe le nom de la fonction homonyme (exec_$nom), // ou de suffixe _dist +// Peut etre appelee plusieurs fois, donc optimiser // http://doc.spip.org/@charger_fonction function charger_fonction($nom, $dossier='exec', $continue=false) { - // Securite de base - if (!preg_match(',^\w+$,', $nom)) - die(htmlspecialchars($nom)." pas autorise"); - if (substr($dossier,-1) != '/') $dossier .= '/'; - // Si la fonction existe deja (definie par mes_options, par exemple) if (function_exists($f = str_replace('/','_',$dossier) . $nom)) return $f; if (function_exists($g = $f . '_dist')) return $g; - // Sinon charger le fichier de declaration + // Sinon charger le fichier de declaration si plausible + + if (!preg_match(',^\w+$,', $f)) + die(htmlspecialchars($nom)." pas autorise"); + // passer en minuscules (cf les balises de formulaires) $inc = include_spip($d = ($dossier . strtolower($nom))); @@ -380,10 +380,9 @@ function self($root = false) { // // http://doc.spip.org/@_T function _T($texte, $args=array()) { - # petite optimisation pour ne passer qu'une fois dans include_spip - static $c; $c OR $c = include_spip('inc/lang'); - $text = traduire_chaine($texte); + $f = charger_fonction('traduire', 'inc'); + $text = $f($texte,$GLOBALS['spip_lang']); if (!empty($GLOBALS['xhtml'])) { include_spip('inc/charsets'); -- GitLab