Skip to content

Commit

Permalink
Editor translate modal translation loading and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
givanz committed Oct 18, 2024
1 parent 46a425e commit 5ebe2eb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
102 changes: 102 additions & 0 deletions admin/controller/editor/translate.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}
1 change: 1 addition & 0 deletions admin/template/editor/editor.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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?>
Expand Down

0 comments on commit 5ebe2eb

Please sign in to comment.