3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette dynamic translations controller
10 * Copyright © 2020 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Controllers
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-02
38 namespace Galette\Controllers
;
40 use Slim\Http\Request
;
41 use Slim\Http\Response
;
42 use Galette\Core\L10n
;
46 * Galette dynamic translations controller
48 * @category Controllers
49 * @name DynamicTranslationsController
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2020 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9.4dev - 2020-05-02
58 class DynamicTranslationsController
extends AbstractController
61 * Dynamic fields translations
63 * @param Request $request PSR Request
64 * @param Response $response PSR Response
65 * @param array $args Request arguments
69 public function dynamicTranslations(Request
$request, Response
$response, array $args = []): Response
72 if (isset($args['text_orig'])) {
73 $text_orig = $args['text_orig'];
74 } elseif (isset($_GET['text_orig'])) {
75 $text_orig = $_GET['text_orig'];
79 'page_title' => _T("Translate labels")
84 $select = $this->zdb
->select(L10n
::TABLE
);
86 array('nb' => new \Laminas\Db\Sql\
Expression('COUNT(text_orig)'))
88 $results = $this->zdb
->execute($select);
89 $result = $results->current();
90 $nb_fields = $result->nb
;
91 } catch (\Exception
$e) {
93 'An error occurred counting l10n entries | ' .
99 if (is_numeric($nb_fields) && $nb_fields > 0) {
101 $select = $this->zdb
->select(L10n
::TABLE
);
102 $select->quantifier('DISTINCT')->columns(
104 )->order('text_orig');
106 $all_texts = $this->zdb
->execute($select);
109 foreach ($all_texts as $idx => $row) {
110 $orig[] = $row->text_orig
;
113 if ($text_orig == '') {
114 $text_orig = $orig[0];
115 } elseif (!in_array($text_orig, $orig)) {
117 $this->flash
->addMessage(
122 _T("No translation for '%s'!<br/>Please fill and submit above form to create it.")
129 * FIXME : it would be faster to get all translations at once
130 * for a specific string
132 foreach ($this->i18n
->getList() as $l) {
133 $text_trans = \
getDynamicTranslation($text_orig, $l->getLongID());
134 $lang_name = $l->getName();
136 'key' => $l->getLongID(),
137 'name' => ucwords($lang_name),
138 'text' => $text_trans
142 $params['exists'] = $exists;
143 $params['orig'] = $orig;
144 $params['trans'] = $trans;
145 } catch (\Exception
$e) {
147 'An error occurred retrieving l10n entries | ' .
154 $params['text_orig'] = $text_orig;
159 'traduire_libelles.tpl',
166 * Do dynamic fields translations
168 * @param Request $request PSR Request
169 * @param Response $response PSR Response
173 public function doDynamicTranslations(Request
$request, Response
$response): Response
175 $post = $request->getParsedBody();
176 $error_detected = false;
178 if (isset($post['trans']) && isset($post['text_orig'])) {
179 if (isset($_POST['new']) && $_POST['new'] == 'true') {
180 //create translation if it does not exists yet
181 $res = \addDynamicTranslation
(
187 foreach ($post as $key => $value) {
188 if (substr($key, 0, 11) == 'text_trans_') {
189 $trans_lang = substr($key, 11);
190 $trans_lang = str_replace('_utf8', '.utf8', $trans_lang);
191 $res = \
updateDynamicTranslation(
197 $error_detected = true;
198 $this->flash
->addMessage(
209 _T("An error occurred saving label `%label` for language `%lang`")
216 if ($error_detected === false) {
217 $this->flash
->addMessage(
219 _T("Labels has been sucessfully translated!")
226 ->withHeader('Location', $this->router
->pathFor(
227 'dynamicTranslations',
228 ['text_orig' => $post['text_orig']]