Skip to content

Commit 36a1647

Browse files
authored
Feature/translations (#5)
* Add translated texts * Fix incorrect translation
1 parent 2aae1b5 commit 36a1647

File tree

1 file changed

+69
-22
lines changed

1 file changed

+69
-22
lines changed

dist/vasttrafik-card.js

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,53 @@ customElements.whenDefined('card-tools').then(() => {
33

44
class VasttrafikCard extends ct.LitElement {
55

6-
static async getConfigElement() {
6+
static async getConfigElement() {
77
await import("./vasttrafik-card-editor.js");
88
return document.createElement("vasttrafik-card-editor");
99
}
10-
10+
1111
static get properties() {
1212
return {
1313
config: {},
1414
hass: {},
15+
translations: {},
16+
};
17+
}
18+
19+
constructor() {
20+
super();
21+
22+
this.translations = {
23+
'en': {
24+
'departureTime': 'Time',
25+
'departureStation': 'From',
26+
'leaveHome': 'Leave in',
27+
},
28+
'sv': {
29+
'departureTime': 'Avgår kl.',
30+
'departureStation': 'Från',
31+
'leaveHome': 'Gå om',
32+
},
33+
'nb': {
34+
'departureTime': 'Avgang kl.',
35+
'departureStation': 'Går fra',
36+
'leaveHome': 'Gå om',
37+
},
38+
'nn': {
39+
'departureTime': 'Avgang kl.',
40+
'departureStation': 'Går fra',
41+
'leaveHome': 'Gå om',
42+
},
43+
'da': {
44+
'departureTime': 'Afgang kl.',
45+
'departureStation': 'Afgår fra',
46+
'leaveHome': 'Afsted om',
47+
},
48+
'nl': {
49+
'departureTime': 'Vertrektijd',
50+
'departureStation': 'Van',
51+
'leaveHome': 'Vertrek over',
52+
},
1553
};
1654
}
1755

@@ -34,36 +72,36 @@ customElements.whenDefined('card-tools').then(() => {
3472
}
3573

3674
render() {
37-
if (!this.isVerified) {
75+
if (!this.isVerified) {
3876
this._verifyEntities();
3977
this.isVerified = true;
4078
}
41-
79+
4280
this._sortEntities();
4381
const renderedEntities = this.entities.map(entity => this._renderEntity(entity));
4482
const linesCssFile = `lines-${this.municipality.toLowerCase().replace(' ', '-').replace('å', 'a').replace('ä', 'a').replace('ö', 'o')}.css`;
4583

4684
return ct.LitHtml`
47-
<link type="text/css" rel="stylesheet" href="/local/community/lovelace-vasttrafik-card/vasttrafik-card.css"></link>
85+
<link type="text/css" rel="stylesheet" href="/local/community/lovelace-vasttrafik-card/vasttrafik-card.css"></link>
4886
<link type="text/css" rel="stylesheet" href="/local/community/lovelace-vasttrafik-card/${linesCssFile}"></link>
49-
<ha-card>
50-
<div class="card-header">
51-
${this.title}
52-
</div>
53-
<div>
54-
<table>
55-
<tr>
56-
<th align="left"></th>
57-
<th align="left">Time</th>
58-
<th align="left">From</th>
59-
<th align="left">Leave home</th>
60-
</tr>
61-
${renderedEntities}
62-
</table>
63-
</div>
64-
</ha-card>`;
87+
<ha-card>
88+
<div class="card-header">
89+
${this.title}
90+
</div>
91+
<div>
92+
<table>
93+
<tr>
94+
<th align="left"></th>
95+
<th align="left">${this._getTranslatedText('departureTime')}</th>
96+
<th align="left">${this._getTranslatedText('departureStation')}</th>
97+
<th align="left">${this._getTranslatedText('leaveHome')}</th>
98+
</tr>
99+
${renderedEntities}
100+
</table>
101+
</div>
102+
</ha-card>`;
65103
}
66-
104+
67105
_verifyEntities() {
68106
this.entities
69107
.filter(entity => !!this.hass.states[entity.id])
@@ -128,6 +166,15 @@ customElements.whenDefined('card-tools').then(() => {
128166

129167
return hourDiff * 60 + minuteDiff - entity.delay;
130168
}
169+
170+
_getTranslatedText(textKey) {
171+
let language = this.hass?.language || 'en';
172+
if (!this.translations.hasOwnProperty(language)) {
173+
console.debug(`Language "${language}" is not configured, using english instead`);
174+
language = 'en';
175+
}
176+
return this.translations[language][textKey] || 'Unknown';
177+
}
131178
}
132179

133180
customElements.define('vasttrafik-card', VasttrafikCard);

0 commit comments

Comments
 (0)