|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Vvveb |
| 5 | + * |
| 6 | + * Copyright (C) 2022 Ziadin Givan |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +namespace Vvveb\Controller\Editor; |
| 24 | + |
| 25 | +use function Vvveb\__; |
| 26 | +use Vvveb\Controller\Base; |
| 27 | + |
| 28 | +class Translate extends Base { |
| 29 | + function get() { |
| 30 | + $text = $this->request->post['text']; |
| 31 | + $languages = \Vvveb\availableLanguages(); |
| 32 | + $translations = []; |
| 33 | + |
| 34 | + foreach ($languages as $lang) { |
| 35 | + $code = $lang['code']; |
| 36 | + \Vvveb\setLanguage($code); |
| 37 | + $translations[$code] = __($text); |
| 38 | + } |
| 39 | + //restore language |
| 40 | + \Vvveb\setLanguage(\Vvveb\getLanguage()); |
| 41 | + |
| 42 | + $this->response->setType('json'); |
| 43 | + $this->response->output($translations); |
| 44 | + } |
| 45 | + |
| 46 | + function save() { |
| 47 | + $translations = $this->request->post ?? []; |
| 48 | + $success = true; |
| 49 | + $message = __('Translations saved!'); |
| 50 | + |
| 51 | + if ($translations) { |
| 52 | + require_once DIR_SYSTEM . 'functions' . DS . 'php-mo.php'; |
| 53 | + |
| 54 | + $defaultLang = key($translations); //'en_US'; |
| 55 | + $domain = 'vvveb'; |
| 56 | + $text = $translations[$defaultLang]; |
| 57 | + |
| 58 | + foreach ($translations as $langCode => $translation) { |
| 59 | + if ($langCode == $defaultLang || $success == false) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + $folder = DIR_ROOT . 'locale' . DS . $langCode . DS . 'LC_MESSAGES' . DS; |
| 63 | + $userpoFile = $folder . 'user.po'; |
| 64 | + $poFile = $folder . $domain . '.po'; |
| 65 | + $moFile = $folder . $domain . '.mo'; |
| 66 | + |
| 67 | + foreach ([$poFile, $moFile] as $file) { |
| 68 | + if (! is_writable($file)) { |
| 69 | + $message = sprintf(__('File %s not writable!'), $file); |
| 70 | + $success = false; |
| 71 | + |
| 72 | + continue 2; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + $userTranslations = []; |
| 77 | + |
| 78 | + if (file_exists($userpoFile)) { |
| 79 | + $userTranslations = phpmo_parse_po_file($userpoFile); |
| 80 | + } |
| 81 | + |
| 82 | + $userTranslations[$text] = ['msgid' => $text, 'msgstr' => [$translation]]; |
| 83 | + |
| 84 | + if (phpmo_write_po_file($userTranslations, $userpoFile)) { |
| 85 | + $userTranslations += phpmo_parse_po_file($poFile); |
| 86 | + |
| 87 | + if (phpmo_write_mo_file($userTranslations, $moFile)) { |
| 88 | + } else { |
| 89 | + $message .= __('Error compiling!'); |
| 90 | + $success = false; |
| 91 | + } |
| 92 | + } else { |
| 93 | + $message = sprintf(__('Error saving %s file!'), $poFile); |
| 94 | + $success = false; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $this->response->setType('json'); |
| 100 | + $this->response->output(['success' => $success, 'message' => $message]); |
| 101 | + } |
| 102 | +} |
0 commit comments