Skip to content

Instantly share code, notes, and snippets.

@MorrisJobke
Last active June 29, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MorrisJobke/b058f70f963869a199cce19f8bfb1b11 to your computer and use it in GitHub Desktop.
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`
<?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