Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Editor translate modal translation loading and saving

givanz5ebe2eb6e30a
diff --git a/admin/controller/editor/translate.php b/admin/controller/editor/translate.php
new file mode 100644
index 0000000..9bbab60
--- /dev/null
+++ b/admin/controller/editor/translate.php
@@ -0,0 +1,102 @@
+<?php
+
+/**
+ * Vvveb
+ *
+ * Copyright (C) 2022 Ziadin Givan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Vvveb\Controller\Editor;
+
+use function Vvveb\__;
+use Vvveb\Controller\Base;
+
+class Translate extends Base {
+ function get() {
+ $text = $this->request->post['text'];
+ $languages = \Vvveb\availableLanguages();
+ $translations = [];
+
+ foreach ($languages as $lang) {
+ $code = $lang['code'];
+ \Vvveb\setLanguage($code);
+ $translations[$code] = __($text);
+ }
+ //restore language
+ \Vvveb\setLanguage(\Vvveb\getLanguage());
+
+ $this->response->setType('json');
+ $this->response->output($translations);
+ }
+
+ function save() {
+ $translations = $this->request->post ?? [];
+ $success = true;
+ $message = __('Translations saved!');
+
+ if ($translations) {
+ require_once DIR_SYSTEM . 'functions' . DS . 'php-mo.php';
+
+ $defaultLang = key($translations); //'en_US';
+ $domain = 'vvveb';
+ $text = $translations[$defaultLang];
+
+ foreach ($translations as $langCode => $translation) {
+ if ($langCode == $defaultLang || $success == false) {
+ continue;
+ }
+ $folder = DIR_ROOT . 'locale' . DS . $langCode . DS . 'LC_MESSAGES' . DS;
+ $userpoFile = $folder . 'user.po';
+ $poFile = $folder . $domain . '.po';
+ $moFile = $folder . $domain . '.mo';
+
+ foreach ([$poFile, $moFile] as $file) {
+ if (! is_writable($file)) {
+ $message = sprintf(__('File %s not writable!'), $file);
+ $success = false;
+
+ continue 2;
+ }
+ }
+
+ $userTranslations = [];
+
+ if (file_exists($userpoFile)) {
+ $userTranslations = phpmo_parse_po_file($userpoFile);
+ }
+
+ $userTranslations[$text] = ['msgid' => $text, 'msgstr' => [$translation]];
+
+ if (phpmo_write_po_file($userTranslations, $userpoFile)) {
+ $userTranslations += phpmo_parse_po_file($poFile);
+
+ if (phpmo_write_mo_file($userTranslations, $moFile)) {
+ } else {
+ $message .= __('Error compiling!');
+ $success = false;
+ }
+ } else {
+ $message = sprintf(__('Error saving %s file!'), $poFile);
+ $success = false;
+ }
+ }
+ }
+
+ $this->response->setType('json');
+ $this->response->output(['success' => $success, 'message' => $message]);
+ }
+}
diff --git a/admin/template/editor/editor.tpl b/admin/template/editor/editor.tpl
index d3d04a0..01e85e9 100755
--- a/admin/template/editor/editor.tpl
+++ b/admin/template/editor/editor.tpl
@@ -125,6 +125,7 @@ foreach ($this->languagesList as $language) {
[data-v-languages] [data-v-language-id]|addClass = <?php if ($_i == 0) echo 'show active';?>
@language [data-v-language-name] = $language['name']
+ @language [data-v-language-code]|name = $language['code']
@language [data-v-language-img]|title = $language['name']
@language [data-v-language-img]|src = <?php echo 'language/' . $language['code'] . '/' . $language['code'] . '.png';?>
@language [data-v-language-link]|href = <?php echo '#lang-' . $language['code'] . '-' . $_lang_instance?>