From 5ebe2eb6e30a6a25660c89c14afab68c7f349f1b Mon Sep 17 00:00:00 2001 From: givanz Date: Fri, 18 Oct 2024 22:20:27 +0300 Subject: [PATCH] Editor translate modal translation loading and saving --- admin/controller/editor/translate.php | 102 ++++++++++++++++++++++++++ admin/template/editor/editor.tpl | 1 + 2 files changed, 103 insertions(+) create mode 100644 admin/controller/editor/translate.php 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 @@ +. + * + */ + +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 = @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 = @language [data-v-language-link]|href =