Add localization to templates
This commit is contained in:
		
							parent
							
								
									8790289006
								
							
						
					
					
						commit
						90eab9b92a
					
				
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@ -7,7 +7,7 @@ import { bindActionHandler } from "./action.js";
 | 
			
		||||
import { createCard, createElement, createEntityRow } from "./lovelace-element.js";
 | 
			
		||||
import { moreInfo } from "./more-info.js";
 | 
			
		||||
import { popUp, closePopUp } from "./popup.js";
 | 
			
		||||
import { parseTemplate, subscribeRenderTemplate } from "./templates.js";
 | 
			
		||||
import { parseTemplate, subscribeRenderTemplate, hasTemplate } from "./templates.js";
 | 
			
		||||
import { hasOldTemplate, parseOldTemplate } from "./old-templates.js";
 | 
			
		||||
import { getData, areaByName, areaDevices, deviceByName, deviceEntities }  from "./devices";
 | 
			
		||||
 | 
			
		||||
@ -43,7 +43,7 @@ class CardTools {
 | 
			
		||||
  static get popUp() { return popUp; }
 | 
			
		||||
  static get closePopUp() { return closePopUp; }
 | 
			
		||||
 | 
			
		||||
  static get hasTemplate() { return hasOldTemplate; }
 | 
			
		||||
  static get hasTemplate() { return (tpl) => hasTemplate(tpl) || hasOldTemplate(tpl); }
 | 
			
		||||
  static parseTemplate(hass, str, specialData = {}) {
 | 
			
		||||
    if (typeof(hass) === "string")
 | 
			
		||||
      return parseOldTemplate(hass, str);
 | 
			
		||||
 | 
			
		||||
@ -19,6 +19,13 @@ export async function parseTemplate(hass, str, specialData = {}) {
 | 
			
		||||
    return hass.callApi("POST", "template", {template: str});
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export function hasTemplate(str) {
 | 
			
		||||
  if(String(str).includes("{%"))
 | 
			
		||||
    return true;
 | 
			
		||||
  if(String(str).includes("{{"))
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function subscribeRenderTemplate(conn, onChange, params) {
 | 
			
		||||
  // params = {template, entity_ids, variables}
 | 
			
		||||
  if(!conn)
 | 
			
		||||
@ -33,7 +40,13 @@ export function subscribeRenderTemplate(conn, onChange, params) {
 | 
			
		||||
  let entity_ids = params.entity_ids;
 | 
			
		||||
 | 
			
		||||
  return conn.subscribeMessage(
 | 
			
		||||
    (msg) => onChange(msg.result),
 | 
			
		||||
    (msg) => {
 | 
			
		||||
      let res = msg.result;
 | 
			
		||||
      // Localize "_(key)" if found in template results
 | 
			
		||||
      const localize_function = /_\([^)]*\)/g;
 | 
			
		||||
      res = res.replace(localize_function, (key) => hass().localize(key.substring(2, key.length-1)) || key);
 | 
			
		||||
      onChange(res)
 | 
			
		||||
    },
 | 
			
		||||
    { type: "render_template",
 | 
			
		||||
      template,
 | 
			
		||||
      variables,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user