-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathcomponents.ts
More file actions
376 lines (341 loc) · 11.7 KB
/
components.ts
File metadata and controls
376 lines (341 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import {
type createLunaria,
type Locale,
type LunariaConfig,
type LunariaStatus,
type StatusEntry,
} from '@lunariajs/core'
import { BaseStyles, CustomStyles } from './styles.ts'
import type { I18nStatus } from '../shared/types/i18n-status.ts'
export function html(
strings: TemplateStringsArray,
...values: ((string | number) | (string | number)[])[]
) {
const treatedValues = values.map(value => (Array.isArray(value) ? value.join('') : value))
return String.raw({ raw: strings }, ...treatedValues)
}
type LunariaInstance = Awaited<ReturnType<typeof createLunaria>>
function collapsePath(path: string) {
const basesToHide = ['src/content/docs/en/', 'src/i18n/en/', 'src/content/docs/', 'src/content/']
for (const base of basesToHide) {
const newPath = path.replace(base, '')
if (newPath === path) continue
return newPath
}
return path
}
export const Page = (
config: LunariaConfig,
status: I18nStatus,
_lunaria: LunariaInstance, // currenly not in use
): string => {
return html`
<!doctype html>
<html dir="ltr" lang="en">
<head>
${Meta} ${BaseStyles} ${CustomStyles}
</head>
<body>
${Body(config, status)}
</body>
</html>
`
}
const Meta = html`
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<title>npmx - Translation Status</title>
<meta
name="description"
content="Translation progress tracker for the npmx site. See how much has been translated in your language and get involved!"
/>
<meta property="last-build" content="${new Date(Date.now()).toString()}" />
<link rel="canonical" href="https://i18n.npmx.dev/" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Geist+Mono:wght@100..900&family=Geist:wght@100..900&display=swap"
rel="stylesheet"
/>
<meta property="og:title" content="npmx - Translation Status" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://i18n.npmx.dev/" />
<meta
property="og:description"
content="Translation progress tracker for the npmx site. See how much has been translated in your language and get involved!"
/>
<link rel="icon" href="https://npmx.dev/favicon.ico" type="image/x-icon" />
<link rel="icon" href="https://npmx.dev/favicon.svg" type="image/svg+xml" />
`
const Body = (config: LunariaConfig, status: I18nStatus): string => {
return html`
<main>
<div class="limit-to-viewport">
<h1>npmx Translation Status</h1>
${TitleParagraph} ${StatusByLocale(config, status)}
</div>
</main>
`
}
const StatusByLocale = (config: LunariaConfig, status: I18nStatus): string => {
const { locales } = config
return html`
<h2 id="by-locale">
<a href="#by-locale">Translation progress by locale</a>
</h2>
${locales.map(locale => LocaleDetails(status, locale))}
`
}
const LocaleDetails = (status: I18nStatus, locale: Locale): string => {
const { label, lang } = locale
const localeStatus = status.locales.find(s => s.lang === lang)
if (!localeStatus) {
return ''
}
const {
missingKeys,
percentComplete,
totalKeys,
completedKeys,
githubEditUrl,
githubHistoryUrl,
} = localeStatus
return html`
<details class="progress-details">
<summary>
<strong>${label} <span class="lang-code">${lang}</span></strong>
<hr />
<div class="progress-summary">
<span>
${missingKeys.length ? `${missingKeys.length.toString()} missing keys` : '✔'}
</span>
<span>${completedKeys} / ${totalKeys}</span>
</div>
${ProgressBar(percentComplete)}
</summary>
<br />
${ContentDetailsLinks(
{ text: `i18n/locales/${lang}.json`, url: githubEditUrl },
githubHistoryUrl,
)}
<br />
<br />
${missingKeys.length > 0
? html`${MissingKeysList(missingKeys)}`
: html` <p>This translation is complete, amazing job! 🎉</p> `}
</details>
`
}
const MissingKeysList = (missingKeys: string[]): string => {
return html`<details>
<summary>Show missing keys</summary>
<ul>
${missingKeys.map(key => html`<li>${key}</li>`)}
</ul>
</details>`
}
const ContentDetailsLinks = (
githubEditLink: { text: string; url: string },
githubHistoryUrl: string,
): string => {
return html`
${Link(githubEditLink.url, githubEditLink.text)} |
${Link(githubHistoryUrl, 'source change history')}
`
}
const ProgressBar = (percentComplete: number): string => {
let barClass = 'completed'
if (percentComplete > 99) {
barClass = 'completed' // dark-green
} else if (percentComplete > 90) {
barClass = 'very-good' // green
} else if (percentComplete > 75) {
barClass = 'good' // orange
} else if (percentComplete > 50) {
barClass = 'help-needed' // red
} else {
barClass = 'basic' // dark-red
}
return html`
<div class="progress-bar-wrapper" aria-hidden="true">
<div class="progress-bar ${barClass}" style="width:${percentComplete}%;"></div>
</div>
`
}
const Link = (href: string, text: string): string => {
return html`<a href="${href}" target="_blank" rel="noopener noreferrer">${text}</a>`
}
const TitleParagraph = html`
<p>
If you're interested in helping us translate
<a href="https://npmx.dev/">npmx.dev</a> into one of the languages listed below, you've come to
the right place! This auto-updating page always lists all the content that could use your help
right now.
</p>
<p>
Before starting, please read our
<a href="https://github.com/npmx-dev/npmx.dev/blob/main/CONTRIBUTING.md#localization-i18n"
>localization (i18n) guide</a
>
to learn about our translation process and how you can get involved.
</p>
`
// Components from here are not used at the moment
// Do not delete as we might use it if we split translations in multiple files for locale
const _StatusByFile = (
config: LunariaConfig,
status: LunariaStatus,
lunaria: LunariaInstance,
): string => {
const { locales } = config
return html`
<h2 id="by-file">
<a href="#by-file">Translation status by file</a>
</h2>
<div class="status-by-file-wrapper">
<table class="status-by-file">
<thead>
<tr>
${['File', ...locales.map(({ lang }) => lang)].map(col => html`<th>${col}</th>`)}
</tr>
</thead>
${TableBody(status, locales, lunaria)}
</table>
</div>
<sup class="capitalize">❌ missing 🔄 outdated ✔ done </sup>
`
}
const TableBody = (status: LunariaStatus, locales: Locale[], lunaria: LunariaInstance): string => {
const links = lunaria.gitHostingLinks()
return html`
<tbody>
${status.map(
file =>
html`
<tr>
<td>${Link(links.source(file.source.path), collapsePath(file.source.path))}</td>
${locales.map(({ lang }) => {
return TableContentStatus(file.localizations, lang, lunaria, file.type)
})}
</td>
</tr>`,
)}
</tbody>
`
}
const TableContentStatus = (
localizations: StatusEntry['localizations'],
lang: string,
lunaria: LunariaInstance,
fileType?: string,
): string => {
const localization = localizations.find(localizationItem => localizationItem.lang === lang)!
const isMissingKeys = 'missingKeys' in localization && localization.missingKeys.length > 0
// For dictionary files, status is determined solely by key completion:
// if there are missing keys it's "outdated", if all keys are present it's "up-to-date",
// regardless of git history. This prevents variants with merge coverage (e.g. en-US, en-GB)
// from showing as outdated when their keys are fully covered by the base locale.
const status =
fileType === 'dictionary'
? isMissingKeys
? 'outdated'
: localization.status === 'missing'
? 'missing'
: 'up-to-date'
: isMissingKeys
? 'outdated'
: localization.status
const links = lunaria.gitHostingLinks()
const link =
status === 'missing' ? links.create(localization.path) : links.source(localization.path)
return html`<td>${EmojiFileLink(link, status)}</td>`
}
const EmojiFileLink = (
href: string | null,
type: 'missing' | 'outdated' | 'up-to-date',
): string => {
const statusTextOpts = {
'missing': 'missing',
'outdated': 'outdated',
'up-to-date': 'done',
} as const
const statusEmojiOpts = {
'missing': '❌',
'outdated': '🔄',
'up-to-date': '✔',
} as const
return href
? html`<a href="${href}" title="${statusTextOpts[type]}">
<span aria-hidden="true">${statusEmojiOpts[type]}</span>
</a>`
: html`<span title="${statusTextOpts[type]}">
<span aria-hidden="true">${statusEmojiOpts[type]}</span>
</span>`
}
const _CreateFileLink = (href: string, text: string): string => {
return html`<a class="create-button" href="${href}">${text}</a>`
}
/**
* Build an SVG file showing a summary of each language's translation progress.
*/
const _SvgSummary = (config: LunariaConfig, status: LunariaStatus): string => {
const localeHeight = 56 // Each locale’s summary is 56px high.
const svgHeight = localeHeight * Math.ceil(config.locales.length / 2)
return html`<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 400 ${svgHeight}"
font-family="ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'"
>
${config.locales
.map(locale => SvgLocaleSummary(status, locale))
.sort((a, b) => b.progress - a.progress)
.map(
({ svg }, index) =>
html`<g transform="translate(${(index % 2) * 215} ${Math.floor(index / 2) * 56})"
>${svg}</g
>`,
)}
</svg>`
}
function SvgLocaleSummary(
status: LunariaStatus,
{ label, lang }: Locale,
): { svg: string; progress: number } {
const missingFiles = status.filter(
file => file.localizations.find(l => l.lang === lang)?.status === 'missing',
)
const outdatedFiles = status.filter(file => {
const localization = file.localizations.find(localizationItem => localizationItem.lang === lang)
if (!localization || localization.status === 'missing') {
return false
} else if (file.type === 'dictionary') {
return 'missingKeys' in localization ? localization.missingKeys.length > 0 : false
} else {
return (
localization.status === 'outdated' ||
('missingKeys' in localization && localization.missingKeys.length > 0)
)
}
})
const doneLength = status.length - outdatedFiles.length - missingFiles.length
const barWidth = 184
const doneFraction = doneLength / status.length
const outdatedFraction = outdatedFiles.length / status.length
const doneWidth = (doneFraction * barWidth).toFixed(2)
const outdatedWidth = ((outdatedFraction + doneFraction) * barWidth).toFixed(2)
return {
progress: doneFraction,
svg: html`<text x="0" y="12" font-size="11" font-weight="600" fill="#999"
>${label} (${lang})</text
>
<text x="0" y="26" font-size="9" fill="#999">
${missingFiles.length == 0 && outdatedFiles.length == 0
? '100% complete, amazing job! 🎉'
: html`${doneLength} done, ${outdatedFiles.length} outdated, ${missingFiles.length}
missing`}
</text>
<rect x="0" y="34" width="${barWidth}" height="8" fill="#999" opacity="0.25"></rect>
<rect x="0" y="34" width="${outdatedWidth}" height="8" fill="#fb923c"></rect>
<rect x="0" y="34" width="${doneWidth}" height="8" fill="#c084fc"></rect>`,
}
}