Skip to content

Commit 47e0fe8

Browse files
authored
docs: Add translation progress (#365)
1 parent c6d5000 commit 47e0fe8

File tree

3 files changed

+141
-5
lines changed

3 files changed

+141
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Update Translation Progress
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- uses: php-actions/composer@v6
16+
with:
17+
php_extensions: intl
18+
19+
- name: Update Translations
20+
run: php scripts/translation-progress.php
21+
22+
- name: Commit Changes
23+
uses: EndBug/add-and-commit@v7
24+
with:
25+
author_name: GitHub Actions
26+
author_email: github-actions[bot]@users.noreply.github.com
27+
message: "docs(readme): Update translation progress"

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ If the `theme` parameter is specified, any color customizations specified will b
5959
| `currStreakLabel` | Current streak label | **hex code** without `#` or **css color** |
6060
| `sideLabels` | Total and longest streak labels | **hex code** without `#` or **css color** |
6161
| `dates` | Date range text color | **hex code** without `#` or **css color** |
62-
| `date_format` | Date format (Default: `M j[, Y]`) | See note below on [Date Formats](#date-formats) |
63-
| `locale` | Locale to use for labels (Default: `en`) | ISO 639-1 code (See [`translations.php`](./src/translations.php)) |
62+
| `date_format` | Date format (Default: `M j[, Y]`) | See note below on [Date Formats](#-date-formats) |
63+
| `locale` | Locale to use for labels (Default: `en`) | ISO 639-1 code (See [Locales](#-locales) |
6464
| `type` | Output format (Default: `svg`) | Current options: `svg`, `png` or `json` |
6565
| `mode` | Streak mode (Default: `daily`) | `daily` (contribute daily) or `weekly` (contribute once per Sun-Sat week) |
6666

67-
## 🖌 Themes
67+
### 🖌 Themes
6868

69-
To enable a theme, append `&theme=` followed by the theme name to the end of the source url:
69+
To enable a theme, append `&theme=` followed by the theme name to the end of the source URL:
7070

7171
```md
7272
[![GitHub Streak](https://streak-stats.demolab.com/?user=DenverCoder1&theme=dark)](https://git.io/streak-stats)
@@ -81,7 +81,16 @@ To enable a theme, append `&theme=` followed by the theme name to the end of the
8181

8282
**If you have come up with a new theme you'd like to share with others, open an issue to add it!**
8383

84-
### Date Formats
84+
### 🗪 Locales
85+
86+
<!-- prettier-ignore-start -->
87+
<!-- TRANSLATION_PROGRESS_START -->
88+
<!-- TRANSLATION_PROGRESS_END -->
89+
<!-- prettier-ignore-end -->
90+
91+
**If you would like to help translate the Streak Stats cards, please see [Issue #236](https://github.com/DenverCoder1/github-readme-streak-stats/issues/236) for more information.**
92+
93+
### 📅 Date Formats
8594

8695
A custom date format can be specified by passing a string to the `date_format` parameter.
8796

scripts/translation-progress.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)