Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

4.9 KB
Raw Blame
<?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\Localization;

use function Vvveb\__;
use Vvveb\Controller\Base;
use function Vvveb\url;

require_once DIR_SYSTEM . 'functions' . DS . 'php-mo.php';

class Translations extends Base {
	/**
	 * Display the list of translation domains for a given language.
	 *
	 * @return void
	 */
	public function domains(): void {
		$lang   = $this->request->get['lang'] ?? false;

		if ($lang) {
			$folder = DIR_ROOT . 'locale' . DS . $lang . DS . 'LC_MESSAGES' . DS;
			$files  = glob($folder . '*.po');

			$url     = ['module' => 'localization/translations', 'action' => 'domain', 'lang' => $lang];
			$domains = [];
			//add user if does not exist yet
			$domains['user'] = url($url + ['domain' => 'user']);

			foreach ($files as $file) {
				$domain           = str_replace('.po', '', basename($file));
				$domains[$domain] =  url($url + ['domain' => $domain]);
			}

			$this->view->domains = $domains;
			$this->view->count   = count($domains);
		} else {
			$this->notFound(__('Invalid request!'));
		}
	}

	/**
	 * Compile all translation domain PO files into a single MO file.
	 *
	 * @return string Status message indicating compilation result.
	 */
	private function compile(): string {
		//add default translations first to be overriden by all others
		$domains = ['vvveb'];
		$message = '';
		$view    = $this->view;

		$folder = DIR_ROOT . 'locale' . DS . $view->lang . DS . 'LC_MESSAGES' . DS;

		foreach (glob("$folder/*.po") as $file) {
			$domain = str_replace('.po', '', basename($file));

			if ($domain !== 'user' && $domain !== 'vvveb') {
				$domains[] = $domain;
			}
		}

		//add user translations last to override all others
		$domains[] = 'user';

		foreach ($domains as $domain) {
			$poFile = $folder . $domain . '.po';

			if (file_exists($poFile) && ($translations = phpmo_parse_po_file($poFile) ?: [])) {
				$view->translations = array_merge($view->translations, $translations);
			}
		}
		//compile
		if (phpmo_write_mo_file($view->translations, $folder . 'vvveb-new.mo')) {
			if (rename($folder . 'vvveb-new.mo', $folder . 'vvveb.mo')) {
				clearstatcache();
				$message .= "\n" . __('Compiled!');
			}
		} else {
			$message .= "\n" . __('Error compiling!');
		}

		return $message;
	}

	/**
	 * Save translation edits to PO file and recompile the MO file.
	 *
	 * @return void
	 */
	public function save(): void {
		$view         = $this->view;
		$translations = $this->request->post['translations'] ?? [];
		$this->response->setType('json');

		$message = __('No data!');

		if ($translations) {
			//use common check po file and parse
			$this->domain();

			$folder = DIR_ROOT . 'locale' . DS . $view->lang . DS . 'LC_MESSAGES' . DS;
			if (!file_exists($folder)) {
				mkdir($folder, 0777, true);
			}

			foreach ($translations as $text => $translation) {
				$view->translations[$text] = ['msgid' => $text, 'msgstr' => [$translation]];
			}

			if (phpmo_write_po_file($view->translations, $folder . $view->domain . '.po')) {
				$message = __('Saved!');
				$message .= $this->compile();
			} else {
				$message = __('Error saving!');
			}
		}

		$result = ['message' => $message];
		$this->response->output($result);
	}

	/**
	 * Display a translation domain's PO file for editing.
	 *
	 * @return void
	 */
	public function domain(): void {
		$view             = $this->view;
		$view->lang       =  $lang       = $this->request->get['lang'] ?? false;
		$view->domain     =  $domain     = $this->request->get['domain'] ?? false;
		$url              = ['module' => 'localization/translations', 'lang' => $lang, 'domain' => $domain];
		$view->domainsUrl = url($url + ['action' => 'domains']);
		$view->saveUrl    = url($url + ['action' => 'save']);

		if ($lang && $domain) {
			$poFile       = DIR_ROOT . 'locale' . DS . $lang . DS . 'LC_MESSAGES' . DS . $domain . '.po';
			$view->poFile = $poFile;

			if (file_exists($poFile)) {
				$view->translations = phpmo_parse_po_file($poFile);

				if (!is_writable($poFile)) {
					$view->info = [__('Po file not writable, saving will not work!')];
				}
			} else {
				if ($domain == 'user') {
					//if domain is user then create it
					$view->translations = [];
				} else {
					$this->notFound(__('Po file does not exist!'));
				}
			}
		} else {
			$this->notFound(__('Invalid request!'));
		}
	}
}