Skip to content

Commit bdc7305

Browse files
authored
feat: add translation ability (#29)
* add translation file * enable translations * add translation files * add missing key * add translation * format * refactor * fix findings * translate loading message * add header * change translation * handle empty header * add sample * add fix script * fix * change header
1 parent f29fb7a commit bdc7305

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

MMM-AirQuality.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ Module.register('MMM-AirQuality', {
2323
DATA: 'AIR_QUALITY_DATA',
2424
DATA_RESPONSE: 'AIR_QUALITY_DATA_RESPONSE',
2525
},
26+
colors: {
27+
GOOD: '#009966',
28+
MODERATE: '#ffde33',
29+
UNHEALTHY_FOR_SENSITIVE_GROUPS: '#ff9933',
30+
UNHEALTHY: '#cc0033',
31+
HAZARDOUS: '#7e0023',
32+
UNKNOWN: '#333333',
33+
},
2634
start: function () {
2735
const self = this
2836
Log.info(`Starting module: ${this.name}`)
@@ -42,23 +50,19 @@ Module.register('MMM-AirQuality', {
4250
this.data.value = data.aqi
4351
this.data.city = data.city.name
4452
this.loaded = true
45-
46-
if (data.aqi < 51) {
47-
this.data.color = '#009966'
48-
this.data.impact = 'Good'
49-
} else if (data.aqi < 101) {
50-
this.data.color = '#ffde33'
51-
this.data.impact = 'Moderate'
52-
} else if (data.aqi < 151) {
53-
this.data.color = '#ff9933'
54-
this.data.impact = 'Unhealty for Sensitive Groups'
55-
} else if (data.aqi < 201) {
56-
this.data.color = '#cc0033'
57-
this.data.impact = 'Unhealthy'
58-
} else if (data.aqi < 301) {
59-
this.data.color = '#7e0023'
60-
this.data.impact = 'Hazardous'
61-
}
53+
this.data.impact = this.getImpact(data.aqi)
54+
this.data.color = this.getColor(this.data.impact)
55+
},
56+
getImpact: function (aqi) {
57+
if (aqi < 51) return 'GOOD'
58+
if (aqi < 101) return 'MODERATE'
59+
if (aqi < 151) return 'UNHEALTHY_FOR_SENSITIVE_GROUPS'
60+
if (aqi < 201) return 'UNHEALTHY'
61+
if (aqi < 301) return 'HAZARDOUS'
62+
return 'UNKNOWN'
63+
},
64+
getColor: function (impact) {
65+
return this.colors[impact]
6266
},
6367
html: {
6468
icon: '<i class="fa-solid fa-smog"></i>',
@@ -77,8 +81,15 @@ Module.register('MMM-AirQuality', {
7781
// Override getHeader method.
7882
getHeader: function () {
7983
let header = ''
80-
if (this.data.header) { header += this.data.header }
81-
if (this.config.appendLocationNameToHeader) {
84+
if (this.data.header !== '') {
85+
if (this.data.header === undefined) {
86+
header += this.translate('HEADER')
87+
} else {
88+
header += this.data.header
89+
}
90+
}
91+
92+
if (this.loaded && this.config.appendLocationNameToHeader) {
8293
if (header !== '') {
8394
header += ' '
8495
}
@@ -101,19 +112,25 @@ Module.register('MMM-AirQuality', {
101112
}
102113

103114
if (!this.loaded) {
104-
wrapper.innerHTML = 'Loading air quality index ...'
115+
wrapper.innerHTML = this.translate('LOADING')
105116
wrapper.className = 'dimmed light small'
106117
return wrapper
107118
}
108119
wrapper.innerHTML =
109120
this.html.quality.format(
110121
this.data.color,
111122
this.html.icon,
112-
this.data.impact,
123+
this.translate(this.data.impact),
113124
(this.config.showIndex ? ' (' + this.data.value + ')' : '')) +
114125
(this.config.showLocation && !this.config.appendLocationNameToHeader ? this.html.city.format(this.data.city) : '')
115126
return wrapper
116127
},
128+
getTranslations: function () {
129+
return {
130+
en: 'l10n/en.json', // fallback language
131+
de: 'l10n/de.json',
132+
}
133+
},
117134
socketNotificationReceived: function (notification, payload) {
118135
const self = this
119136
Log.debug('received ' + notification)

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ Add the module configuration to your `config.js` file.
3535
{
3636
module: 'MMM-AirQuality',
3737
position: 'top_center', // you may choose any location
38-
header: 'AQI', //choose a header if you like
3938
config: {
40-
location: 'beijing' // the location to check the index for
4139
token: '' // add your token here
40+
location: 'beijing' // the location to check the index for
4241
}
4342
},
4443
```

l10n/de.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"HEADER": "Luftqualität",
3+
"GOOD": "gut",
4+
"MODERATE": "moderat",
5+
"UNHEALTHY_FOR_SENSITIVE_GROUPS": "ungesund für gefährdete Gruppen",
6+
"UNHEALTHY": "ungesund",
7+
"HAZARDOUS": "gefährlich",
8+
"UNKNOWN": "unbekannt"
9+
}

l10n/en.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"HEADER": "Air Quality",
3+
"GOOD": "Good",
4+
"MODERATE": "Moderate",
5+
"UNHEALTHY_FOR_SENSITIVE_GROUPS": "Unhealty for Sensitive Groups",
6+
"UNHEALTHY": "Unhealthy",
7+
"HAZARDOUS": "Hazardous",
8+
"UNKNOWN": "Unknown"
9+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"scripts": {
2222
"validate": "npm run validate:js && npm run validate:md",
2323
"validate:js": "eslint **/*.js",
24-
"validate:md": "node_modules/.bin/markdownlint *.md"
24+
"validate:md": "node_modules/.bin/markdownlint *.md",
25+
"fix:js": "eslint **/*.js --fix"
2526
},
2627
"devDependencies": {
2728
"eslint": "8.56.0",

0 commit comments

Comments
 (0)