Skip to content

Commit

Permalink
Added value localization
Browse files Browse the repository at this point in the history
Added missing translations
  • Loading branch information
selvalt7 committed Nov 1, 2024
1 parent 86f544d commit 100dd42
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/editor/components/entityEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class EntityEditor extends LitElement {
return this.hass.localize(`ui.panel.lovelace.editor.card.mgc.${schema.name}`);
}

localizeValue(key) {
const localized = this.hass.localize(`ui.panel.lovelace.editor.card.mgc.values.${key}`);
if (localized !== '') {
return localized;
}
return key;
}

computeHelper(schema, data) {
if (schema.name === 'entity') {
return data.entity;
Expand Down Expand Up @@ -49,6 +57,7 @@ class EntityEditor extends LitElement {
.schema=${ENTITYSCHEMA}
.computeLabel=${this.computeLabel}
.computeHelper=${schema => this.computeHelper(schema, isObject ? this.config : DATA)}
.localizeValue=${this.localizeValue}
@value-changed=${this.valueChanged}
></ha-form>
`;
Expand Down
17 changes: 15 additions & 2 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,23 @@ class MiniGraphCardEditor extends LitElement {
}

computeLabel(schema) {
const localized = this.hass.localize(`ui.panel.lovelace.editor.card.generic.${schema.name}`);
let localized = this.hass.localize(`ui.panel.lovelace.editor.card.generic.${schema.name}`);
if (localized !== '') {
return localized;
}
return this.hass.localize(`ui.panel.lovelace.editor.card.mgc.${schema.name}`);
localized = this.hass.localize(`ui.panel.lovelace.editor.card.mgc.${schema.name}`);
if (localized !== '') {
return localized;
}
return schema.name;
}

localizeValue(key) {
const localized = this.hass.localize(`ui.panel.lovelace.editor.card.mgc.values.${key}`);
if (localized !== '') {
return localized;
}
return key;
}

render() {
Expand Down Expand Up @@ -107,6 +119,7 @@ class MiniGraphCardEditor extends LitElement {
.data=${DATA}
.schema=${MAINSCHEMA}
.computeLabel=${this.computeLabel}
.localizeValue=${this.localizeValue}
@value-changed=${this.valueChanged}
></ha-form>
</div>
Expand Down
36 changes: 26 additions & 10 deletions src/editor/editorConst.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {

const MAINSCHEMA = [
{
name: '',
name: 'appearance',
type: 'expandable',
iconPath: mdiPalette,
title: 'Appearance',
flatten: true,
schema: [
{
name: '',
Expand Down Expand Up @@ -53,8 +53,11 @@ const MAINSCHEMA = [
{ label: 'First', value: 'first' },
{ label: 'Last', value: 'last' },
{ label: 'Sum', value: 'sum' },
{ label: 'Delta', value: 'delta' },
{ label: 'Diff', value: 'diff' },
],
mode: 'dropdown',
translation_key: 'aggregate_func',
},
},
},
Expand All @@ -68,6 +71,7 @@ const MAINSCHEMA = [
{ label: 'Hour', value: 'hour' },
],
mode: 'dropdown',
translation_key: 'group_by',
},
},
},
Expand All @@ -92,6 +96,7 @@ const MAINSCHEMA = [
{ label: 'Hard', value: 'hard' },
],
mode: 'dropdown',
translation_key: 'transition',
},
},
},
Expand All @@ -106,10 +111,10 @@ const MAINSCHEMA = [
],
},
{
name: '',
name: 'bounds',
type: 'expandable',
iconPath: mdiArrowExpandVertical,
title: 'Bounds',
flatten: true,
schema: [
{
name: '',
Expand Down Expand Up @@ -150,10 +155,10 @@ const MAINSCHEMA = [
],
},
{
name: '',
name: 'alignment',
type: 'expandable',
iconPath: mdiAlignHorizontalLeft,
title: 'Alignment',
flatten: true,
schema: [
{
name: '',
Expand All @@ -170,6 +175,7 @@ const MAINSCHEMA = [
{ label: 'Center', value: 'center' },
],
mode: 'dropdown',
translation_key: 'alignment',
},
},
},
Expand All @@ -183,6 +189,7 @@ const MAINSCHEMA = [
{ label: 'State', value: 'state' },
],
mode: 'dropdown',
translation_key: 'alignment',
},
},
},
Expand All @@ -196,6 +203,7 @@ const MAINSCHEMA = [
{ label: 'Center', value: 'center' },
],
mode: 'dropdown',
translation_key: 'alignment',
},
},
},
Expand All @@ -207,7 +215,6 @@ const MAINSCHEMA = [
name: 'show',
type: 'expandable',
iconPath: mdiEye,
title: 'Display',
schema: [
{
name: '',
Expand All @@ -233,6 +240,7 @@ const MAINSCHEMA = [
{ label: 'Last', value: 'last' },
],
mode: 'dropdown',
translation_key: 'state',
},
},
},
Expand All @@ -246,6 +254,7 @@ const MAINSCHEMA = [
{ label: 'Hide', value: 'hide' },
],
mode: 'dropdown',
translation_key: 'graph',
},
},
},
Expand All @@ -259,6 +268,7 @@ const MAINSCHEMA = [
{ label: 'Fade', value: 'fade' },
],
mode: 'dropdown',
translation_key: 'fill',
},
},
},
Expand All @@ -272,6 +282,7 @@ const MAINSCHEMA = [
{ label: 'Hover', value: 'hover' },
],
mode: 'dropdown',
translation_key: 'points',
},
},
},
Expand All @@ -285,12 +296,12 @@ const MAINSCHEMA = [
{ label: 'Hover', value: 'hover' },
],
mode: 'dropdown',
translation_key: 'labels',
},
},
},
{
name: 'labels_secondary',
default: 'burba',
selector: {
select: {
options: [
Expand All @@ -299,6 +310,7 @@ const MAINSCHEMA = [
{ label: 'Hover', value: 'hover' },
],
mode: 'dropdown',
translation_key: 'labels',
},
},
},
Expand Down Expand Up @@ -436,18 +448,21 @@ const ENTITYSCHEMA = [
{ label: 'First', value: 'first' },
{ label: 'Last', value: 'last' },
{ label: 'Sum', value: 'sum' },
{ label: 'Delta', value: 'delta' },
{ label: 'Diff', value: 'diff' },
],
mode: 'dropdown',
translation_key: 'aggregate_func',
},
},
},
],
},
{
name: '',
name: 'show',
type: 'expandable',
iconPath: mdiEye,
title: 'Display',
flatten: true,
schema: [
{
name: '',
Expand Down Expand Up @@ -508,6 +523,7 @@ const ENTITYSCHEMA = [
{ label: 'Primary', value: 'primary' },
{ label: 'Secondary', value: 'secondary' },
],
translation_key: 'y_axis',
},
},
},
Expand Down
84 changes: 83 additions & 1 deletion src/localize/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,87 @@
"show_legend": "Show legends",
"show_adaptive_color": "Show adaptive color",
"smoothing": "Smoothing",
"y_axis": "Axis"
"y_axis": "Axis",
"appearance": "Appearance",
"bounds": "Bounds",
"alignment": "Alignment",
"show": "Display",
"values": {
"aggregate_func": {
"options": {
"avg": "Average",
"median": "Median",
"min": "Minimum",
"max": "Maximum",
"first": "First",
"last": "Last",
"sum": "Sum",
"delta": "Delta",
"diff": "Diff"
}
},
"group_by": {
"options": {
"interval": "Interval",
"date": "Date",
"hour": "Hour"
}
},
"transition": {
"options": {
"smooth": "Smooth",
"hard": "Hard"
}
},
"alignment": {
"options": {
"default": "Default",
"left": "Left",
"right": "Right",
"center": "Center",
"state": "State"
}
},
"state": {
"options": {
"show": "Show",
"hide": "Hide",
"last": "Last"
}
},
"graph": {
"options": {
"line": "Line",
"bar": "Bar",
"hide": "Hide"
}
},
"fill": {
"options": {
"show": "Show",
"hide": "Hide",
"fade": "Fade"
}
},
"points": {
"options": {
"show": "Show",
"hide": "Hide",
"hover": "Hover"
}
},
"labels": {
"options": {
"show": "Show",
"hide": "Hide",
"hover": "Hover"
}
},
"y_axis": {
"options":{
"primary": "Primary",
"secondary": "Secondary"
}
}
}
}
26 changes: 23 additions & 3 deletions src/localize/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@ const languages = {

const DEFAULT_LANG = 'en';

function processTranslations(obj) {
let keys = [];
Object.keys(obj).forEach((key) => {
if (typeof obj[key] === 'object') {
const subKeys = processTranslations(obj[key]);
keys = keys.concat(subKeys.map(subkey => `${key}.${subkey}`));
} else {
keys.push(key);
}
});
return keys;
}

export default function setupTranslations(hass) {
const lang = hass.locale.language || DEFAULT_LANG;
const hassObject = hass;
const resources = hassObject.resources[hass.locale.language];
const languageObject = languages[lang] || languages[DEFAULT_LANG];

Object.entries(languageObject).forEach(([key, value]) => {
if (key !== 'default') {
resources[`ui.panel.lovelace.editor.card.mgc.${key}`] = value;
const keys = processTranslations(languageObject);

keys.forEach((key) => {
if (!key.startsWith('default')) {
const nestedKeys = key.split('.');
const value = nestedKeys.reduce((a, c) => a[c], languageObject);

if (value) {
resources[`ui.panel.lovelace.editor.card.mgc.${key}`] = value;
}
}
});
}
Expand Down

0 comments on commit 100dd42

Please sign in to comment.