mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-13 12:26:35 +00:00
Don't expose translation methods on window (#1194)
This commit is contained in:
parent
4de7cbec30
commit
0789c0884c
@ -22,7 +22,7 @@ import '../managers/notification-manager.js';
|
||||
import '../resources/ha-style.js';
|
||||
import '../resources/html-import/polyfill.js';
|
||||
import '../util/ha-pref-storage.js';
|
||||
import '../util/hass-translation.js';
|
||||
import { getActiveTranslation, getTranslation } from '../util/hass-translation.js';
|
||||
import '../util/legacy-support';
|
||||
import '../util/roboto.js';
|
||||
import hassCallApi from '../util/hass-call-api.js';
|
||||
@ -108,7 +108,7 @@ class HomeAssistant extends PolymerElement {
|
||||
}
|
||||
|
||||
loadResources(fragment) {
|
||||
window.getTranslation(fragment).then((result) => {
|
||||
getTranslation(fragment).then((result) => {
|
||||
this._updateResources(result.language, result.data);
|
||||
});
|
||||
}
|
||||
@ -167,7 +167,7 @@ class HomeAssistant extends PolymerElement {
|
||||
themes: null,
|
||||
panelUrl: this.panelUrl,
|
||||
|
||||
language: window.getActiveTranslation(),
|
||||
language: getActiveTranslation(),
|
||||
// If resources are already loaded, don't discard them
|
||||
resources: (this.hass && this.hass.resources) || null,
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import translationMetadata from '../../build-translations/translationMetadata.json';
|
||||
|
||||
window.getActiveTranslation = function () {
|
||||
export function getActiveTranslation() {
|
||||
// Perform case-insenstive comparison since browser isn't required to
|
||||
// report languages with specific cases.
|
||||
const lookup = {};
|
||||
@ -58,18 +58,18 @@ window.getActiveTranslation = function () {
|
||||
|
||||
// Final fallback
|
||||
return 'en';
|
||||
};
|
||||
}
|
||||
|
||||
// Store loaded translations in memory so translations are available immediately
|
||||
// when DOM is created in Polymer. Even a cache lookup creates noticeable latency.
|
||||
const translations = {};
|
||||
|
||||
window.getTranslation = function (fragment, translationInput) {
|
||||
export function getTranslation(fragment, translationInput) {
|
||||
const translation = translationInput || getActiveTranslation();
|
||||
const metadata = translationMetadata.translations[translation];
|
||||
if (!metadata) {
|
||||
if (translationInput !== 'en') {
|
||||
return window.getTranslation(fragment, 'en');
|
||||
return getTranslation(fragment, 'en');
|
||||
}
|
||||
return Promise.reject(new Error('Language en not found in metadata'));
|
||||
}
|
||||
@ -89,14 +89,14 @@ window.getTranslation = function (fragment, translationInput) {
|
||||
delete translations[translationFingerprint];
|
||||
if (translationInput !== 'en') {
|
||||
// Couldn't load selected translation. Try a fall back to en before failing.
|
||||
return window.getTranslation(fragment, 'en');
|
||||
return getTranslation(fragment, 'en');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
});
|
||||
}
|
||||
return translations[translationFingerprint];
|
||||
};
|
||||
}
|
||||
|
||||
// Load selected translation into memory immediately so it is ready when Polymer
|
||||
// initializes.
|
||||
window.getTranslation();
|
||||
getTranslation();
|
||||
|
Loading…
x
Reference in New Issue
Block a user