From fa11fbc85d81bed12a19319877395a886bc75010 Mon Sep 17 00:00:00 2001 From: c727 Date: Fri, 1 Jun 2018 16:07:33 +0200 Subject: [PATCH] Localize notification toasts (#1243) * Localize notification toasts * Use correct placeholder syntax * Lint * Use original string --- src/entrypoints/app.js | 29 +++++++++++++++++++++------- src/managers/notification-manager.js | 6 ++++-- src/translations/en.json | 7 +++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/entrypoints/app.js b/src/entrypoints/app.js index f122b8b3a7..8c4c2cf474 100644 --- a/src/entrypoints/app.js +++ b/src/entrypoints/app.js @@ -9,6 +9,8 @@ import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { setPassiveTouchGestures } from '@polymer/polymer/lib/utils/settings.js'; import { PolymerElement } from '@polymer/polymer/polymer-element.js'; +import LocalizeMixin from '../mixins/localize-mixin.js'; + import { ERR_INVALID_AUTH, subscribeEntities, @@ -46,7 +48,7 @@ window.removeInitMsg = function () { } }; -class HomeAssistant extends PolymerElement { +class HomeAssistant extends LocalizeMixin(PolymerElement) { static get template() { return html` @@ -182,23 +184,36 @@ class HomeAssistant extends PolymerElement { conn.callService(domain, service, serviceData || {}) .then( () => { - var message; - var name; + let message; + let name; if (serviceData.entity_id && this.hass.states && this.hass.states[serviceData.entity_id]) { name = computeStateName(this.hass.states[serviceData.entity_id]); } if (service === 'turn_on' && serviceData.entity_id) { - message = 'Turned on ' + (name || serviceData.entity_id) + '.'; + message = this.localize( + 'ui.notification_toast.entity_turned_on', + 'entity', name || serviceData.entity_id + ); } else if (service === 'turn_off' && serviceData.entity_id) { - message = 'Turned off ' + (name || serviceData.entity_id) + '.'; + message = this.localize( + 'ui.notification_toast.entity_turned_off', + 'entity', name || serviceData.entity_id + ); } else { - message = 'Service ' + domain + '/' + service + ' called.'; + message = this.localize( + 'ui.notification_toast.service_called', + 'service', `${domain}/${service}` + ); } notifications.showNotification(message); }, function () { - notifications.showNotification('Failed to call service ' + domain + '/' + service); + const msg = this.localize( + 'ui.notification_toast.service_call_failed', + 'service', `${domain}/${service}` + ); + notifications.showNotification(msg); return Promise.reject(); } ), diff --git a/src/managers/notification-manager.js b/src/managers/notification-manager.js index 34245b9922..c55eff9347 100644 --- a/src/managers/notification-manager.js +++ b/src/managers/notification-manager.js @@ -2,7 +2,9 @@ import '@polymer/paper-toast/paper-toast.js'; import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { PolymerElement } from '@polymer/polymer/polymer-element.js'; -class NotificationManager extends PolymerElement { +import LocalizeMixin from '../mixins/localize-mixin.js'; + +class NotificationManager extends LocalizeMixin(PolymerElement) { static get template() { return html` - + `; } diff --git a/src/translations/en.json b/src/translations/en.json index 3645f4bae8..4be10565e8 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -398,6 +398,13 @@ "remember": "Remember", "log_in": "Log in" }, + "notification_toast": { + "entity_turned_on": "Turned on {entity}.", + "entity_turned_off": "Turned off {entity}.", + "service_called": "Service {service} called.", + "service_call_failed": "Failed to call service {service}.", + "connection_lost": "Connection lost. Reconnecting…" + }, "sidebar": { "developer_tools": "Developer tools", "log_out": "Log out"