@@ -180,6 +180,62 @@ function splitLines(string $text, int $maxChars, int $line1Offset): string
180
180
);
181
181
}
182
182
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
+
183
239
/**
184
240
* Generate SVG output for a stats array
185
241
*
@@ -197,13 +253,9 @@ function generateCard(array $stats, array $params = null): string
197
253
// get requested theme
198
254
$ theme = getRequestedTheme ($ params );
199
255
200
- // get the labels from the translations file
201
- $ translations = include "translations.php " ;
202
256
// get requested locale, default to English
203
257
$ localeCode = $ params ["locale " ] ?? "en " ;
204
- $ localeTranslations = $ translations [$ localeCode ] ?? [];
205
- // add missing translations from English
206
- $ localeTranslations += $ translations ["en " ];
258
+ $ localeTranslations = getTranslations ($ localeCode );
207
259
208
260
// whether the locale is right-to-left
209
261
$ direction = $ localeTranslations ["rtl " ] ?? false ? "rtl " : "ltr " ;
0 commit comments