thomasloven / lovelace-template-entity-row

🔹 Display whatever you want in an entities card row.
MIT License
210 stars 16 forks source link

Support context variables in localizations #85

Closed swiergot closed 4 months ago

swiergot commented 1 year ago

This add support for passing parameters to localization keys that require them. For instance:

_(ui.duration.second, count, 30)

ildar170975 commented 1 year ago

@swiergot Could you tell me, will it solve this issue? https://github.com/thomasloven/lovelace-template-entity-row/issues/82

swiergot commented 1 year ago

@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

ildar170975 commented 1 year ago

@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". image

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)}}

image 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: image

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?

swiergot commented 1 year ago

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
ildar170975 commented 1 year ago

I simply add "ago" in my language:

Much shorter version of my variant, thank you!

Well, have to keep adding "ago" then.

swiergot commented 1 year ago

@thomasloven Can we have this merged please?

ildar170975 commented 1 year ago

Functionality provided by this PR is very useful!

thomasloven commented 4 months ago

Thanks!

And sorry it's taken so long...

ildar170975 commented 4 months ago

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.