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