Skip to content

Commit

Permalink
added language optional and fix horizontal card
Browse files Browse the repository at this point in the history
  • Loading branch information
t1gr0u committed May 8, 2023
1 parent 4b66599 commit 2b4e4f3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ entity: sensor.weather_station_uv
| show_error | boolean | **Optional** | Show what an error looks like for the card | `false` |
| show_warning | boolean | **Optional** | Show what a warning looks like for the card | `false` |
| entity | string | **Required** | Home Assistant entity ID. | `none` |
| language | string | **Optional** | The 2 character that determines the language| `en` |
| tap_action | object | **Optional** | Action to take on tap | `action: more-info` |
| hold_action | object | **Optional** | Action to take on hold | `none` |
| double_tap_action | object | **Optional** | Action to take on double tap | `none` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uv-index-card",
"version": "1.1.0",
"version": "1.2.0",
"description": "Lovelace uv-index-card",
"keywords": [
"home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CARD_VERSION = '1.1.0';
export const CARD_VERSION = '1.2.0';
18 changes: 18 additions & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { formfieldDefinition } from '../elements/formfield';
import { selectDefinition } from '../elements/select';
import { switchDefinition } from '../elements/switch';
import { textfieldDefinition } from '../elements/textfield';
import { CARD_LANGUAGES } from './localize/localize';

@customElement('uv-index-card-editor')
export class UVIndexCardEditor extends ScopedRegistryHost(LitElement) implements LovelaceCardEditor {
Expand Down Expand Up @@ -49,6 +50,10 @@ export class UVIndexCardEditor extends ScopedRegistryHost(LitElement) implements
return this._config?.entity || '';
}

get _language(): string {
return this._config?.language || '';
}

get _show_warning(): boolean {
return this._config?.show_warning || false;
}
Expand Down Expand Up @@ -85,6 +90,19 @@ export class UVIndexCardEditor extends ScopedRegistryHost(LitElement) implements
.configValue=${'name'}
@input=${this._valueChanged}
></mwc-textfield>
<mwc-select
naturalMenuWidth
fixedMenuPosition
label="Language (Optional)"
.configValue=${'language'}
.value=${this._language}
@selected=${this._valueChanged}
@closed=${(ev) => ev.stopPropagation()}
>
${CARD_LANGUAGES.map((languageItem) => {
return html`<mwc-list-item .value=${languageItem}>${languageItem}</mwc-list-item>`;
})}
</mwc-select>
<mwc-formfield .label=${`Toggle warning ${this._show_warning ? 'off' : 'on'}`}>
<mwc-switch
.checked=${this._show_warning !== false}
Expand Down
14 changes: 11 additions & 3 deletions src/localize/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ const languages: any = {
it: it,
};

export function localize(string: string, search = '', replace = ''): string {
const lang = (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_');
export const CARD_LANGUAGES = [...Object.keys(languages), ''].sort();

export function getLocalLanguage(): string {
return (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_');
}

export function localize(string: string, search = '', replace = '', language = '' ): string {
let translated: string;

if (language === '') {
language = getLocalLanguage();
}

try {
translated = string.split('.').reduce((o, i) => o[i], languages[lang]);
translated = string.split('.').reduce((o, i) => o[i], languages[language]);
} catch (e) {
translated = string.split('.').reduce((o, i) => o[i], languages['en']);
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface UVIndexCardConfig extends LovelaceCardConfig {
show_error?: boolean;
test_gui?: boolean;
entity?: string;
language?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
double_tap_action?: ActionConfig;
Expand Down
12 changes: 6 additions & 6 deletions src/uv-index-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ export class UVIndexCard extends LitElement {
protected render(): TemplateResult | void {
// TODO Check for stateObj or other necessary things and render a warning if missing
if (this.config.show_warning) {
return this._showWarning(localize('common.show_warning'));
return this._showWarning(localize('common.show_warning', '', '', this.config.language));
}

if (this.config.show_error) {
return this._showError(localize('common.show_error'));
return this._showError(localize('common.show_error', '', '', this.config.language));
}

const entityId = this.config.entity;
Expand Down Expand Up @@ -128,7 +128,7 @@ export class UVIndexCard extends LitElement {
>
<div style="display: flex;">
<div style="width: 50%; padding-left: 30px;">
<svg width="162px" height="136px" viewBox="0 0 162 136" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<svg width="80%" viewBox="0 0 162 136" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>UV</title>
<g id="UV-Index-Triangle" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<polygon points="81.9537723 2.99975159 77.2979826 10.4602611 86.4956236 10.4362484" fill="${stateValue >= 12 ? colours.extreme : colours.idle}"></polygon>
Expand All @@ -150,14 +150,14 @@ export class UVIndexCard extends LitElement {
<div>
<div>
<p>
<span style="font-weight: bold;">${localize('common.uv_index')}</span><br/>
<span style="font-weight: bold;">${localize('common.uv_index', '', '', this.config.language)}</span><br/>
${stateValue}
</p>
</div>
<div>
<p>
<span style="font-weight: bold;">${localize('common.uv_risk')}</span><br/>
${localize(uvRiskStr)}
<span style="font-weight: bold;">${localize('common.uv_risk', '', '', this.config.language)}</span><br/>
${localize(uvRiskStr, '', '', this.config.language)}
</p>
</div>
</div>
Expand Down

0 comments on commit 2b4e4f3

Please sign in to comment.