From c8c21e6fac3b6c9763afa4f7f14ec8c188161c83 Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Tue, 19 Dec 2017 09:18:50 -0500 Subject: [PATCH] Refactor out our localize custom variant (#743) --- js/common/util/compute_state_display.js | 14 +++--- .../shopping-list/ha-panel-shopping-list.html | 8 ++-- src/cards/ha-entities-card.html | 6 +-- src/cards/ha-media_player-card.html | 8 ++-- .../entity/ha-state-label-badge.html | 6 +-- src/components/ha-sidebar.html | 24 +++++----- src/state-summary/state-card-display.html | 6 +-- .../state-card-media_player.html | 8 ++-- src/util/hass-mixins.html | 13 ++---- .../common/util/compute_state_display.js | 46 +++++++++---------- 10 files changed, 67 insertions(+), 72 deletions(-) diff --git a/js/common/util/compute_state_display.js b/js/common/util/compute_state_display.js index 45dd562b4f..8468d16487 100644 --- a/js/common/util/compute_state_display.js +++ b/js/common/util/compute_state_display.js @@ -3,17 +3,17 @@ import formatDateTime from './format_date_time.js'; import formatDate from './format_date.js'; import formatTime from './format_time.js'; -export default function computeStateDisplay(haLocalize, stateObj, language) { +export default function computeStateDisplay(localize, stateObj, language) { if (!stateObj._stateDisplay) { const domain = computeStateDomain(stateObj); if (domain === 'binary_sensor') { // Try device class translation, then default binary sensor translation if (stateObj.attributes.device_class) { stateObj._stateDisplay = - haLocalize(`state.${domain}.${stateObj.attributes.device_class}`, stateObj.state); + localize(`state.${domain}.${stateObj.attributes.device_class}.${stateObj.state}`); } if (!stateObj._stateDisplay) { - stateObj._stateDisplay = haLocalize(`state.${domain}.default`, stateObj.state); + stateObj._stateDisplay = localize(`state.${domain}.default.${stateObj.state}`); } } else if (stateObj.attributes.unit_of_measurement) { stateObj._stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement; @@ -43,16 +43,16 @@ export default function computeStateDisplay(haLocalize, stateObj, language) { } } else if (domain === 'zwave') { if (['initializing', 'dead'].includes(stateObj.state)) { - stateObj._stateDisplay = haLocalize('state.zwave.query_stage', stateObj.state, 'query_stage', stateObj.attributes.query_stage); + stateObj._stateDisplay = localize(`state.zwave.query_stage.${stateObj.state}`, 'query_stage', stateObj.attributes.query_stage); } else { - stateObj._stateDisplay = haLocalize('state.zwave.default', stateObj.state); + stateObj._stateDisplay = localize(`state.zwave.default.${stateObj.state}`); } } else { - stateObj._stateDisplay = haLocalize(`state.${domain}`, stateObj.state); + stateObj._stateDisplay = localize(`state.${domain}.${stateObj.state}`); } // Fall back to default or raw state if nothing else matches. stateObj._stateDisplay = stateObj._stateDisplay - || haLocalize('state.default', stateObj.state) || stateObj.state; + || localize(`state.default.${stateObj.state}`) || stateObj.state; } return stateObj._stateDisplay; diff --git a/panels/shopping-list/ha-panel-shopping-list.html b/panels/shopping-list/ha-panel-shopping-list.html index 29663f35fb..f0e8716ee1 100644 --- a/panels/shopping-list/ha-panel-shopping-list.html +++ b/panels/shopping-list/ha-panel-shopping-list.html @@ -66,7 +66,7 @@ -
[[haLocalize('panel', 'shopping_list')]]
+
[[localize('panel.shopping_list')]]
[[haLocalize('ui.panel.shopping-list', 'clear_completed')]] + >[[localize('ui.panel.shopping-list.clear_completed')]]
@@ -97,7 +97,7 @@ @@ -124,7 +124,7 @@
- [[haLocalize('ui.panel.shopping-list', 'microphone_tip')]] + [[localize('ui.panel.shopping-list.microphone_tip')]]
diff --git a/src/cards/ha-entities-card.html b/src/cards/ha-entities-card.html index 9d4cadf9d0..129191c8ff 100644 --- a/src/cards/ha-entities-card.html +++ b/src/cards/ha-entities-card.html @@ -72,17 +72,17 @@ class HaEntitiesCard extends groupEntity: Object, title: { type: String, - computed: 'computeTitle(states, groupEntity, haLocalize)', + computed: 'computeTitle(states, groupEntity, localize)', }, }; } - computeTitle(states, groupEntity, haLocalize) { + computeTitle(states, groupEntity, localize) { if (groupEntity) { return window.hassUtil.computeStateName(groupEntity).trim(); } const domain = window.hassUtil.computeDomain(states[0]); - return (haLocalize && haLocalize('domain', domain)) || domain.replace(/_/g, ' '); + return (localize && localize(`domain.${domain}`)) || domain.replace(/_/g, ' '); } computeTitleClass(groupEntity) { diff --git a/src/cards/ha-media_player-card.html b/src/cards/ha-media_player-card.html index 81490cdd5e..99e4c69a2a 100644 --- a/src/cards/ha-media_player-card.html +++ b/src/cards/ha-media_player-card.html @@ -153,7 +153,7 @@
[[computeStateName(stateObj)]] -
[[computePrimaryText(haLocalize, playerObj)]]
+
[[computePrimaryText(localize, playerObj)]]
[[playerObj.secondaryTitle]]
@@ -310,10 +310,10 @@ class HaMediaPlayerCard extends return new window.MediaPlayerEntity(hass, stateObj); } - computePrimaryText(haLocalize, playerObj) { + computePrimaryText(localize, playerObj) { return playerObj.primaryTitle - || haLocalize('state.media_player', playerObj.stateObj.state) - || haLocalize('state.default', playerObj.stateObj.state) || playerObj.stateObj.state; + || localize(`state.media_player.${playerObj.stateObj.state}`) + || localize(`state.default.${playerObj.stateObj.state}`) || playerObj.stateObj.state; } computePlaybackControlIcon(playerObj) { diff --git a/src/components/entity/ha-state-label-badge.html b/src/components/entity/ha-state-label-badge.html index 81b6681c27..8b7f5b1a9c 100644 --- a/src/components/entity/ha-state-label-badge.html +++ b/src/components/entity/ha-state-label-badge.html @@ -40,7 +40,7 @@ value='[[computeValue(state)]]' icon='[[computeIcon(state)]]' image='[[computeImage(state)]]' - label='[[computeLabel(haLocalize, state)]]' + label='[[computeLabel(localize, state)]]' description='[[computeDescription(state)]]' > @@ -137,14 +137,14 @@ class HaStateLabelBadge extends return state.attributes.entity_picture || null; } - computeLabel(haLocalize, state) { + computeLabel(localize, state) { const domain = window.hassUtil.computeDomain(state); if (state.state === 'unavailable' || ['device_tracker', 'alarm_control_panel'].includes(domain)) { // Localize the state with a special state_badge namespace, which has variations of // the state translations that are truncated to fit within the badge label. Translations // are only added for device_tracker and alarm_control_panel. - return haLocalize(`state_badge.${domain}`, state.state) || haLocalize('state_badge.default', state.state) || state.state; + return localize(`state_badge.${domain}.${state.state}`) || localize(`state_badge.default.${state.state}`) || state.state; } return state.attributes.unit_of_measurement || null; } diff --git a/src/components/ha-sidebar.html b/src/components/ha-sidebar.html index 48c1af5180..a7f9407f72 100644 --- a/src/components/ha-sidebar.html +++ b/src/components/ha-sidebar.html @@ -104,53 +104,53 @@ - [[haLocalize('panel', 'states')]] + [[localize('panel.states')]] - [[haLocalize('ui.sidebar', 'log_out')]] + [[localize('ui.sidebar.log_out')]]
-
[[haLocalize('ui.sidebar', 'developer_tools')]]
+
[[localize('ui.sidebar.developer_tools')]]
@@ -189,8 +189,8 @@ class HaSidebar extends return hass.config.core.components.indexOf('mqtt') !== -1; } - computePanelName(haLocalize, panel) { - return haLocalize('panel', panel.title) || panel.title; + computePanelName(localize, panel) { + return localize(`panel.${panel.title}`) || panel.title; } computePanels(hass) { diff --git a/src/state-summary/state-card-display.html b/src/state-summary/state-card-display.html index 411158c411..718e112892 100644 --- a/src/state-summary/state-card-display.html +++ b/src/state-summary/state-card-display.html @@ -22,7 +22,7 @@
-
[[computeStateDisplay(haLocalize, stateObj, language)]]
+
[[computeStateDisplay(localize, stateObj, language)]]
@@ -42,8 +42,8 @@ class StateCardDisplay extends window.hassMixins.LocalizeMixin(Polymer.Element) }; } - computeStateDisplay(haLocalize, stateObj, language) { - return window.hassUtil.computeStateDisplay(haLocalize, stateObj, language); + computeStateDisplay(localize, stateObj, language) { + return window.hassUtil.computeStateDisplay(localize, stateObj, language); } } customElements.define(StateCardDisplay.is, StateCardDisplay); diff --git a/src/state-summary/state-card-media_player.html b/src/state-summary/state-card-media_player.html index 46cdfafdc0..bba35a79ea 100644 --- a/src/state-summary/state-card-media_player.html +++ b/src/state-summary/state-card-media_player.html @@ -38,7 +38,7 @@
-
[[computePrimaryText(haLocalize, playerObj)]]
+
[[computePrimaryText(localize, playerObj)]]
[[playerObj.secondaryTitle]]
@@ -68,10 +68,10 @@ class StateCardMediaPlayer extends window.hassMixins.LocalizeMixin(Polymer.Eleme return new window.MediaPlayerEntity(hass, stateObj); } - computePrimaryText(haLocalize, playerObj) { + computePrimaryText(localize, playerObj) { return playerObj.primaryTitle - || haLocalize('state.media_player', playerObj.stateObj.state) - || haLocalize('state.default', playerObj.stateObj.state) || playerObj.stateObj.state; + || localize(`state.media_player.${playerObj.stateObj.state}`) + || localize(`state.default.${playerObj.stateObj.state}`) || playerObj.stateObj.state; } } customElements.define(StateCardMediaPlayer.is, StateCardMediaPlayer); diff --git a/src/util/hass-mixins.html b/src/util/hass-mixins.html index cf200353f5..c592075383 100644 --- a/src/util/hass-mixins.html +++ b/src/util/hass-mixins.html @@ -79,7 +79,10 @@ window.hassMixins.NavigateMixin = Polymer.dedupingMixin(superClass => } }); -/* @polymerMixin */ +/** + * @polymerMixin + * @appliesMixin Polymer.AppLocalizeBehavior + */ window.hassMixins.LocalizeMixin = Polymer.dedupingMixin(superClass => class extends Polymer.mixinBehaviors([Polymer.AppLocalizeBehavior], superClass) { static get properties() { @@ -93,10 +96,6 @@ window.hassMixins.LocalizeMixin = Polymer.dedupingMixin(superClass => type: Object, computed: 'computeResources(hass)', }, - haLocalize: { - type: Function, - computed: 'computeHaLocalize(localize)', - }, }; } @@ -107,10 +106,6 @@ window.hassMixins.LocalizeMixin = Polymer.dedupingMixin(superClass => computeResources(hass) { return hass && hass.resources; } - - computeHaLocalize(localize) { - return (namespace, message, ...args) => localize(namespace + '.' + message, ...args); - } }); diff --git a/test-mocha/common/util/compute_state_display.js b/test-mocha/common/util/compute_state_display.js index 93d9c0600d..5f4c5386e9 100644 --- a/test-mocha/common/util/compute_state_display.js +++ b/test-mocha/common/util/compute_state_display.js @@ -3,9 +3,9 @@ import { assert } from 'chai'; import computeStateDisplay from '../../../js/common/util/compute_state_display'; describe('computeStateDisplay', () => { - const haLocalize = function (namespace, message, ...args) { + const localize = function (message, ...args) { // Mock Localize function for testing - return namespace + '.' + message + (args.length ? ': ' + args.join(',') : ''); + return message + (args.length ? ': ' + args.join(',') : ''); }; it('Localizes binary sensor defaults', () => { @@ -15,7 +15,7 @@ describe('computeStateDisplay', () => { attributes: { }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.binary_sensor.default.off'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.binary_sensor.default.off'); }); it('Localizes binary sensor device class', () => { @@ -26,13 +26,13 @@ describe('computeStateDisplay', () => { device_class: 'moisture', }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.binary_sensor.moisture.off'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.binary_sensor.moisture.off'); }); it('Localizes binary sensor invalid device class', () => { - const altHaLocalize = function (namespace, message, ...args) { - if (namespace === 'state.binary_sensor.invalid_device_class') return null; - return haLocalize(namespace, message, ...args); + const altLocalize = function (message, ...args) { + if (message === 'state.binary_sensor.invalid_device_class.off') return null; + return localize(message, ...args); }; const stateObj = { entity_id: 'binary_sensor.test', @@ -41,7 +41,7 @@ describe('computeStateDisplay', () => { device_class: 'invalid_device_class', }, }; - assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'state.binary_sensor.default.off'); + assert.strictEqual(computeStateDisplay(altLocalize, stateObj, 'en'), 'state.binary_sensor.default.off'); }); it('Localizes sensor value with units', () => { @@ -52,7 +52,7 @@ describe('computeStateDisplay', () => { unit_of_measurement: 'm', }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), '123 m'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), '123 m'); }); it('Localizes input_datetime with full date time', () => { @@ -70,7 +70,7 @@ describe('computeStateDisplay', () => { second: 13, }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'November 18, 2017, 11:12 AM'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'November 18, 2017, 11:12 AM'); }); it('Localizes input_datetime with date', () => { @@ -88,7 +88,7 @@ describe('computeStateDisplay', () => { second: 13, }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'November 18, 2017'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'November 18, 2017'); }); it('Localizes input_datetime with time', () => { @@ -106,7 +106,7 @@ describe('computeStateDisplay', () => { second: 13, }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), '11:12 AM'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), '11:12 AM'); }); it('Localizes zwave ready', () => { @@ -117,7 +117,7 @@ describe('computeStateDisplay', () => { query_stage: 'Complete', }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.zwave.default.ready'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.zwave.default.ready'); }); it('Localizes zwave initializing', () => { @@ -128,7 +128,7 @@ describe('computeStateDisplay', () => { query_stage: 'Probe', }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.zwave.query_stage.initializing: query_stage,Probe'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.zwave.query_stage.initializing: query_stage,Probe'); }); it('Localizes cover open', () => { @@ -138,13 +138,13 @@ describe('computeStateDisplay', () => { attributes: { }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.cover.open'); }); it('Localizes unavailable', () => { - const altHaLocalize = function (namespace, message, ...args) { - if (namespace === 'state.sensor') return null; - return haLocalize(namespace, message, ...args); + const altLocalize = function (message, ...args) { + if (message === 'state.sensor.unavailable') return null; + return localize(message, ...args); }; const stateObj = { entity_id: 'sensor.test', @@ -152,11 +152,11 @@ describe('computeStateDisplay', () => { attributes: { }, }; - assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'state.default.unavailable'); + assert.strictEqual(computeStateDisplay(altLocalize, stateObj, 'en'), 'state.default.unavailable'); }); it('Localizes custom state', () => { - const altHaLocalize = function () { + const altLocalize = function () { // No matches can be found return null; }; @@ -166,7 +166,7 @@ describe('computeStateDisplay', () => { attributes: { }, }; - assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'My Custom State'); + assert.strictEqual(computeStateDisplay(altLocalize, stateObj, 'en'), 'My Custom State'); }); it('Only calculates state display once per immutable state object', () => { @@ -176,9 +176,9 @@ describe('computeStateDisplay', () => { attributes: { }, }; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.cover.open'); stateObj.state = 'closing'; - assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open'); + assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), 'state.cover.open'); }); });