3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
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]) AND (_request('var_mode') != 'traduction'))
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);
130 $modules = array('spip', 'ecrire');
132 $ori_complet = implode('|', $modules) . ':' . $ori;
136 // parcourir tous les modules jusqu'a ce qu'on trouve
137 foreach ($modules as $module) {
138 $var = "i18n_".$module."_".$lang;
140 if (empty($GLOBALS[$var])) {
141 charger_langue($lang, $module);
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');
152 surcharger_langue($local['local']);
155 if (isset($GLOBALS[$var][$code])) {
156 $module_retenu = $module;
157 $text = $GLOBALS[$var][$code];
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;
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'] : '');
172 $text = inc_traduire_dist($ori, 'fr');
173 $langue_retenue = (!strlen($text) ?
'fr' : '');
177 // Supprimer la mention <NEW> ou <MODIF>
178 if (substr($text,0,1) === '<')
179 $text = str_replace(array('<NEW>', '<MODIF>'), array(), $text);
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');
189 if (_request('var_mode') == 'traduction') {
191 $classe = 'debug-traduction' . ($module_retenu == 'ecrire' ?
'-prive' : '');
192 $text = '<span lang=' . $langue_retenue . ' class=' . $classe . ' title=' . $ori_complet . '(' . $langue_retenue . ')>' . $text . '</span>';
194 array("$module_retenu:", "$module_retenu|"),
195 array("*$module_retenu*:", "*$module_retenu*|"),
200 $deja_vu[$lang][$ori] = $text;