Skip to content

Commit 8844644

Browse files
authored
feat: Added locale aliases and normalization (#387)
1 parent b471819 commit 8844644

File tree

6 files changed

+146
-16
lines changed

6 files changed

+146
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ To enable a theme, append `&theme=` followed by the theme name to the end of the
8686
<!-- This section is automatically generated by the `translation-progress.php` script. -->
8787
<!-- prettier-ignore-start -->
8888
<!-- TRANSLATION_PROGRESS_START -->
89-
<table><tbody><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L28"><code>en</code></a> - English<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L28"><img src="https://progress-bar.dev/100" alt="English 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L37"><code>ar</code></a> - العربية<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L37"><img src="https://progress-bar.dev/100" alt="العربية 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L46"><code>bn</code></a> - বাংলা<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L46"><img src="https://progress-bar.dev/100" alt="বাংলা 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L66"><code>es</code></a> - español<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L66"><img src="https://progress-bar.dev/100" alt="español 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L74"><code>fa</code></a> - فارسی<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L74"><img src="https://progress-bar.dev/100" alt="فارسی 100%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L89"><code>he</code></a> - עברית<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L89"><img src="https://progress-bar.dev/100" alt="עברית 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L98"><code>hi</code></a> - हिन्दी<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L98"><img src="https://progress-bar.dev/100" alt="हिन्दी 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L118"><code>ja</code></a> - 日本語<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L118"><img src="https://progress-bar.dev/100" alt="日本語 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L133"><code>ko</code></a> - 한국어<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L133"><img src="https://progress-bar.dev/100" alt="한국어 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L141"><code>mr</code></a> - मराठी<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L141"><img src="https://progress-bar.dev/100" alt="मराठी 100%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L155"><code>pl</code></a> - polski<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L155"><img src="https://progress-bar.dev/100" alt="polski 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L163"><code>pt-br</code></a> - português (Brasil)<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L163"><img src="https://progress-bar.dev/100" alt="português (Brasil) 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L195"><code>zh</code></a> - 中文<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L195"><img src="https://progress-bar.dev/100" alt="中文 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L203"><code>zh_Hant</code></a> - 中文(繁體)<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L203"><img src="https://progress-bar.dev/100" alt="中文(繁體) 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L54"><code>da</code></a> - dansk<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L54"><img src="https://progress-bar.dev/67" alt="dansk 67%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L60"><code>de</code></a> - Deutsch<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L60"><img src="https://progress-bar.dev/67" alt="Deutsch 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L83"><code>fr</code></a> - français<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L83"><img src="https://progress-bar.dev/67" alt="français 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L106"><code>id</code></a> - Indonesia<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L106"><img src="https://progress-bar.dev/67" alt="Indonesia 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L112"><code>it</code></a> - italiano<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L112"><img src="https://progress-bar.dev/67" alt="italiano 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L127"><code>kn</code></a> - ಕನ್ನಡ<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L127"><img src="https://progress-bar.dev/67" alt="ಕನ್ನಡ 67%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L149"><code>nl</code></a> - Nederlands<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L149"><img src="https://progress-bar.dev/67" alt="Nederlands 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L171"><code>ru</code></a> - русский<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L171"><img src="https://progress-bar.dev/67" alt="русский 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L177"><code>ta</code></a> - தமிழ்<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L177"><img src="https://progress-bar.dev/67" alt="தமிழ் 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L183"><code>tr</code></a> - Türkçe<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L183"><img src="https://progress-bar.dev/67" alt="Türkçe 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L189"><code>vi</code></a> - Tiếng Việt<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L189"><img src="https://progress-bar.dev/67" alt="Tiếng Việt 67%"></a></td></tr></tbody></table>
89+
<table><tbody><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L33"><code>en</code></a> - English<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L33"><img src="https://progress-bar.dev/100" alt="English 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L42"><code>ar</code></a> - العربية<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L42"><img src="https://progress-bar.dev/100" alt="العربية 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L51"><code>bn</code></a> - বাংলা<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L51"><img src="https://progress-bar.dev/100" alt="বাংলা 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L71"><code>es</code></a> - español<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L71"><img src="https://progress-bar.dev/100" alt="español 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L79"><code>fa</code></a> - فارسی<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L79"><img src="https://progress-bar.dev/100" alt="فارسی 100%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L94"><code>he</code></a> - עברית<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L94"><img src="https://progress-bar.dev/100" alt="עברית 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L103"><code>hi</code></a> - हिन्दी<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L103"><img src="https://progress-bar.dev/100" alt="हिन्दी 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L123"><code>ja</code></a> - 日本語<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L123"><img src="https://progress-bar.dev/100" alt="日本語 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L138"><code>ko</code></a> - 한국어<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L138"><img src="https://progress-bar.dev/100" alt="한국어 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L146"><code>mr</code></a> - मराठी<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L146"><img src="https://progress-bar.dev/100" alt="मराठी 100%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L160"><code>pl</code></a> - polski<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L160"><img src="https://progress-bar.dev/100" alt="polski 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L168"><code>pt_BR</code></a> - português (Brasil)<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L168"><img src="https://progress-bar.dev/100" alt="português (Brasil) 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L201"><code>zh_Hans</code></a> - 中文(简体)<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L201"><img src="https://progress-bar.dev/100" alt="中文(简体) 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L209"><code>zh_Hant</code></a> - 中文(繁體)<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L209"><img src="https://progress-bar.dev/100" alt="中文(繁體) 100%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L59"><code>da</code></a> - dansk<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L59"><img src="https://progress-bar.dev/67" alt="dansk 67%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L65"><code>de</code></a> - Deutsch<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L65"><img src="https://progress-bar.dev/67" alt="Deutsch 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L88"><code>fr</code></a> - français<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L88"><img src="https://progress-bar.dev/67" alt="français 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L111"><code>id</code></a> - Indonesia<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L111"><img src="https://progress-bar.dev/67" alt="Indonesia 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L117"><code>it</code></a> - italiano<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L117"><img src="https://progress-bar.dev/67" alt="italiano 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L132"><code>kn</code></a> - ಕನ್ನಡ<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L132"><img src="https://progress-bar.dev/67" alt="ಕನ್ನಡ 67%"></a></td></tr><tr><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L154"><code>nl</code></a> - Nederlands<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L154"><img src="https://progress-bar.dev/67" alt="Nederlands 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L176"><code>ru</code></a> - русский<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L176"><img src="https://progress-bar.dev/67" alt="русский 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L182"><code>ta</code></a> - தமிழ்<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L182"><img src="https://progress-bar.dev/67" alt="தமிழ் 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L188"><code>tr</code></a> - Türkçe<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L188"><img src="https://progress-bar.dev/67" alt="Türkçe 67%"></a></td><td><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L194"><code>vi</code></a> - Tiếng Việt<br /><a href="https://github.com/DenverCoder1/github-readme-streak-stats/blob/main/src/translations.php#L194"><img src="https://progress-bar.dev/67" alt="Tiếng Việt 67%"></a></td></tr></tbody></table>
9090
<!-- TRANSLATION_PROGRESS_END -->
9191
<!-- prettier-ignore-end -->
9292

scripts/translation-progress.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function getProgress(array $translations): array
2222
$translations_file = file(__DIR__ . "/../src/translations.php");
2323
$progress = [];
2424
foreach ($translations as $locale => $phrases) {
25+
// skip aliases
26+
if (is_string($phrases)) {
27+
continue;
28+
}
2529
$translated = 0;
2630
foreach ($phrases_to_translate as $phrase) {
2731
if (isset($phrases[$phrase])) {

src/card.php

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,62 @@ function splitLines(string $text, int $maxChars, int $line1Offset): string
180180
);
181181
}
182182

183+
/**
184+
* Normalize a locale code
185+
*
186+
* @param string $localeCode Locale code
187+
*
188+
* @return string Normalized locale code
189+
*/
190+
function normalizeLocaleCode(string $localeCode): string
191+
{
192+
preg_match("/^([a-z]{2,3})(?:[_-]([a-z]{4}))?(?:[_-]([0-9]{3}|[a-z]{2}))?$/i", $localeCode, $matches);
193+
if (empty($matches)) {
194+
return "en";
195+
}
196+
$language = $matches[1];
197+
$script = $matches[2] ?? "";
198+
$region = $matches[3] ?? "";
199+
// convert language to lowercase
200+
$language = strtolower($language);
201+
// convert script to title case
202+
$script = ucfirst(strtolower($script));
203+
// convert region to uppercase
204+
$region = strtoupper($region);
205+
// combine language, script, and region using underscores
206+
return implode("_", array_filter([$language, $script, $region]));
207+
}
208+
209+
/**
210+
* Get the translations for a locale code after normalizing it
211+
*
212+
* @param string $localeCode Locale code
213+
*
214+
* @return array Translations for the locale code
215+
*/
216+
function getTranslations(string $localeCode)
217+
{
218+
// normalize locale code
219+
$localeCode = normalizeLocaleCode($localeCode);
220+
// get the labels from the translations file
221+
$translations = include "translations.php";
222+
// if the locale does not exist, try without the script and region
223+
if (!isset($translations[$localeCode])) {
224+
$localeCode = explode("_", $localeCode)[0];
225+
}
226+
// get the translations for the locale or empty array if it does not exist
227+
$localeTranslations = $translations[$localeCode] ?? [];
228+
// if the locale returned is a string, it is an alias for another locale
229+
if (is_string($localeTranslations)) {
230+
// get the translations for the alias
231+
$localeTranslations = $translations[$localeTranslations];
232+
}
233+
// fill in missing translations with English
234+
$localeTranslations += $translations["en"];
235+
// return the translations
236+
return $localeTranslations;
237+
}
238+
183239
/**
184240
* Generate SVG output for a stats array
185241
*
@@ -197,13 +253,9 @@ function generateCard(array $stats, array $params = null): string
197253
// get requested theme
198254
$theme = getRequestedTheme($params);
199255

200-
// get the labels from the translations file
201-
$translations = include "translations.php";
202256
// get requested locale, default to English
203257
$localeCode = $params["locale"] ?? "en";
204-
$localeTranslations = $translations[$localeCode] ?? [];
205-
// add missing translations from English
206-
$localeTranslations += $translations["en"];
258+
$localeTranslations = getTranslations($localeCode);
207259

208260
// whether the locale is right-to-left
209261
$direction = $localeTranslations["rtl"] ?? false ? "rtl" : "ltr";

0 commit comments

Comments
 (0)