[SPIP] ~maj v3.0.14-->v3.0.17
[ptitvelo/web/www.git] / www / ecrire / inc / traduire.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
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 /**
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
19 *
20 * @staticvar <type> $dirs
21 * @param <type> $file
22 * @param <type> $dirname
23 * @return <type>
24 */
25 function find_langs_in_path ($file, $dirname='lang') {
26 static $dirs=array();
27 $liste = array();
28 foreach(creer_chemin() as $dir) {
29 if (!isset($dirs[$a = $dir . $dirname]))
30 $dirs[$a] = (is_dir($a) || !$a) ;
31 if ($dirs[$a]) {
32 if (is_readable($a .= $file)) {
33 $liste[] = $a;
34 }
35 }
36 }
37 return array_reverse($liste);
38 }
39 //
40 // Charger un fichier langue
41 //
42 // http://doc.spip.org/@chercher_module_lang
43 function chercher_module_lang($module, $lang = '') {
44 if ($lang)
45 $lang = '_'.$lang;
46
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);
52
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)
56 : false;
57 }
58
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);
65 } else {
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');
73
74 if ($fichiers_lang) {
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");
81 }
82 }
83 }
84
85 //
86 // Surcharger le fichier de langue courant avec un ou plusieurs autre (tordu, hein...)
87 //
88 // http://doc.spip.org/@surcharger_langue
89 function surcharger_langue($fichiers) {
90 static $surcharges = array();
91 if (!isset($GLOBALS['idx_lang'])) return;
92
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';
99 include($fichier);
100 $surcharges[$fichier] = $GLOBALS[$GLOBALS['idx_lang']];
101 unset ($GLOBALS[$GLOBALS['idx_lang']]);
102 $GLOBALS['idx_lang'] = $idx_lang_normal;
103 }
104 if (is_array($surcharges[$fichier])) {
105 $GLOBALS[$GLOBALS['idx_lang']] = array_merge(
106 (array)$GLOBALS[$GLOBALS['idx_lang']],
107 $surcharges[$fichier]
108 );
109 }
110 }
111 }
112
113 //
114 // Traduire une chaine internationalisee
115 //
116 // http://doc.spip.org/@inc_traduire_dist
117 function inc_traduire_dist($ori, $lang) {
118 static $deja_vu = array();
119 static $local = array();
120
121 if (isset($deja_vu[$lang][$ori]) AND (_request('var_mode') != 'traduction'))
122 return $deja_vu[$lang][$ori];
123
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);
128 $ori_complet = $ori;
129 } else {
130 $modules = array('spip', 'ecrire');
131 $code = $ori;
132 $ori_complet = implode('|', $modules) . ':' . $ori;
133 }
134
135 $text = '';
136 // parcourir tous les modules jusqu'a ce qu'on trouve
137 foreach ($modules as $module) {
138 $var = "i18n_".$module."_".$lang;
139
140 if (empty($GLOBALS[$var])) {
141 charger_langue($lang, $module);
142
143 // surcharge perso -- on cherche (lang/)local_xx.php ...
144 if (!isset($local['local_'.$lang]))
145 $local['local_'.$lang] = chercher_module_lang('local', $lang);
146 if ($local['local_'.$lang])
147 surcharger_langue($local['local_'.$lang]);
148 // ... puis (lang/)local.php
149 if (!isset($local['local']))
150 $local['local'] = chercher_module_lang('local');
151 if ($local['local'])
152 surcharger_langue($local['local']);
153 }
154
155 if (isset($GLOBALS[$var][$code])) {
156 $module_retenu = $module;
157 $text = $GLOBALS[$var][$code];
158 break;
159 }
160 }
161
162 // Retour aux sources si la chaine est absente dans la langue cible ;
163 // on essaie d'abord la langue du site, puis a defaut la langue fr
164 $langue_retenue = $lang;
165 if (!strlen($text)
166 AND $lang !== 'fr') {
167 if ($lang !== $GLOBALS['meta']['langue_site']) {
168 $text = inc_traduire_dist($ori, $GLOBALS['meta']['langue_site']);
169 $langue_retenue = (!strlen($text) ? $GLOBALS['meta']['langue_site'] : '');
170 }
171 else {
172 $text = inc_traduire_dist($ori, 'fr');
173 $langue_retenue = (!strlen($text) ? 'fr' : '');
174 }
175 }
176
177 // Supprimer la mention <NEW> ou <MODIF>
178 if (substr($text,0,1) === '<')
179 $text = str_replace(array('<NEW>', '<MODIF>'), array(), $text);
180
181 // Si on n'est pas en utf-8, la chaine peut l'etre...
182 // le cas echeant on la convertit en entites html &#xxx;
183 if ($GLOBALS['meta']['charset'] !== 'utf-8'
184 AND preg_match(',[\x7f-\xff],S', $text)) {
185 include_spip('inc/charsets');
186 $text = charset2unicode($text,'utf-8');
187 }
188
189 if (_request('var_mode') == 'traduction') {
190 if ($text) {
191 $classe = 'debug-traduction' . ($module_retenu == 'ecrire' ? '-prive' : '');
192 $text = '<span lang=' . $langue_retenue . ' class=' . $classe . ' title=' . $ori_complet . '(' . $langue_retenue . ')>' . $text . '</span>';
193 $text = str_replace(
194 array("$module_retenu:", "$module_retenu|"),
195 array("*$module_retenu*:", "*$module_retenu*|"),
196 $text);
197 }
198 }
199 else {
200 $deja_vu[$lang][$ori] = $text;
201 }
202
203 return $text;
204 }
205 ?>