[SPIP] +2.1.12
[velocampus/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-2011 *
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 // Charger un fichier langue
17 //
18 // http://doc.spip.org/@chercher_module_lang
19 function chercher_module_lang($module, $lang = '') {
20 if ($lang)
21 $lang = '_'.$lang;
22
23 // 1) dans un repertoire nomme lang/ se trouvant sur le chemin
24 if ($f = find_in_path($module.$lang.'.php', 'lang/'))
25 return $f;
26
27 // 2) directement dans le chemin (old style, uniquement pour local)
28 return ($module == 'local')
29 ? find_in_path('local'.$lang. '.php')
30 : '';
31 }
32
33 // http://doc.spip.org/@charger_langue
34 function charger_langue($lang, $module = 'spip') {
35 if ($lang AND $fichier_lang = chercher_module_lang($module, $lang)) {
36 $GLOBALS['idx_lang']='i18n_'.$module.'_'.$lang;
37 include($fichier_lang);
38 } else {
39 // si le fichier de langue du module n'existe pas, on se rabat sur
40 // la langue par defaut du site -- et au pire sur le francais, qui
41 // *par definition* doit exister, et on copie le tableau dans la
42 // var liee a la langue
43 $l = $GLOBALS['meta']['langue_site'];
44 if (!$fichier_lang = chercher_module_lang($module, $l))
45 $fichier_lang = chercher_module_lang($module, 'fr');
46
47 if ($fichier_lang) {
48 $GLOBALS['idx_lang']='i18n_'.$module.'_' .$l;
49 include($fichier_lang);
50 $GLOBALS['i18n_'.$module.'_'.$lang]
51 = &$GLOBALS['i18n_'.$module.'_'.$l];
52 #spip_log("module de langue : ${module}_$l.php");
53 }
54 }
55 }
56
57 //
58 // Surcharger le fichier de langue courant avec un autre (tordu, hein...)
59 //
60 // http://doc.spip.org/@surcharger_langue
61 function surcharger_langue($fichier) {
62 static $surcharges = array();
63 if (!isset($GLOBALS['idx_lang'])) return;
64 if (!isset($surcharges[$fichier])) {
65 $idx_lang_normal = $GLOBALS['idx_lang'];
66 $GLOBALS['idx_lang'] = $GLOBALS['idx_lang'].'@temporaire';
67 include($fichier);
68 $surcharges[$fichier] = $GLOBALS[$GLOBALS['idx_lang']];
69 unset ($GLOBALS[$GLOBALS['idx_lang']]);
70 $GLOBALS['idx_lang'] = $idx_lang_normal;
71 }
72 if (is_array($surcharges[$fichier])) {
73 $GLOBALS[$GLOBALS['idx_lang']] = array_merge(
74 (array)$GLOBALS[$GLOBALS['idx_lang']],
75 $surcharges[$fichier]
76 );
77 }
78 }
79
80 //
81 // Traduire une chaine internationalisee
82 //
83 // http://doc.spip.org/@inc_traduire_dist
84 function inc_traduire_dist($ori, $lang) {
85
86 static $deja_vu = array();
87
88 if (isset($deja_vu[$lang][$ori]))
89 return $deja_vu[$lang][$ori];
90
91 // modules demandes explicitement <xxx/yyy/zzz:code>
92 if (strpos($ori,':')) {
93 list($modules,$code) = explode(':',$ori,2);
94 $modules = explode('/', $modules);
95 } else {
96 $modules = array('spip', 'ecrire');
97 $code = $ori;
98 }
99
100 $text = '';
101 // parcourir tous les modules jusqu'a ce qu'on trouve
102 foreach ($modules as $module) {
103 $var = "i18n_".$module."_".$lang;
104 if (empty($GLOBALS[$var])) {
105 charger_langue($lang, $module);
106
107 // surcharge perso -- on cherche (lang/)local_xx.php ...
108 if ($f = chercher_module_lang('local', $lang))
109 surcharger_langue($f);
110 // ... puis (lang/)local.php
111 if ($f = chercher_module_lang('local'))
112 surcharger_langue($f);
113 }
114 if (isset($GLOBALS[$var][$code])) {
115 $text = $GLOBALS[$var][$code];
116 break;
117 }
118 }
119
120 // Retour aux sources si la chaine est absente dans la langue cible ;
121 // on essaie d'abord la langue du site, puis a defaut la langue fr
122 if (!strlen($text)
123 AND $lang !== 'fr') {
124 if ($lang !== $GLOBALS['meta']['langue_site'])
125 $text = inc_traduire_dist($ori, $GLOBALS['meta']['langue_site']);
126 else
127 $text = inc_traduire_dist($ori, 'fr');
128 }
129
130 // Supprimer la mention <NEW> ou <MODIF>
131 if (substr($text,0,1) === '<')
132 $text = str_replace(array('<NEW>', '<MODIF>'), array(), $text);
133
134 // Si on n'est pas en utf-8, la chaine peut l'etre...
135 // le cas echeant on la convertit en entites html &#xxx;
136 if ($GLOBALS['meta']['charset'] !== 'utf-8'
137 AND preg_match(',[\x7f-\xff],S', $text)) {
138 include_spip('inc/charsets');
139 $text = charset2unicode($text,'utf-8');
140 }
141
142 $deja_vu[$lang][$ori] = $text;
143
144 return $text;
145 }
146 ?>