Miicroo / ha-birthdays

Birthday integration for HomeAssistant
24 stars 8 forks source link

[Feature Request] Translation or custom "value" #17

Closed mhoogenbosch closed 1 month ago

mhoogenbosch commented 2 months ago

I live in the Netherlands, and use the birthdays for showing the kids how long it takes before their birthday is. Now it is only possible to have xx 'days', it would be really nice if it translates to Dutch 'dagen' and 'dag' for 1.

Another approach could be to be able to alter the 'days' to anything you'd like.

Miicroo commented 2 months ago

@mhoogenbosch Nice, would be good to have! Do you already use dutch as your language in HA, so we can use that, or were you thinking more along the lines of a configuration value where you have to specify it yourself?

mhoogenbosch commented 2 months ago

I think it makes sense to comply with the default language of ha indeed. Using Dutch as primary language for ha.

Miicroo commented 1 month ago

@mhoogenbosch PR is ready! Found a few caveats, the language that is used is the language that is set on your homeassistant instance. I first wanted it to reflect the language that a logged in user can set in their profile, but that doesn't make sense since you can have multiple users with multiple languages, but the sensors are always the same. In the case of for instance push notifications we don't have a user context, and thus it is the instance language that has to decide the values.

I guess most users don't change language that often, so I didn't add any event listener to automatically update language upon changes. If you do change the language (from http://localhost:8123/config/general or configuration.yaml) you have to restart HA for the changes to take effect.

Miicroo commented 1 month ago

Another caveat is that, since state is always number of days left, if you list these with auto-entities you will get "0 dagen" if the birthday is today. This can be changed if you use a custom attribute instead (the elif (this.state or -1) == 1 can be merged with the below elif if you want to view it as 1 dag):

birthdays:
  config:
    attributes:
      custom_state: >
        {% if (this.state or -1) == 0 %}
          Vandaag
        {% elif (this.state or -1) == 1 %}
          Morgen
        {% elif (this.state or -1) > 1 %}
          {{ this.state }} {{ this.unit_of_measurement }}
        {% else %}
          Unknown
        {% endif %}
  birthdays:
    - name: more config here for birthdays...

Of course than the UI will have to look at this attribute instead of the state, but just wanted to show that it is possible.