]> git.agnieray.net Git - galette.git/blob - galette/includes/i18n.inc.php
aa2cfef614cc9a7ef2150302b978ed9b266a3d11
[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 Laminas\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 $owhere = $select->where;
93
94 $update = $zdb->update(L10n::TABLE);
95 $update->set($values)->where($owhere);
96 $zdb->execute($update);
97 } else {
98 //add new entry
99 // User is supposed to use current language as original text.
100 $text_trans = $text_orig;
101 if ($lang->getLongID() != $i18n->getLongID()) {
102 $text_trans = '';
103 }
104 $values = array(
105 'text_orig' => $text_orig,
106 'text_locale' => $lang->getLongID(),
107 'text_trans' => $text_trans
108 );
109
110 $insert = $zdb->insert(L10n::TABLE);
111 $insert->values($values);
112 $zdb->execute($insert);
113 }
114 }
115 return true;
116 } catch (\Exception $e) {
117 Analog::log(
118 'An error occurred adding dynamic translation for `' .
119 $text_orig . '` | ' . $e->getMessage(),
120 Analog::ERROR
121 );
122 return false;
123 }
124 }
125
126 /**
127 * Delete a translation stored in the database
128 *
129 * @param string $text_orig Text to translate
130 *
131 * @return boolean
132 */
133 function deleteDynamicTranslation($text_orig)
134 {
135 global $zdb, $i18n;
136
137 try {
138 $delete = $zdb->delete(L10n::TABLE);
139 $delete->where(
140 array(
141 'text_orig' => $text_orig,
142 'text_locale' => ':lang_id'
143 )
144 );
145 $stmt = $zdb->sql->prepareStatementForSqlObject($delete);
146
147 foreach ($i18n->getList() as $lang) {
148 $stmt->execute(
149 array(
150 'where2' => $lang->getLongID()
151 )
152 );
153 }
154 return true;
155 } catch (Exception $e) {
156 Analog::log(
157 'An error occurred deleting dynamic translation for `' .
158 $text_orig . '` (lang `' . $lang->getLongID() . '`) | ' .
159 $e->getMessage(),
160 Analog::ERROR
161 );
162 return false;
163 }
164 }
165
166 /**
167 * Update a translation stored in the database
168 *
169 * @param string $text_orig Text to translate
170 * @param string $text_locale The locale
171 * @param string $text_trans Translated text
172 *
173 * @return boolean
174 */
175 function updateDynamicTranslation($text_orig, $text_locale, $text_trans)
176 {
177 global $zdb;
178
179 try {
180 //check if translation already exists
181 $select = $zdb->select(L10n::TABLE);
182 $select->columns(array('text_nref'))->where(
183 array(
184 'text_orig' => $text_orig,
185 'text_locale' => $text_locale
186 )
187 );
188
189 $results = $zdb->execute($select);
190 $result = $results->current();
191
192 $exists = false;
193 if ($result) {
194 $nref = $result->text_nref;
195 $exists = (is_numeric($nref) && $nref > 0);
196 }
197
198 $values = array(
199 'text_trans' => $text_trans
200 );
201
202 if ($exists) {
203 $owhere = $select->where;
204
205 $update = $zdb->update(L10n::TABLE);
206 $update->set($values)->where($owhere);
207 $zdb->execute($update);
208 } else {
209 $values['text_orig'] = $text_orig;
210 $values['text_locale'] = $text_locale;
211
212 $insert = $zdb->insert(L10n::TABLE);
213 $insert->values($values);
214 $zdb->execute($insert);
215 }
216 return true;
217 } catch (Exception $e) {
218 Analog::log(
219 'An error occurred updating dynamic translation for `' .
220 $text_orig . '` | ' . $e->getMessage(),
221 Analog::ERROR
222 );
223 return false;
224 }
225 }
226
227 /**
228 * Get a translation stored in the database
229 *
230 * @param string $text_orig Text to translate
231 * @param string $text_locale The locale
232 *
233 * @return string
234 */
235 function getDynamicTranslation($text_orig, $text_locale)
236 {
237 global $zdb;
238 try {
239 $select = $zdb->select(L10n::TABLE);
240 $select->limit(1)->columns(
241 array('text_trans')
242 )->where(
243 array(
244 'text_orig' => $text_orig,
245 'text_locale' => $text_locale
246 )
247 );
248 $results = $zdb->execute($select);
249 if ($results->count() > 0) {
250 $res = $results->current();
251 return $res->text_trans;
252 } else {
253 return;
254 }
255 } catch (Exception $e) {
256 Analog::log(
257 'An error occurred retrieving l10n entry. text_orig=' . $text_orig .
258 ', text_locale=' . $text_locale . ' | ' . $e->getMessage(),
259 Analog::WARNING
260 );
261 return false;
262 }
263 }
264
265 /**
266 * Translate a string, or return original one
267 *
268 * @param string $string The string to translate
269 * @param string $domain Translation domain. Default to false (will take default domain)
270 * @param boolean $nt Indicate not translated strings; defaults to true
271 *
272 * @return string
273 */
274 function _T($string, $domain = 'galette', $nt = true)
275 {
276 global $language, $installer, $translator;
277
278 if (strpos($domain, 'route') !== false) {
279 Analog::log(
280 'Routes are no longer translated, return string.',
281 Analog::DEBUG
282 );
283 return $string;
284 }
285
286 if ($translator->translationExists($string, $domain)) {
287 return $translator->translate($string, $domain);
288 }
289
290 $trans = false;
291 if (!isset($installer) || $installer !== true) {
292 $trans = getDynamicTranslation(
293 $string,
294 $language
295 );
296 }
297
298 if (!$trans) {
299 $trans = $string;
300
301 if (GALETTE_MODE == 'DEV' && $nt === true) {
302 $trans .= ' (not translated)';
303 }
304 }
305 return $trans;
306 }
307
308 /**
309 * Translate a string, without displaying not translated
310 *
311 * @param string $string The string to translate
312 * @param string $domain Translation domain. Default to false (will take default domain)
313 *
314 * @return string
315 */
316 function __($string, $domain = 'galette')
317 {
318 return _T($string, $domain, false);
319 }
320
321 /**********************************************
322 * some constant strings found in the database *
323 **********************************************/
324 /** TODO: these string should be not be handled here */
325 $foo = _T("Realization:");
326 $foo = _T("Graphics:");
327 $foo = _T("Publisher:");
328 $foo = _T("President");
329 $foo = _T("Vice-president");
330 $foo = _T("Treasurer");
331 $foo = _T("Vice-treasurer");
332 $foo = _T("Secretary");
333 $foo = _T("Vice-secretary");
334 $foo = _T("Active member");
335 $foo = _T("Benefactor member");
336 $foo = _T("Founder member");
337 $foo = _T("Old-timer");
338 $foo = _T("Legal entity");
339 $foo = _T("Non-member");
340 $foo = _T("Reduced annual contribution");
341 $foo = _T("Company cotisation");
342 $foo = _T("Donation in kind");
343 $foo = _T("Donation in money");
344 $foo = _T("Partnership");
345 $foo = _T("french");
346 $foo = _T("english");
347 $foo = _T("spanish");
348 $foo = _T("annual fee");
349 $foo = _T("annual fee (to be paid)");
350 $foo = _T("company fee");
351 $foo = _T("donation in kind");
352 $foo = _T("donation in money");
353 $foo = _T("partnership");
354 $foo = _T("reduced annual fee");
355 $foo = _T("Identity");
356 $foo = _T("Galette-related data");
357 $foo = _T("Contact information");
358 $foo = _T("Mr.");
359 $foo = _T("Mrs.");
360 $foo = _T("Miss");
361 $foo = _T("Identity:");
362 $foo = _T("Galette-related data:");
363 $foo = _T("Contact information:");
364 $foo = _T("Society");