]> git.agnieray.net Git - galette.git/blob - galette/includes/i18n.inc.php
a2dd4cdb109702206c3a9b6f72c7c191114f3cac
[galette.git] / galette / includes / i18n.inc.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * i18n functions
7 *
8 * PHP version 5
9 *
10 * Copyright © 2003-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Main
28 * @package Galette
29 *
30 * @author Frédéric Jaqcuot <unknown@unknow.com>
31 * @author Georges Khaznadar (i18n using gettext) <unknown@unknow.com>
32 * @author Johan Cwiklinski <johan@x-tnd.be>
33 * @copyright 2003-2014 The Galette Team
34 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
35 * @version SVN: $Id$
36 * @link http://galette.tuxfamily.org
37 * @since Available since 0.62
38 */
39
40 if (!defined('GALETTE_ROOT')) {
41 die("Sorry. You can't access directly to this file");
42 }
43
44 use Analog\Analog;
45 use Zend\Db\Sql\Expression;
46 use Galette\Core\L10n;
47
48 $i18n->updateEnv();
49 $language = $i18n->getLongID();
50
51 /**
52 * Add a translation stored in the database
53 *
54 * @param string $text_orig Text to translate
55 *
56 * @return boolean
57 */
58 function addDynamicTranslation($text_orig)
59 {
60 global $zdb, $i18n;
61
62 try {
63 foreach ($i18n->getList() as $lang) {
64 //check if translation already exists
65 $select = $zdb->select(L10n::TABLE);
66 $select->columns(array('text_nref'))
67 ->where(
68 array(
69 'text_orig' => $text_orig,
70 'text_locale' => $lang->getLongID()
71 )
72 );
73
74 $results = $zdb->execute($select);
75 $result = $results->current();
76 $nref = 0;
77 if ($result) {
78 $nref = $result->text_nref;
79 }
80
81 if (is_numeric($nref) && $nref > 0) {
82 //already existing, update
83 $values = array(
84 'text_nref' => new Expression('text_nref+1')
85 );
86 Analog::log(
87 'Entry for `' . $text_orig .
88 '` dynamic translation already exists.',
89 Analog::INFO
90 );
91
92 $where = array();
93 $owhere = $select->where;
94
95 $update = $zdb->update(L10n::TABLE);
96 $update->set($values)->where($owhere);
97 $zdb->execute($update);
98 } else {
99 //add new entry
100 // User is supposed to use current language as original text.
101 $text_trans = $text_orig;
102 if ($lang->getLongID() != $i18n->getLongID()) {
103 $text_trans = '';
104 }
105 $values = array(
106 'text_orig' => $text_orig,
107 'text_locale' => $lang->getLongID(),
108 'text_trans' => $text_trans
109 );
110
111 $insert = $zdb->insert(L10n::TABLE);
112 $insert->values($values);
113 $zdb->execute($insert);
114 }
115 }
116 return true;
117 } catch (\Exception $e) {
118 Analog::log(
119 'An error occurred adding dynamic translation for `' .
120 $text_orig . '` | ' . $e->getMessage(),
121 Analog::ERROR
122 );
123 return false;
124 }
125 }
126
127 /**
128 * Delete a translation stored in the database
129 *
130 * @param string $text_orig Text to translate
131 *
132 * @return boolean
133 */
134 function deleteDynamicTranslation($text_orig)
135 {
136 global $zdb, $i18n;
137
138 try {
139 $delete = $zdb->delete(L10n::TABLE);
140 $delete->where(
141 array(
142 'text_orig' => $text_orig,
143 'text_locale' => ':lang_id'
144 )
145 );
146 $stmt = $zdb->sql->prepareStatementForSqlObject($delete);
147
148 foreach ($i18n->getList() as $lang) {
149 $stmt->execute(
150 array(
151 'where2' => $lang->getLongID()
152 )
153 );
154 }
155 return true;
156 } catch (Exception $e) {
157 Analog::log(
158 'An error occurred deleting dynamic translation for `' .
159 $text_orig . '` (lang `' . $lang->getLongID() . '`) | ' .
160 $e->getMessage(),
161 Analog::ERROR
162 );
163 return false;
164 }
165 }
166
167 /**
168 * Update a translation stored in the database
169 *
170 * @param string $text_orig Text to translate
171 * @param string $text_locale The locale
172 * @param string $text_trans Translated text
173 *
174 * @return boolean
175 */
176 function updateDynamicTranslation($text_orig, $text_locale, $text_trans)
177 {
178 global $zdb;
179
180 try {
181 //check if translation already exists
182 $select = $zdb->select(L10n::TABLE);
183 $select->columns(array('text_nref'))->where(
184 array(
185 'text_orig' => $text_orig,
186 'text_locale' => $text_locale
187 )
188 );
189
190 $results = $zdb->execute($select);
191 $result = $results->current();
192
193 $exists = false;
194 if ($result) {
195 $nref = $result->text_nref;
196 $exists = (is_numeric($nref) && $nref > 0);
197 }
198
199 $values = array(
200 'text_trans' => $text_trans
201 );
202
203 if ($exists) {
204 $where = array();
205 $owhere = $select->where;
206
207 $update = $zdb->update(L10n::TABLE);
208 $update->set($values)->where($owhere);
209 $zdb->execute($update);
210 } else {
211 $values['text_orig'] = $text_orig;
212 $values['text_locale'] = $text_locale;
213
214 $insert = $zdb->insert(L10n::TABLE);
215 $insert->values($values);
216 $zdb->execute($insert);
217 }
218 return true;
219 } catch (Exception $e) {
220 Analog::log(
221 'An error occurred updating dynamic translation for `' .
222 $text_orig . '` | ' . $e->getMessage(),
223 Analog::ERROR
224 );
225 return false;
226 }
227 }
228
229 /**
230 * Get a translation stored in the database
231 *
232 * @param string $text_orig Text to translate
233 * @param string $text_locale The locale
234 *
235 * @return translated string
236 */
237 function getDynamicTranslation($text_orig, $text_locale)
238 {
239 global $zdb;
240 try {
241 $select = $zdb->select(L10n::TABLE);
242 $select->limit(1)->columns(
243 array('text_trans')
244 )->where(
245 array(
246 'text_orig' => $text_orig,
247 'text_locale' => $text_locale
248 )
249 );
250 $results = $zdb->execute($select);
251 if ($results->count() > 0) {
252 $res = $results->current();
253 return $res->text_trans;
254 } else {
255 return;
256 }
257 } catch (Exception $e) {
258 Analog::log(
259 'An error occurred retrieving l10n entry. text_orig=' . $text_orig .
260 ', text_locale=' . $text_locale . ' | ' . $e->getMessage(),
261 Analog::WARNING
262 );
263 return false;
264 }
265 }
266
267 /**
268 * Translate a string
269 *
270 * @param string $string The string to translate
271 * @param string $domain Translation domain. Default to false (will take default domain)
272 * @param boolean $nt Indicate not translated strings; defaults to true
273 *
274 * @return Translated string (if available) ; $chaine otherwise
275 */
276 function _T($string, $domain = 'galette', $nt = true)
277 {
278 global $language, $installer, $translator;
279
280 if (strpos($domain, 'route') !== false) {
281 Analog::log(
282 'Routes are no longer translated, return string.',
283 Analog::DEBUG
284 );
285 return $string;
286 }
287
288 if ($translator->translationExists($string, $domain)) {
289 return $translator->translate($string, $domain);
290 }
291
292 $trans = false;
293 if (!isset($installer) || $installer !== true) {
294 $trans = getDynamicTranslation(
295 $string,
296 $language
297 );
298 }
299
300 if (!$trans) {
301 $trans = $string;
302
303 if ($nt === true) {
304 $trans .= ' (not translated)';
305 }
306 }
307 return $trans;
308 }
309
310 /**
311 * Translate a string, without displaying not translated
312 *
313 * @param string $string The string to translate
314 * @param string $domain Translation domain. Default to false (will take default domain)
315 *
316 * @return Translated string (if available), verbatim string otherwise
317 */
318 function __($string, $domain = 'galette')
319 {
320 return _T($string, $domain, false);
321 }
322
323 /**********************************************
324 * some constant strings found in the database *
325 **********************************************/
326 /** TODO: these string should be not be handled here */
327 $foo = _T("Realization:");
328 $foo = _T("Graphics:");
329 $foo = _T("Publisher:");
330 $foo = _T("President");
331 $foo = _T("Vice-president");
332 $foo = _T("Treasurer");
333 $foo = _T("Vice-treasurer");
334 $foo = _T("Secretary");
335 $foo = _T("Vice-secretary");
336 $foo = _T("Active member");
337 $foo = _T("Benefactor member");
338 $foo = _T("Founder member");
339 $foo = _T("Old-timer");
340 $foo = _T("Legal entity");
341 $foo = _T("Non-member");
342 $foo = _T("Reduced annual contribution");
343 $foo = _T("Company cotisation");
344 $foo = _T("Donation in kind");
345 $foo = _T("Donation in money");
346 $foo = _T("Partnership");
347 $foo = _T("french");
348 $foo = _T("english");
349 $foo = _T("spanish");
350 $foo = _T("annual fee");
351 $foo = _T("annual fee (to be paid)");
352 $foo = _T("company fee");
353 $foo = _T("donation in kind");
354 $foo = _T("donation in money");
355 $foo = _T("partnership");
356 $foo = _T("reduced annual fee");
357 $foo = _T("Identity");
358 $foo = _T("Galette-related data");
359 $foo = _T("Contact information");
360 $foo = _T("Mr.");
361 $foo = _T("Mrs.");
362 $foo = _T("Miss");
363 $foo = _T("Identity:");
364 $foo = _T("Galette-related data:");
365 $foo = _T("Contact information:");