aboutsummaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/README.md4
-rwxr-xr-xcli/check.translation.php116
2 files changed, 43 insertions, 77 deletions
diff --git a/cli/README.md b/cli/README.md
index 4d6ba62fe..47f28a7f7 100644
--- a/cli/README.md
+++ b/cli/README.md
@@ -149,13 +149,13 @@ cd /usr/share/FreshRSS
# -r, --revert revert the action (only used with ignore action).
# -o, --origin-language selects the origin language (only used with add language action).
-./cli/check-translation.php [ ---display-result --help --language fr --display-report --generate-readme ]
+./cli/check.translation.php [ ---display-result --help --language fr --display-report --generate-readme ]
# Check if translation files have missing keys or missing translations.
# -d, --display-result display results of check.
# -h, --help display help text and exit.
# -l, --language set the language check.
# -r, --display-report display completion report.
-# -g, --generate-readme generate readme for translation status.
+# -g, --generate-readme generate translation progress section in readme.
```
## Note about cron
diff --git a/cli/check.translation.php b/cli/check.translation.php
index b4c7e6850..5c0873099 100755
--- a/cli/check.translation.php
+++ b/cli/check.translation.php
@@ -74,7 +74,22 @@ if ($cliOptions->displayReport) {
}
}
-function writeToReadme(string $readmePath, string $markdownImgStr): void {
+function writeToReadme(string $readmePath, string $markdownTable): void {
+ $language = explode('.', $readmePath)[1];
+ // expecting `README.md` for `en` or `README.fr.md` for `fr`
+ if ($language === 'md') {
+ $language = 'en';
+ }
+ Minz_Translate::init($language);
+ $placeholders = [];
+ if (preg_match_all('/__.*?__/', $markdownTable, $placeholders) === false) {
+ echo 'Error: Fail while matching translation placeholders', PHP_EOL;
+ exit(1);
+ }
+ foreach (array_unique($placeholders[0]) as $_ => $placeholder) {
+ $markdownTable = str_replace($placeholder, _t('gen.readme.' . substr($placeholder, 2, -2)), $markdownTable);
+ }
+
$readme = file_get_contents($readmePath);
if ($readme === false) {
echo 'Error: Unable to open ' . $readmePath, PHP_EOL;
@@ -82,9 +97,9 @@ function writeToReadme(string $readmePath, string $markdownImgStr): void {
}
if (file_put_contents($readmePath, preg_replace('/<translations>(.*?)<\/translations>/s', <<<EOF
<translations>
- <!-- This section is automatically generated by `cli/check.translation.php -g` -->
+ <!-- This section is automatically generated by `./cli/check.translation.php -g` -->
- $markdownImgStr
+ $markdownTable
</translations>
EOF, $readme)) === false) {
@@ -94,90 +109,41 @@ function writeToReadme(string $readmePath, string $markdownImgStr): void {
echo 'Successfully written translation status into ' . $readmePath, PHP_EOL;
}
-function embedSvg(string $contents): string {
- return preg_replace(
- '/<svg\s+(?:(?:[^>]*?)(xmlns=["\'][^"\']+["\']))?(?:(?:[^>]*?)(viewBox=["\'][^"\']+["\']))?(?:[^>]*?)>/i',
- '<svg \1 \2 width="16" height="16" x="9" y="2">',
- $contents
- ) ?? '';
-}
-
if ($cliOptions->generateReadme) {
- $supportedFormats = ['txt', 'svg'];
- $flagsDir = dirname(__DIR__) . '/docs/i18n/flags';
+ $markdownTable = <<<EOF
+ | __language__ | __translated__ | |
+ | - | - | - |
+ EOF;
+ $markdownTable .= "\n";
- $markdownImgStr = '';
foreach ($percentage as $lang => $value) {
$percentageInt = intval(rtrim($value, '%'));
- $color = 'green';
- if ($percentageInt < 90) {
- $color = 'gold';
- }
- if ($percentageInt < 70) {
- $color = 'darkred';
- }
- $svgFile = $flagsDir . '/' . $lang . '.svg';
- $svg = '';
- if (file_exists($svgFile)) {
- $svg = file_get_contents($svgFile);
- if ($svg === false) {
- echo 'Error: Unable to open ' . $svgFile, PHP_EOL;
- exit(1);
- }
- }
+ $completed = intval($percentageInt / 10);
+ $uncompleted = intval(ceil((100 - $percentageInt) / 10));
+ $progressBar = str_repeat('■', $completed) . str_repeat('・', $uncompleted);
$ghSearchUrl = 'https://github.com/search?q=' . urlencode("repo:FreshRSS/FreshRSS path:app/i18n/$lang /(TODO|DIRTY)$/");
- $genPath = $flagsDir . '/gen/' . $lang . '.svg';
- $template = '<!-- This file is automatically generated by `cli/check.translation.php -g` -->' . "\n";
-
- if ($svg === '') {
- $i18nGen = include dirname(__DIR__) . "/app/i18n/$lang/gen.php";
- if (!is_array($i18nGen) || !is_string($i18nGen['flag'] ?? null)) {
- echo 'Error: No Unicode flag found for language ' . $lang, PHP_EOL;
- exit(1);
- }
- $unicodeFlag = $i18nGen['flag'];
- if ($lang !== 'en' && $unicodeFlag === '🇬🇧') {
- echo 'Error: Wrong Unicode flag for language ' . $lang, PHP_EOL;
- exit(1);
- }
- $value = $unicodeFlag . ' ' . $percentageInt . '%';
- $template .= <<<EOF
- <svg xmlns="http://www.w3.org/2000/svg" width="70" height="20">
- <g fill="white" font-size="12" font-family="Verdana" text-anchor="middle">
- <rect rx="3" width="70" height="20" fill="$color" />
- <text x="34" y="14">$value</text>
- </g>
- </svg>
- EOF;
- } else {
- // An SVG file is available to override the Unicode flag
- $value = $percentageInt . '%';
- $contents = embedSvg($svg);
- $template .= <<<EOF
- <svg xmlns="http://www.w3.org/2000/svg" width="70" height="20">
- <g fill="white" font-size="12" font-family="Verdana" text-anchor="middle">
- <rect rx="3" width="70" height="20" fill="$color" />
- <!-- embedded SVG -->
- $contents
- <!-- end of embedded SVG -->
- <text x="43" y="14">$value</text>
- </g>
- </svg>
- EOF;
- }
- if (file_put_contents($genPath, $template) === false) {
- echo 'Error: Fail while writing to ' . $genPath, PHP_EOL;
- exit(1);
+
+ // BCP 47 / ISO 3166: Uppercase the last (region) subtag if any (e.g. pt-pt -> pt-PT)
+ $bcp47 = $lang;
+ if (str_contains($bcp47, '-')) {
+ $parts = explode('-', $bcp47);
+ $parts[count($parts) - 1] = strtoupper($parts[count($parts) - 1]);
+ $bcp47 = implode('-', $parts);
}
- $markdownImgStr .= "[![$lang](./docs/i18n/flags/gen/$lang.svg)]($ghSearchUrl) ";
+
+ $markdownTable .= '| ' . implode(' | ', [
+ _t('gen.lang.' . $lang) . " ($bcp47)",
+ $progressBar . ' ' . $percentageInt . '%',
+ "[__contribute__]($ghSearchUrl)",
+ ]) . " |\n";
}
// In case we're located in ./cli/
if (!file_exists('constants.php')) {
chdir('..');
}
foreach (array_merge(['README.md'], glob('README.*.md') ?: []) as $readmePath) {
- writeToReadme($readmePath, rtrim($markdownImgStr));
+ writeToReadme($readmePath, rtrim($markdownTable));
}
exit();
}
@@ -233,7 +199,7 @@ DESCRIPTION
-h, --help display this help and exit.
-l, --language=LANG filter by LANG.
-r, --display-report display completion report.
- -g, --generate-readme generate readme for translation status.
+ -g, --generate-readme generate translation progress section in readme.
HELP;
exit();