mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-01-23 04:38:01 -05:00
* Ignore i18n gen.dir key * Add a makefile target to update an i18n key * Mark some i18n keys to ignore * Reformat i18n files correctly * Make i18n keys sort case-sensitive Calling `make i18n-format` was always inverting 4 lines: - gen.date.dec with gen.date.Dec - and gen.date.nov with gen.date.Nov I don't know why these particular lines and not the others, but it appeared the sort function was case insensitive due to the `SORT_FLAG_CASE` flag passed to the `ksort` function. Removing this flag makes the calls to the formatter idempotent and more reliable. Unfortunately it moves a lot of lines since the `_` character is considered differently. * Check i18n files are correctly formatted on Travis
124 lines
2.9 KiB
PHP
124 lines
2.9 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/I18nFileInterface.php';
|
|
|
|
class I18nFile implements I18nFileInterface{
|
|
|
|
private $i18nPath;
|
|
|
|
public function __construct() {
|
|
$this->i18nPath = __DIR__ . '/../../app/i18n';
|
|
}
|
|
|
|
public function load() {
|
|
$i18n = array();
|
|
$dirs = new DirectoryIterator($this->i18nPath);
|
|
foreach ($dirs as $dir) {
|
|
if ($dir->isDot()) {
|
|
continue;
|
|
}
|
|
$files = new DirectoryIterator($dir->getPathname());
|
|
foreach ($files as $file) {
|
|
if (!$file->isFile()) {
|
|
continue;
|
|
}
|
|
$i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten(include $file->getPathname(), $file->getBasename('.php'));
|
|
}
|
|
}
|
|
|
|
return $i18n;
|
|
}
|
|
|
|
public function dump(array $i18n) {
|
|
foreach ($i18n as $language => $file) {
|
|
$dir = $this->i18nPath . DIRECTORY_SEPARATOR . $language;
|
|
if (!file_exists($dir)) {
|
|
mkdir($dir);
|
|
}
|
|
foreach ($file as $name => $content) {
|
|
$filename = $dir . DIRECTORY_SEPARATOR . $name;
|
|
file_put_contents($filename, $this->format($content));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Flatten an array of translation
|
|
*
|
|
* @param array $translation
|
|
* @param string $prefix
|
|
* @return array
|
|
*/
|
|
private function flatten($translation, $prefix = '') {
|
|
$a = array();
|
|
|
|
if ('' !== $prefix) {
|
|
$prefix .= '.';
|
|
}
|
|
|
|
foreach ($translation as $key => $value) {
|
|
if (is_array($value)) {
|
|
$a += $this->flatten($value, $prefix . $key);
|
|
} else {
|
|
$a[$prefix . $key] = $value;
|
|
}
|
|
}
|
|
|
|
return $a;
|
|
}
|
|
|
|
/**
|
|
* Unflatten an array of translation
|
|
*
|
|
* The first key is dropped since it represents the filename and we have
|
|
* no use of it.
|
|
*
|
|
* @param array $translation
|
|
* @return array
|
|
*/
|
|
private function unflatten($translation) {
|
|
$a = array();
|
|
|
|
ksort($translation, SORT_NATURAL);
|
|
foreach ($translation as $compoundKey => $value) {
|
|
$keys = explode('.', $compoundKey);
|
|
array_shift($keys);
|
|
eval("\$a['" . implode("']['", $keys) . "'] = '" . addcslashes($value, "'") . "';");
|
|
}
|
|
|
|
return $a;
|
|
}
|
|
|
|
/**
|
|
* Format an array of translation
|
|
*
|
|
* It takes an array of translation and format it to be dumped in a
|
|
* translation file. The array is first converted to a string then some
|
|
* formatting regexes are applied to match the original content.
|
|
*
|
|
* @param array $translation
|
|
* @return string
|
|
*/
|
|
private function format($translation) {
|
|
$translation = var_export($this->unflatten($translation), true);
|
|
$patterns = array(
|
|
'/array \(/',
|
|
'/=>\s*array/',
|
|
'/(\w) {2}/',
|
|
'/ {2}/',
|
|
'/ -> todo\',/',
|
|
);
|
|
$replacements = array(
|
|
'array(',
|
|
'=> array',
|
|
'$1 ',
|
|
"\t", // Double quoting is mandatory to have a tab instead of the \t string
|
|
"',\t// TODO - Translation", // Double quoting is mandatory to have a tab instead of the \t string
|
|
);
|
|
$translation = preg_replace($patterns, $replacements, $translation);
|
|
|
|
// Double quoting is mandatory to have new lines instead of \n strings
|
|
return sprintf("<?php\n\nreturn %s;\n", $translation);
|
|
}
|
|
}
|