-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
weblate-project-translated-percentage.service.js
67 lines (59 loc) · 1.98 KB
/
weblate-project-translated-percentage.service.js
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
import Joi from 'joi'
import { colorScale } from '../color-formatters.js'
import WeblateBase from './weblate-base.js'
const schema = Joi.object({
translated_percent: Joi.number().required(),
}).required()
/**
* This badge displays the percentage of strings translated on a project on a
* Weblate instance.
*/
export default class WeblateProjectTranslatedPercentage extends WeblateBase {
static category = 'other'
static route = {
base: 'weblate/progress',
pattern: ':project',
queryParamSchema: this.queryParamSchema,
}
static examples = [
{
title: 'Weblate project translated',
namedParams: { project: 'godot-engine' },
queryParams: { server: 'https://hosted.weblate.org' },
staticPreview: this.render({ translatedPercent: 20.5 }),
keywords: ['i18n', 'translation', 'internationalization'],
},
]
static defaultBadgeData = { label: 'translated' }
/**
* Takes a percentage and maps it to a message and color.
*
* The colors are determined based on how Weblate does it internally.
* {@link https://github.com/WeblateOrg/weblate/blob/main/weblate/trans/widgets.py Weblate on GitHub}
*
* @param {*} translatedPercent The percentage of translations translated.
* @returns {object} Format for the badge.
*/
static render({ translatedPercent }) {
const color = colorScale([75, 90])(translatedPercent)
return { message: `${translatedPercent.toFixed(0)}%`, color }
}
async fetch({ project, server = 'https://hosted.weblate.org' }) {
return super.fetch({
schema,
url: `${server}/api/projects/${project}/statistics/`,
errorMessages: {
403: 'access denied by remote server',
404: 'project not found',
429: 'rate limited by remote server',
},
})
}
async handle({ project }, { server }) {
const { translated_percent: translatedPercent } = await this.fetch({
project,
server,
})
return this.constructor.render({ translatedPercent })
}
}