Files
FreshRSS/cli/i18n/I18nFile.php
Christian Weiske 1acc646222 Show time since when a feed has problems + new timeago() method and i18n plurals (#8670)
Closes https://github.com/FreshRSS/FreshRSS/issues/8508

Changes proposed in this pull request:

- Use an integer for `Feed::error` everywhere (follow up to #8646)
- Extract `Entry::machineReadableDate()` into function for use in HTML templates
- Add `timeago()` function that converts a unix timestamp into a "4 weeks ago" string
- Show the last successful feed update, and the last erroneous update

How to test the feature manually:

1. Update a feed
2. Modify the feed URL in the database and set it to a non-existing URL
3. Update the feed again
4. Open the "Manage feed" and see the expanded error message:

>  Blast! This feed has encountered a problem. If this situation persists, please verify that it is still reachable.
> Last successful update 3 hours ago, last erroneous update 1 hour ago. 

You can hover the relative dates to see the timestamp.

* Make Feed::error an int everywhere

Related: https://github.com/FreshRSS/FreshRSS/pull/8646

* Extract timestamptomachinedate()

.. for later usage in the feed error time display.

* Show time since when a feed has problems

We add our own "timeago" function that converts a unix timestamp
into a "4 weeks ago" string.

Resolves: https://github.com/FreshRSS/FreshRSS/issues/8508

* Add new translation keys

* i18n fr, en-US

* Minor XHTML preference

* Slightly shorter rewrite, also hopefully easier to read

* Rewrite to allow (simple) plural
I also moved some functions around for hopefully a more generic and better structure.
I made some changes for the sake of speed (e.g. second-based logic instead of datetime intervals).
Note: I used automatic translation as I was worried it would be too complicated to explain to translators... I proofread the few languages I have some familiarity with.

* Add reference to CLDR

* Slightly more compact syntax

* Always show last update, fix case of unknown error date

* Remove forgotten span

* No need for multi-lines anymore

* Fix error date thresshold

* plurals forms

* Extract gettext formula conversion script to cli

* Simplify a bit

* Escort excess parentheses to the door

* Simplify

* Avoid being too clever in localization

* Fix German

* Fix plural TODO parsing

* Ignore en-US translation

* make fix-all

* git update-index --chmod=+x cli/compile.plurals.php

* Heredoc indent PHP 7.3+

* compileAll: Continue on error

* PHP strict comparisons

* Light logical simplification

* Cache plural_message_families

* Avoid case of empty value

* A bit of documentation

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Co-authored-by: Frans de Jonge <frans@clevercast.com>
Co-authored-by: Frans de Jonge <fransdejonge@gmail.com>
2026-04-07 22:56:02 +02:00

210 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/I18nValue.php';
class I18nFile {
/**
* @param array<mixed,mixed> $array
* @phpstan-assert-if-true array<int|string,string|array<mixed>> $array
*/
public static function is_array_recursive_string(array $array): bool {
foreach ($array as $value) {
if (!is_string($value) && !(is_array($value) && self::is_array_recursive_string($value))) {
return false;
}
}
return true;
}
/**
* @return array<string,array<string,array<string,I18nValue>>>
*/
public function load(): array {
$i18n = [];
$dirs = new DirectoryIterator(I18N_PATH);
foreach ($dirs as $dir) {
if ($dir->isDot()) {
continue;
}
$files = new DirectoryIterator($dir->getPathname());
foreach ($files as $file) {
if (!$file->isFile()) {
continue;
}
if ($file->getFilename() === 'plurals.php') {
continue;
}
$i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten($this->process($file->getPathname()), $file->getBasename('.php'));
}
}
return $i18n;
}
/**
* @param array<string,array<string,array<string,I18nValue>>> $i18n
*/
public function dump(array $i18n): void {
foreach ($i18n as $language => $file) {
$dir = I18N_PATH . DIRECTORY_SEPARATOR . $language;
if (!file_exists($dir)) {
mkdir($dir, 0770, true);
}
foreach ($file as $name => $content) {
$filename = $dir . DIRECTORY_SEPARATOR . $name;
file_put_contents($filename, $this->format($content));
}
}
}
/**
* Process the content of an i18n file
* @return array<int|string,string|array<mixed>>
*/
private function process(string $filename): array {
$fileContent = file_get_contents($filename);
if (!is_string($fileContent)) {
return [];
}
$content = str_replace('<?php', '', $fileContent);
$content = preg_replace([
"#',\s*//\s*TODO.*#i",
"#',\s*//\s*DIRTY.*#i",
"#',\s*//\s*IGNORE.*#i",
], [
' -> todo\',',
' -> dirty\',',
' -> ignore\',',
], $content);
try {
$content = eval($content);
} catch (ParseError $ex) {
if (defined('STDERR')) {
fwrite(STDERR, "Error while processing: $filename\n");
fwrite(STDERR, $ex->getMessage());
}
die(1);
}
if (is_array($content) && self::is_array_recursive_string($content)) {
return $content;
}
return [];
}
/**
* Flatten an array of translation
*
* @param array<int|string,I18nValue|string|array<mixed>|mixed> $translation
* @return array<string,I18nValue>
*/
private function flatten(array $translation, string $prefix = ''): array {
$a = [];
if ('' !== $prefix) {
$prefix .= '.';
}
foreach ($translation as $key => $value) {
$key = (string)$key;
if (is_array($value) && self::is_array_recursive_string($value)) {
$a += $this->flatten($value, $prefix . $key);
} elseif (is_string($value) || $value instanceof I18nValue) {
$a[$prefix . $key] = new I18nValue($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<string,I18nValue> $translation
* @return array<int|string,mixed>
*/
private function unflatten(array $translation): array {
$a = [];
ksort($translation, SORT_NATURAL);
foreach ($translation as $compoundKey => $value) {
$keys = explode('.', $compoundKey);
array_shift($keys);
$current =& $a;
$lastIndex = count($keys) - 1;
foreach ($keys as $index => $key) {
$normalisedKey = ctype_digit($key) ? (int)$key : $key;
if ($index === $lastIndex) {
$current[$normalisedKey] = $value->__toString();
continue;
}
if (!isset($current[$normalisedKey]) || !is_array($current[$normalisedKey])) {
$current[$normalisedKey] = [];
}
$current =& $current[$normalisedKey];
}
unset($current);
}
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<string,I18nValue> $translation
*/
private function format(array $translation): string {
$translation = var_export($this->unflatten($translation), true);
$patterns = [
'/ -> todo\',/',
'/ -> dirty\',/',
'/ -> ignore\',/',
'/array \(/',
'/=>\s*array/',
'/(\w) {2}/',
'/ {2}/',
];
$replacements = [
"',\t// TODO", // Double quoting is mandatory to have a tab instead of the \t string
"',\t// DIRTY", // Double quoting is mandatory to have a tab instead of the \t string
"',\t// IGNORE", // Double quoting is mandatory to have a tab instead of the \t string
'array(',
'=> array',
'$1 ',
"\t", // Double quoting is mandatory to have a tab instead of the \t string
];
$translation = preg_replace($patterns, $replacements, $translation);
return <<<PHP
<?php
/******************************************************************************
* Each entry of that file can be associated with a comment to indicate its *
* state. When there is no comment, it means the entry is fully translated. *
* The recognized comments are (comment matching is case-insensitive): *
* + TODO: the entry has never been translated. *
* + DIRTY: the entry has been translated but needs to be updated. *
* + IGNORE: the entry does not need to be translated. *
* When a comment is not recognized, it is discarded. *
******************************************************************************/
return {$translation};
PHP;
}
}