lovelace-template-entity-row icon indicating copy to clipboard operation
lovelace-template-entity-row copied to clipboard

Support context variables in localizations

Open swiergot opened this issue 2 years ago • 5 comments

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

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

swiergot avatar Aug 24 '22 08:08 swiergot

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

ildar170975 avatar Aug 24 '22 18:08 ildar170975

@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 avatar Aug 24 '22 19:08 swiergot

@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?

ildar170975 avatar Sep 18 '22 14:09 ildar170975

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

swiergot avatar Sep 18 '22 16:09 swiergot

I simply add "ago" in my language:

Much shorter version of my variant, thank you!

Well, have to keep adding "ago" then.

ildar170975 avatar Sep 18 '22 21:09 ildar170975

@thomasloven Can we have this merged please?

swiergot avatar Apr 04 '23 09:04 swiergot

Functionality provided by this PR is very useful!

ildar170975 avatar Apr 06 '23 18:04 ildar170975

Thanks!

And sorry it's taken so long...

thomasloven avatar Feb 11 '24 20:02 thomasloven

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.

ildar170975 avatar Feb 11 '24 23:02 ildar170975