Files
FreshRSS/cli/i18n/I18nIgnoreFile.php
Alexis Degrugillier 3e49b44839 Update i18n cli tools (#2673)
* Update i18n cli tools

Few things were bugging me when using the cli tool for i18n. So I've modified
the tools to be easier to use.

First, the tool automatically adds missing keys to all languages. This way, we
always have all keys in all languages.
Second, the tool detects all untranslated keys and adds automatically the todo
comment after the value.
Third, when adding a new key, the key is pushed to all languages at once. There
is no need to duplicate it manually. Thus making the duplication process obsolete.
Fourth, translation and ignore keys are manipulated at the same time. Thus we
don't have obsolete ignored strings anymore.

* Add i18n rules

I find that having the common rules in the Makefile is easier to use,
as long as you know they are here. As it is self documented, people
will see the new rules when using make.

* Use long parameters in Makefile

I find that using long parameters in scripts makes it easier to understand
what's going on. So I've switched all short parameters to long one.

* Format all i18n files

I've used the updated version of the cli tools to have some output
that can be consistently formated. This commit is a huge formating
commit. Nothing was added but some comments were removed in the
process.
2019-12-04 08:27:39 +01:00

64 lines
1.5 KiB
PHP

<?php
require_once __DIR__ . '/I18nFileInterface.php';
class I18nIgnoreFile implements I18nFileInterface {
private $i18nPath;
public function __construct() {
$this->i18nPath = __DIR__ . '/ignore';
}
public function dump(array $i18n) {
foreach ($i18n as $language => $content) {
$filename = $this->i18nPath . DIRECTORY_SEPARATOR . $language . '.php';
file_put_contents($filename, $this->format($content));
}
}
public function load() {
$i18n = array();
$files = new DirectoryIterator($this->i18nPath);
foreach ($files as $file) {
if (!$file->isFile()) {
continue;
}
$i18n[$file->getBasename('.php')] = (include $file->getPathname());
}
return $i18n;
}
/**
* 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(($translation), true);
$patterns = array(
'/array \(/',
'/=>\s*array/',
'/ {2}/',
'/\d+ => /',
);
$replacements = array(
'array(',
'=> array',
"\t", // 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);
}
}