Created
December 27, 2018 13:29
-
-
Save mpdroog/f246e124258a809c119712920f29d5eb to your computer and use it in GitHub Desktop.
TOML-config to PO-file for Poedit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* TOML to PO converter. | |
*/ | |
$args = $_SERVER["argv"]; | |
if ($args[0] === "php") { | |
array_shift($args); | |
} | |
array_shift($args); | |
$args = array_values($args); // php-file | |
if (count($args) !== 1) { | |
exit("Missing arg lang"); | |
} | |
$lang = $args[0]; | |
mkdir("text-$lang"); | |
require __DIR__ . "/../../_toml.php"; | |
$head = 'msgid "" | |
msgstr "" | |
"Project-Id-Version: EasyUsenet 2.0.0\n" | |
"Report-Msgid-Bugs-To: support@easyusenet.nl \n" | |
"Last-Translator: Mark Droog <rootdev@gmail.com>\n" | |
"MIME-Version: 1.0\n" | |
"Content-Type: text/plain; charset=UTF-8\n" | |
"Content-Transfer-Encoding: 8bit\n" | |
"Plural-Forms: nplurals=2; plural=(n != 1);\n"'; | |
function getDirContents($dir, &$results = array()){ | |
$files = scandir($dir); | |
foreach($files as $key => $value){ | |
$path = realpath($dir.DIRECTORY_SEPARATOR.$value); | |
if(!is_dir($path)) { | |
$results[] = $path; | |
} else if($value != "." && $value != "..") { | |
getDirContents($path, $results); | |
$results[] = $path; | |
} | |
} | |
return $results; | |
} | |
function kvpmap($prefix, $map) { | |
$out = []; | |
foreach ((array)$map as $k => $v) { | |
if (is_array($v) || is_object($v)) { | |
$out = array_merge($out, kvpmap("$prefix.$k", $v)); | |
continue; | |
} | |
$out[ "$prefix.$k" ] = $v; | |
} | |
return $out; | |
} | |
$lines = []; | |
$uniq = []; | |
foreach (getDirContents(__DIR__ . "/en/") as $f) { | |
$tok = explode("/", $f); | |
$ext = substr($f, strrpos($f, ".")+1); | |
$name = $tok[ count($tok) -1 ]; | |
if ($ext === "toml") { | |
// toml | |
$p = (array)Toml::parse(file_get_contents($f)); | |
$o = kvpmap($name, $p); | |
//var_dump($o); | |
foreach ($o as $k => $v) { | |
$line = []; | |
$line[] = sprintf('#: %s:%s', $f, $k); | |
if (strpos($v, '%s') !== false) { | |
$line[] = "#, c-format"; | |
} | |
if (strpos($v, '{') !== false && strpos($v, '}') !== false) { | |
$line[] = sprintf('#, python-format'); | |
$v = str_replace('{', '%(', $v); | |
$v = str_replace('}', ')', $v); | |
} | |
$line[] = sprintf('msgid "%s"', $v); | |
$line[] = sprintf('msgstr "%s"', ''); | |
if (isset($uniq[$v])) { | |
// Duplicate | |
array_unshift($lines[ $uniq[$v] ], $line[0]); | |
} else { | |
$lines[] = $line; | |
$uniq[$v] = count($lines)-1; | |
} | |
} | |
} else { | |
// text | |
$wname = str_replace('.md', '', $name); | |
$wname = str_replace('.txt', '', $wname); | |
file_put_contents("text-$lang/$wname.txt", file_get_contents($f)); | |
} | |
} | |
$txt = $head . "\n\n"; | |
foreach ($lines as $line) { | |
$txt .= implode("\n", $line) . "\n"; | |
} | |
file_put_contents("text-$lang/$lang.po", $txt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment