miniflux-legacy/scripts/sync-locales.php

74 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2014-07-23 12:43:48 +02:00
<?php
$reference_lang = 'fr_FR';
2016-12-29 02:52:58 +01:00
$reference_file = __DIR__.'/../app/locales/'.$reference_lang.'/translations.php';
2014-07-23 12:43:48 +02:00
$reference = include $reference_file;
function update_missing_locales(array $reference, $outdated_file)
{
$outdated = include $outdated_file;
2014-07-23 12:43:48 +02:00
$output = "<?php\n\n";
$output .= "return array(\n";
foreach (array_keys($reference) as $key) {
$outputKey = str_replace("'", "\'", $key);
$outputValue = " // '".$outputKey."' => ''";
if (isset($outdated[$key])) {
if ($key === 'plural') {
$outputValue = $outputKey."' => ".getFunctionCode($outdated[$key]);
}
elseif (is_array($outdated[$key])) {
foreach($outdated[$key] as &$value) {
$value = str_replace("'", "\'", $value);
}
$outputValue = $outputKey."' => array('".join("', '", $outdated[$key])."')";
}
else {
$outputValue = $outputKey."' => '".str_replace("'", "\'", $outdated[$key])."'";
}
$outputValue = " '".$outputValue;
}
$output .= $outputValue.",\n";
2014-07-23 12:43:48 +02:00
}
$output .= ");\n";
return $output;
2014-07-23 12:43:48 +02:00
}
2016-08-19 03:02:49 +02:00
foreach (new DirectoryIterator('app/locales') as $fileInfo) {
if (! $fileInfo->isDot() && $fileInfo->isDir() && $fileInfo->getFilename() !== $reference_lang && $fileInfo->getFilename() !== 'en_US') {
2016-08-19 03:02:49 +02:00
$filename = 'app/locales/'.$fileInfo->getFilename().'/translations.php';
2016-05-03 10:45:07 +02:00
echo $fileInfo->getFilename(), ' (', $filename, ')', PHP_EOL;
file_put_contents($filename, update_missing_locales($reference, $filename));
}
}
function getFunctionCode($name)
{
$reflector = new ReflectionFunction($name);
$file = new SplFileObject($reflector->getFileName());
$file->seek($reflector->getStartLine()-1);
$sourcecode = '';
while ($file->key() < $reflector->getEndLine())
{
$sourcecode .= $file->current();
$file->next();
}
$begin = strpos($sourcecode, 'function');
$end = strrpos($sourcecode, '}');
return substr($sourcecode, $begin, $end - $begin + 1);
2015-10-20 03:21:18 +02:00
}