For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /developers/blog/2023-03-27-entity_name_translations.md.

翻译 entity 的 name 和 attributes

现在可以翻译 entity 的 name,且这优于在 Python 实现中硬编码自然语言名称。此外,entity components 提供共享翻译,例如用于 binary sensor device class 的翻译,应使用这些翻译以避免重复翻译相同内容。

此外,frontend 现已完全支持翻译 entity state attributes 的 name 和值。

Warning

通过 translation_key 属性指向翻译目前仅支持具有 unique_id 的 entity。

此外,翻译 entity name 要求 has_entity_name 属性设置为 True

翻译 entity name

以下 strings.json 示例适用于 translation_key 属性设置为 thermostat_modesensor entity:

{
  "entity": {
    "sensor": {
      "thermostat_mode": {
        "name": "Thermostat mode"
      }
    }
  }
}

以下 strings.json 示例适用于 translation_key 属性设置为 temperature_sensorsensor entity,其中使用了 sensor 集成提供的共享翻译:

{
  "entity": {
    "sensor": {
      "temperature_sensor": {
        "name": "[%key:component::sensor::entity_component::temperature::name%]"
      }
    }
  }
}

翻译 entity attributes

以下 strings.json 示例适用于 translation_key 属性设置为 ubercooldemoclimate entity,它包含自定义的 fan_modeswing_mode 设置:

{
  "entity": {
    "climate": {
      "ubercool": {
        "state_attributes": {
          "fan_mode": {
            "state": {
              "auto_high": "Auto High",
              "auto_low": "Auto Low",
              "on_high": "On High",
              "on_low": "On Low"
            }
          },
          "swing_mode": {
            "state": {
              "1": "1",
              "2": "2",
              "3": "3",
              "auto": "Auto",
              "off": "Off"
            }
          }
        }
      }
    }
  }
}

更多详情请参见 entity name translationentity attribute translationentity 文档。