6
6
* Convert date from Y-M-D to more human-readable format
7
7
*
8
8
* @param string $dateString String in Y-M-D format
9
- * @param string $format Date format to use
9
+ * @param string|null $format Date format to use, or null to use locale default
10
+ * @param string $locale Locale code
10
11
* @return string Formatted Date string
11
12
*/
12
- function formatDate (string $ dateString , string $ format ): string
13
+ function formatDate (string $ dateString , string | null $ format, string $ locale ): string
13
14
{
14
15
$ date = new DateTime ($ dateString );
15
16
$ formatted = "" ;
17
+ $ patternGenerator = new IntlDatePatternGenerator ($ locale );
16
18
// if current year, display only month and day
17
19
if (date_format ($ date , "Y " ) == date ("Y " )) {
18
- // remove brackets and all text within them
19
- $ formatted = date_format ($ date , preg_replace ("/\[.*?\]/ " , "" , $ format ));
20
+ if ($ format ) {
21
+ // remove brackets and all text within them
22
+ $ formatted = date_format ($ date , preg_replace ("/\[.*?\]/ " , "" , $ format ));
23
+ } else {
24
+ // format without year using locale
25
+ $ pattern = $ patternGenerator ->getBestPattern ("MMM d " );
26
+ $ dateFormatter = new IntlDateFormatter ($ locale , IntlDateFormatter::MEDIUM , IntlDateFormatter::NONE , pattern: $ pattern );
27
+ $ formatted = $ dateFormatter ->format ($ date );
28
+ }
20
29
}
21
- // otherwise, display month, day, and year (just brackets removed)
30
+ // otherwise, display month, day, and year
22
31
else {
23
- $ formatted = date_format ($ date , str_replace (array ("[ " , "] " ), "" , $ format ));
32
+ if ($ format ) {
33
+ // remove brackets, but leave text within them
34
+ $ formatted = date_format ($ date , str_replace (["[ " , "] " ], "" , $ format ));
35
+ } else {
36
+ // format with year using locale
37
+ $ pattern = $ patternGenerator ->getBestPattern ("YYYY MMM d " );
38
+ $ dateFormatter = new IntlDateFormatter ($ locale , IntlDateFormatter::MEDIUM , IntlDateFormatter::NONE , pattern: $ pattern );
39
+ $ formatted = $ dateFormatter ->format ($ date );
40
+ }
24
41
}
25
42
// sanitize and return formatted date
26
43
return htmlspecialchars ($ formatted );
@@ -101,35 +118,36 @@ function generateCard(array $stats, array $params = null): string
101
118
$ theme = getRequestedTheme ($ params );
102
119
103
120
// get the labels from the translations file
104
- $ labels = include "translations.php " ;
121
+ $ translations = include "translations.php " ;
105
122
// get requested locale, default to English
106
- $ locale = $ params ["locale " ] ?? "en " ;
107
- // if the locale does not exist in the first value of the labels array, throw an exception
108
- if (!isset (reset ($ labels )[$ locale ])) {
109
- throw new InvalidArgumentException ("That locale is not supported. You can help by adding it to the translations file. " );
110
- }
123
+ $ localeCode = $ params ["locale " ] ?? "en " ;
124
+ $ localeTranslations = $ translations [$ localeCode ] ?? $ translations ["en " ];
111
125
112
126
// get date format
113
- $ dateFormat = $ params ["date_format " ] ?? "M j[, Y] " ;
127
+ // locale date formatter (used only if date_format is not specified)
128
+ $ dateFormat = $ params ["date_format " ] ?? $ localeTranslations ["date_format " ] ?? null ;
129
+
130
+ // number formatter
131
+ $ numFormatter = new NumberFormatter ($ localeCode , NumberFormatter::DECIMAL );
114
132
115
133
// total contributions
116
- $ totalContributions = $ stats ["totalContributions " ];
117
- $ firstContribution = formatDate ($ stats ["firstContribution " ], $ dateFormat );
134
+ $ totalContributions = $ numFormatter -> format ( $ stats ["totalContributions " ]) ;
135
+ $ firstContribution = formatDate ($ stats ["firstContribution " ], $ dateFormat, $ localeCode );
118
136
$ totalContributionsRange = $ firstContribution . " - Present " ;
119
137
120
138
// current streak
121
- $ currentStreak = $ stats ["currentStreak " ]["length " ];
122
- $ currentStreakStart = formatDate ($ stats ["currentStreak " ]["start " ], $ dateFormat );
123
- $ currentStreakEnd = formatDate ($ stats ["currentStreak " ]["end " ], $ dateFormat );
139
+ $ currentStreak = $ numFormatter -> format ( $ stats ["currentStreak " ]["length " ]) ;
140
+ $ currentStreakStart = formatDate ($ stats ["currentStreak " ]["start " ], $ dateFormat, $ localeCode );
141
+ $ currentStreakEnd = formatDate ($ stats ["currentStreak " ]["end " ], $ dateFormat, $ localeCode );
124
142
$ currentStreakRange = $ currentStreakStart ;
125
143
if ($ currentStreakStart != $ currentStreakEnd ) {
126
144
$ currentStreakRange .= " - " . $ currentStreakEnd ;
127
145
}
128
146
129
147
// longest streak
130
- $ longestStreak = $ stats ["longestStreak " ]["length " ];
131
- $ longestStreakStart = formatDate ($ stats ["longestStreak " ]["start " ], $ dateFormat );
132
- $ longestStreakEnd = formatDate ($ stats ["longestStreak " ]["end " ], $ dateFormat );
148
+ $ longestStreak = $ numFormatter -> format ( $ stats ["longestStreak " ]["length " ]) ;
149
+ $ longestStreakStart = formatDate ($ stats ["longestStreak " ]["start " ], $ dateFormat, $ localeCode );
150
+ $ longestStreakEnd = formatDate ($ stats ["longestStreak " ]["end " ], $ dateFormat, $ localeCode );
133
151
$ longestStreakRange = $ longestStreakStart ;
134
152
if ($ longestStreakStart != $ longestStreakEnd ) {
135
153
$ longestStreakRange .= " - " . $ longestStreakEnd ;
@@ -178,7 +196,7 @@ function generateCard(array $stats, array $params = null): string
178
196
<g transform='translate(1,84)'>
179
197
<rect width='163' height='50' stroke='none' fill='none'></rect>
180
198
<text x='81.5' y='32' stroke-width='0' text-anchor='middle' style='font-family:Segoe UI, Ubuntu, sans-serif;font-weight:400;font-size:14px;font-style:normal;fill: {$ theme ["sideLabels " ]};stroke:none; opacity: 0; animation: fadein 0.5s linear forwards 0.7s;'>
181
- {$ labels [ " totalContributions " ][ $ locale ]}
199
+ {$ localeTranslations [ " Total Contributions " ]}
182
200
</text>
183
201
</g>
184
202
@@ -203,7 +221,7 @@ function generateCard(array $stats, array $params = null): string
203
221
<g transform='translate(166,108)'>
204
222
<rect width='163' height='50' stroke='none' fill='none'></rect>
205
223
<text x='81.5' y='32' stroke-width='0' text-anchor='middle' style='font-family:Segoe UI, Ubuntu, sans-serif;font-weight:700;font-size:14px;font-style:normal;fill: {$ theme ["currStreakLabel " ]};stroke:none;opacity: 0; animation: fadein 0.5s linear forwards 0.9s;'>
206
- {$ labels [ " currentStreak " ][ $ locale ]}
224
+ {$ localeTranslations [ " Current Streak " ]}
207
225
</text>
208
226
</g>
209
227
@@ -239,7 +257,7 @@ function generateCard(array $stats, array $params = null): string
239
257
<g transform='translate(331,84)'>
240
258
<rect width='163' height='50' stroke='none' fill='none'></rect>
241
259
<text x='81.5' y='32' stroke-width='0' text-anchor='middle' style='font-family:Segoe UI, Ubuntu, sans-serif;font-weight:400;font-size:14px;font-style:normal;fill: {$ theme ["sideLabels " ]};stroke:none;opacity: 0; animation: fadein 0.5s linear forwards 1.3s;'>
242
- {$ labels [ " longestStreak " ][ $ locale ]}
260
+ {$ localeTranslations [ " Longest Streak " ]}
243
261
</text>
244
262
</g>
245
263
@@ -371,7 +389,7 @@ function renderOutput(string|array $output, int $responseCode = 200): void
371
389
// set content type to JSON
372
390
header ('Content-Type: application/json ' );
373
391
// generate array from output
374
- $ data = gettype ($ output ) === "string " ? array ( "error " => $ output) : $ output ;
392
+ $ data = gettype ($ output ) === "string " ? [ "error " => $ output] : $ output ;
375
393
// output as JSON
376
394
echo json_encode ($ data );
377
395
}
0 commit comments