|
| 1 | +<?php |
| 2 | + |
| 3 | +$TRANSLATIONS = include __DIR__ . "/../src/translations.php"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get the percentage of translated phrases for each locale |
| 7 | + * |
| 8 | + * @param array $translations The translations array |
| 9 | + * @return array The percentage of translated phrases for each locale |
| 10 | + */ |
| 11 | +function getProgress(array $translations): array |
| 12 | +{ |
| 13 | + $phrases_to_translate = [ |
| 14 | + "Total Contributions", |
| 15 | + "Current Streak", |
| 16 | + "Longest Streak", |
| 17 | + "Week Streak", |
| 18 | + "Longest Week Streak", |
| 19 | + "Present", |
| 20 | + ]; |
| 21 | + |
| 22 | + $progress = []; |
| 23 | + foreach ($translations as $locale => $phrases) { |
| 24 | + $translated = 0; |
| 25 | + foreach ($phrases_to_translate as $phrase) { |
| 26 | + if (isset($phrases[$phrase])) { |
| 27 | + $translated++; |
| 28 | + } |
| 29 | + } |
| 30 | + $percentage = round(($translated / count($phrases_to_translate)) * 100); |
| 31 | + $locale_name = Locale::getDisplayName($locale, $locale); |
| 32 | + $progress[$locale] = [ |
| 33 | + "locale" => $locale, |
| 34 | + "locale_name" => $locale_name, |
| 35 | + "percentage" => $percentage, |
| 36 | + ]; |
| 37 | + } |
| 38 | + // sort by percentage |
| 39 | + uasort($progress, function ($a, $b) { |
| 40 | + return $b["percentage"] <=> $a["percentage"]; |
| 41 | + }); |
| 42 | + return $progress; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Convert progress to labeled badges |
| 47 | + * |
| 48 | + * @param array $progress The progress array |
| 49 | + * @return string The markdown for the image badges |
| 50 | + */ |
| 51 | +function progressToBadges(array $progress): string |
| 52 | +{ |
| 53 | + $per_row = 5; |
| 54 | + $badges = str_repeat("| ", $per_row) . "|" . "\n"; |
| 55 | + $badges .= str_repeat("| --- ", $per_row) . "|" . "\n"; |
| 56 | + $i = 0; |
| 57 | + foreach (array_values($progress) as $data) { |
| 58 | + $badges .= "| `{$data["locale"]}` - {$data["locale_name"]} <br /> ![{$data["locale_name"]} {$data["percentage"]}%](https://progress-bar.dev/{$data["percentage"]}) "; |
| 59 | + $i++; |
| 60 | + if ($i % $per_row === 0) { |
| 61 | + $badges .= "|\n"; |
| 62 | + } |
| 63 | + } |
| 64 | + if ($i % $per_row !== 0) { |
| 65 | + $badges .= "|\n"; |
| 66 | + } |
| 67 | + return $badges; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Update readme by replacing the content between the start and end markers |
| 72 | + * |
| 73 | + * @param string $path The path to the readme file |
| 74 | + * @param string $start The start marker |
| 75 | + * @param string $end The end marker |
| 76 | + * @param string $content The content to replace the content between the start and end markers |
| 77 | + * @return int|false The number of bytes that were written to the file, or false on failure |
| 78 | + */ |
| 79 | +function updateReadme(string $path, string $start, string $end, string $content): int|false |
| 80 | +{ |
| 81 | + $readme = file_get_contents($path); |
| 82 | + if (strpos($readme, $start) === false || strpos($readme, $end) === false) { |
| 83 | + throw new Exception("Start or end marker not found in readme"); |
| 84 | + } |
| 85 | + $start_pos = strpos($readme, $start) + strlen($start); |
| 86 | + $end_pos = strpos($readme, $end); |
| 87 | + $length = $end_pos - $start_pos; |
| 88 | + $readme = substr_replace($readme, $content, $start_pos, $length); |
| 89 | + return file_put_contents($path, $readme); |
| 90 | +} |
| 91 | + |
| 92 | +$progress = getProgress($GLOBALS["TRANSLATIONS"]); |
| 93 | +$badges = "\n" . progressToBadges($progress); |
| 94 | +$update = updateReadme( |
| 95 | + __DIR__ . "/../README.md", |
| 96 | + "<!-- TRANSLATION_PROGRESS_START -->", |
| 97 | + "<!-- TRANSLATION_PROGRESS_END -->", |
| 98 | + $badges |
| 99 | +); |
| 100 | +exit($update === false ? 1 : 0); |
0 commit comments