3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2012 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
16 * Rechercher tous les lang/file dans le path
17 * qui seront ensuite charges dans l'ordre du path
18 * version dediee et optimisee pour cet usage de find_in_path
20 * @staticvar <type> $dirs
22 * @param <type> $dirname
25 function find_langs_in_path ($file, $dirname='lang') {
28 foreach(creer_chemin() as $dir) {
29 if (!isset($dirs[$a = $dir . $dirname]))
30 $dirs[$a] = (is_dir($a) ||
!$a) ;
32 if (is_readable($a .= $file)) {
37 return array_reverse($liste);
40 // Charger un fichier langue
42 // http://doc.spip.org/@chercher_module_lang
43 function chercher_module_lang($module, $lang = '') {
47 // 1) dans un repertoire nomme lang/ se trouvant sur le chemin
48 if ($f = ($module == 'local'
49 ?
find_in_path($module.$lang.'.php', 'lang/')
50 : find_langs_in_path($module.$lang.'.php', 'lang/')))
51 return is_array($f)?
$f:array($f);
53 // 2) directement dans le chemin (old style, uniquement pour local)
54 return (($module == 'local') OR strpos($module, '/'))
55 ?
(($f = find_in_path($module.$lang. '.php')) ?
array($f):false)
59 // http://doc.spip.org/@charger_langue
60 function charger_langue($lang, $module = 'spip') {
61 if ($lang AND $fichiers_lang = chercher_module_lang($module, $lang)) {
62 $GLOBALS['idx_lang']='i18n_'.$module.'_'.$lang;
63 include(array_shift($fichiers_lang));
64 surcharger_langue($fichiers_lang);
66 // si le fichier de langue du module n'existe pas, on se rabat sur
67 // la langue par defaut du site -- et au pire sur le francais, qui
68 // *par definition* doit exister, et on copie le tableau dans la
69 // var liee a la langue
70 $l = $GLOBALS['meta']['langue_site'];
71 if (!$fichiers_lang = chercher_module_lang($module, $l))
72 $fichiers_lang = chercher_module_lang($module, 'fr');
75 $GLOBALS['idx_lang']='i18n_'.$module.'_' .$l;
76 include(array_shift($fichiers_lang));
77 surcharger_langue($fichiers_lang);
78 $GLOBALS['i18n_'.$module.'_'.$lang]
79 = &$GLOBALS['i18n_'.$module.'_'.$l];
80 #spip_log("module de langue : ${module}_$l.php");
86 // Surcharger le fichier de langue courant avec un ou plusieurs autre (tordu, hein...)
88 // http://doc.spip.org/@surcharger_langue
89 function surcharger_langue($fichiers) {
90 static $surcharges = array();
91 if (!isset($GLOBALS['idx_lang'])) return;
93 if (!is_array($fichiers)) $fichiers = array($fichiers);
94 if (!count($fichiers)) return;
95 foreach($fichiers as $fichier){
96 if (!isset($surcharges[$fichier])) {
97 $idx_lang_normal = $GLOBALS['idx_lang'];
98 $GLOBALS['idx_lang'] = $GLOBALS['idx_lang'].'@temporaire';
100 $surcharges[$fichier] = $GLOBALS[$GLOBALS['idx_lang']];
101 unset ($GLOBALS[$GLOBALS['idx_lang']]);
102 $GLOBALS['idx_lang'] = $idx_lang_normal;
104 if (is_array($surcharges[$fichier])) {
105 $GLOBALS[$GLOBALS['idx_lang']] = array_merge(
106 (array)$GLOBALS[$GLOBALS['idx_lang']],
107 $surcharges[$fichier]
114 // Traduire une chaine internationalisee
116 // http://doc.spip.org/@inc_traduire_dist
117 function inc_traduire_dist($ori, $lang) {
118 static $deja_vu = array();
119 static $local = array();
121 if (isset($deja_vu[$lang][$ori]))
122 return $deja_vu[$lang][$ori];
124 // modules demandes explicitement <xxx|yyy|zzz:code> cf MODULES_IDIOMES
125 if (strpos($ori,':')) {
126 list($modules,$code) = explode(':',$ori,2);
127 $modules = explode('|', $modules);
129 $modules = array('spip', 'ecrire');
134 // parcourir tous les modules jusqu'a ce qu'on trouve
135 foreach ($modules as $module) {
136 $var = "i18n_".$module."_".$lang;
137 if (empty($GLOBALS[$var])) {
138 charger_langue($lang, $module);
140 // surcharge perso -- on cherche (lang/)local_xx.php ...
141 if (!isset($local['local_'.$lang]))
142 $local['local_'.$lang] = chercher_module_lang('local', $lang);
143 if ($local['local_'.$lang])
144 surcharger_langue($local['local_'.$lang]);
145 // ... puis (lang/)local.php
146 if (!isset($local['local']))
147 $local['local'] = chercher_module_lang('local');
149 surcharger_langue($local['local']);
151 if (isset($GLOBALS[$var][$code])) {
152 $text = $GLOBALS[$var][$code];
157 // Retour aux sources si la chaine est absente dans la langue cible ;
158 // on essaie d'abord la langue du site, puis a defaut la langue fr
160 AND $lang !== 'fr') {
161 if ($lang !== $GLOBALS['meta']['langue_site'])
162 $text = inc_traduire_dist($ori, $GLOBALS['meta']['langue_site']);
164 $text = inc_traduire_dist($ori, 'fr');
167 // Supprimer la mention <NEW> ou <MODIF>
168 if (substr($text,0,1) === '<')
169 $text = str_replace(array('<NEW>', '<MODIF>'), array(), $text);
171 // Si on n'est pas en utf-8, la chaine peut l'etre...
172 // le cas echeant on la convertit en entites html &#xxx;
173 if ($GLOBALS['meta']['charset'] !== 'utf-8'
174 AND preg_match(',[\x7f-\xff],S', $text)) {
175 include_spip('inc/charsets');
176 $text = charset2unicode($text,'utf-8');
179 $deja_vu[$lang][$ori] = $text;