lovelace-template-entity-row
lovelace-template-entity-row copied to clipboard
Support context variables in localizations
This add support for passing parameters to localization keys that require them. For instance:
_(ui.duration.second, count, 30)
@swiergot Could you tell me, will it solve this issue? https://github.com/thomasloven/lovelace-template-entity-row/issues/82
@ildar170975 Yes, it will. Something like this:
_(ui.errors.config.key_missing, key, {{ 'some parameter' }})
Here is a build with this change if you want to try it:
https://raw.githubusercontent.com/swiergot/lovelace-template-entity-row/test_build/template-entity-row.js
@swiergot Checked this PR at last. Thank you! Tested with some resource strings with arguments. IMHO the most useful could be this one (also - for minutes, days etc):
- type: custom:template-entity-row
entity: sun.sun
state: >-
_(ui.duration.hour, count, {{"3"}}), _(ui.duration.hour, count, {{"1"}})
returning number of hours "3 hours", "1 hour".
I am mostly interested in patterns like "23 hours ago" like a standard secondary_info: last-changed
.
Consider this code:
type: entities
entities:
- type: section
label: standard
- entity: input_number.test_number_3
secondary_info: last-changed
- type: section
label: "relative_time"
- type: custom:template-entity-row
entity: input_number.test_number_3
secondary: |
{{relative_time(states[config.entity].last_changed)}} ago
state: >-
{{states(config.entity)}}
- type: section
label: localization
- type: custom:template-entity-row
entity: input_number.test_number_3
secondary: >-
{% set LAST_CHANGED = relative_time(states[config.entity].last_changed)
-%} {%- set VALUE = LAST_CHANGED.split(" ")[0] -%} {%- if LAST_CHANGED is
search("second") -%}
{%- set UNIT = "second" -%}
{%- elif LAST_CHANGED is search("minute") -%}
{%- set UNIT = "minute" -%}
{%- elif LAST_CHANGED is search("hour") -%}
{%- set UNIT = "hour" -%}
{%- elif LAST_CHANGED is search("day") -%}
{%- set UNIT = "day" -%}
{%- else -%}
{%- set UNIT = "week" -%}
{%- endif -%} _(ui.duration.{{UNIT}}, count, {{VALUE}})
state: >-
{{states(config.entity)}}
There are 3 rows for the same entity:
-- "standard" row;
--
template-entity-row
with using relative_time
and hard-coded "ago" label;
-- template-entity-row
with using localization.
Here is what happens when switched to Russian:
What we can see is:
-- relative_time
returns a value in English always;
-- localization works.
Thanks a lot for your good job!
But - for completeness we need to add a localized "ago" label.
And I could not find this localization in resources (document.querySelector("home-assistant").hass.resources;
).
Do you know anything about it?
Hi, thanks for testing.
I think the frontend does it this way:
new Intl.RelativeTimeFormat('ru').format(-10, 'hour')
Which doesn't help unfortunately. At least not with template-entity-row.
I too have this problem and I simply add "ago" in my language:
{% set unit = time_ago | regex_findall_index("(second|minute|hour|day|week)s?") %}
_(ui.duration.{{ unit }}, count, {{ count }}) temu
I simply add "ago" in my language:
Much shorter version of my variant, thank you!
Well, have to keep adding "ago" then.
@thomasloven Can we have this merged please?
Functionality provided by this PR is very useful!
Thanks!
And sorry it's taken so long...
Does anyone know what is a current replacement for the ui.duration.second
(hour
, minute
) constant?
It seems to be removed from HA.
Cannot find a similar one in console.