diff --git a/tools/generate_table_po.php b/tools/generate_table_po.php new file mode 100755 index 0000000000..29f9c9fcfc --- /dev/null +++ b/tools/generate_table_po.php @@ -0,0 +1,116 @@ +#!/usr/bin/php + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://www.github.com/aces/Loris/ + */ +require_once __DIR__ . "/../vendor/autoload.php"; +require_once "generic_includes.php"; + +/** + * Prints usage info to stderr and exists + * + * @return void + */ +function usage() +{ + global $argv; + fprintf(STDERR, "usage: %s --columns=column1,column2 tablename\n", $argv[0]); + exit(1); +} + +$lastidx = 0; +$opts = getopt("", [ "columns::" ], $lastidx); +$columns = $opts['columns']; +if (empty($opts['columns'])) { + usage(); +} + +if ($lastidx !== $argc-1) { + usage(); +} +$table = $argv[$lastidx]; + +$outputfile = __DIR__ . "/../project/locale/$table.pot"; + +if (file_exists($outputfile)) { + fprintf( + STDERR, + "Will not overwrite existing template file %s. ' + .'Backup and remove the file if you want to overwrite it.", + realpath($outputfile) + ); + exit(2); +} + +// This is not particularly robust or secure, but this script is only run by +// trusted users on the backend. +$rows = $DB->pselect("SELECT $columns from $table", []); + +$values = []; +foreach ($rows as $row) { + foreach ($row as $val) { + $values[$val] = ''; + } +} + +$values = array_keys($values); + +if (count($values) == 0) { + fprintf(STDERR, "No translateable values found\n"); + exit(3); +} + + +$fd = fopen($outputfile, "w"); + +fwrite( + $fd, + <<\\n" +"Language-Team: LANGUAGE \\n" +"Language: \\n" +"MIME-Version: 1.0\\n" +"Content-Type: text/plain; charset=UTF-8\\n" +"Content-Transfer-Encoding: 8bit\\n" + + +EOF +); + +foreach ($values as $val) { + if (!empty($val)) { + fprintf($fd, "msgid \"%s\"\n", $val); + fprintf($fd, "msgstr \"\"\n\n"); + } +}