Files
Home-Assistant/custom_components/maintenance_supporter/helpers/i18n.py
T
2026-07-08 10:43:39 -04:00

20 lines
819 B
Python

"""Internationalization helpers."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
def normalize_language(hass: HomeAssistant) -> str:
"""Return the HA UI language as a lowercase 2-letter table key.
HA emits regional language codes (e.g. ``zh-Hans``, ``zh-Hant``,
``pt-BR``), but the integration's localization tables — the
calendar/notification/config-flow Python string dicts, the
``name_<lang>`` template fields, and the ``styles.ts`` panel strings —
are keyed by the bare 2-letter prefix. Centralizing the normalization
keeps every consumer identical, so a regional-code user never silently
falls back to English. Defaults to ``en`` when the language is unset.
"""
return (getattr(hass.config, "language", None) or "en")[:2].lower()