Last active
June 29, 2016 10:06
-
-
Save MorrisJobke/b058f70f963869a199cce19f8bfb1b11 to your computer and use it in GitHub Desktop.
Creates a po file from a (ownC|Nextc)loud JSON file in the server code. Call it like this: `php reverse-l10n.php de`
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 | |
/** | |
* composer install gettext/gettext | |
*/ | |
require 'vendor/autoload.php'; | |
use Gettext\Translations; | |
$apps = [ | |
'comments', | |
'encryption', | |
'federatedfilesharing', | |
'federation', | |
'files', | |
'files_external', | |
'files_sharing', | |
'files_trashbin', | |
'files_versions', | |
'systemtags', | |
'updatenotification', | |
'user_ldap', | |
]; | |
function getTranslations($file, $language) { | |
if(!file_exists(__DIR__ . '/../' . $file)) { | |
throw new \Exception(); | |
} | |
$data = json_decode(file_get_contents(__DIR__ . '/../' . $file), true); | |
$translations = new Translations(); | |
foreach($data['translations'] as $key => $value) { | |
$plural = ''; | |
if(strpos($key, '_') === 0 && strrpos($key, '_') === strlen($key)-1 && strpos($key, '_::_') !== false) { | |
$key = trim($key, '_'); | |
$split = explode('_::_', $key); | |
$key = $split[0]; | |
$plural = $split[1]; | |
} | |
$t = new \Gettext\Translation('', $key, $plural); | |
if(is_string($value)) { | |
$t->setTranslation($value); | |
if(strpos($value, '%') !== false || strpos($value, '{') !== false) { | |
$t->addFlag('php-format'); | |
} | |
} else { | |
$a = array_shift($value); | |
$t->setTranslation($a); | |
$t->setPluralTranslations($value); | |
} | |
$translations[] = $t; | |
} | |
$translations->setLanguage($language); | |
$translations->setHeader(Translations::HEADER_PLURAL, $data['pluralForm']); | |
return $translations; | |
} | |
$lang = $argv[1]; | |
if(!file_exists($lang)) { | |
mkdir($lang); | |
} | |
foreach ($apps as $app) { | |
try { | |
$file = 'apps/' . $app . '/l10n/' . $lang . '.json'; | |
$translations = getTranslations($file, $lang); | |
//Export to a po file | |
$translations->toPoFile($lang . '/' . $app . '.po'); | |
} catch (\Exception $e) { | |
echo "Skipped " . $lang . ' ' . $app . PHP_EOL; | |
} | |
} | |
$libs = [ | |
'settings', | |
'lib', | |
'core' | |
]; | |
foreach ($libs as $lib) { | |
try { | |
$file = $lib . '/l10n/' . $lang . '.json'; | |
$translations = getTranslations($file, $lang); | |
//Export to a po file | |
$translations->toPoFile($lang . '/' . $lib . '.po'); | |
} catch (\Exception $e) { | |
echo "Skipped " . $lang . ' ' . $lib . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment